XmlSerializer, .XmlSerializationWriterList1' threw an exception - c#

I've kicked my head with this one for several days now. I've done a lot of research and have found issues closely related but not specific to my case.
Working on a Visual Studio 2008 SSIS package. Using only one scripting component. In this scripting component, I am serializing a stongly typed class (CCMSModel) and trying to write it out to the server's d:\ drive. I am using .NET 3.5. Works just fine on my development machine but breaks when I deploy it to my SQL Server 2008 R2 (SSIS). I've completely trimmed down my code to remove any added complexity:
[Serializable]
public class CCMSModel
{
public string BOBorEOB { get; set; }
}
...
List<CCMSModel> myList = new List<CCMSModel>();
CCMSModel item = new CCMSModel();
item.BOBorEOB = "Test";
myList.Add(item);
string filePath = "d:\\ScheduleFiles\\6xml\\ss.xml";
var serializer = new XmlSerializer(typeof(List<CCMSModel>));
using (var stream = File.OpenWrite(filePath))
{
serializer.Serialize(stream, myList);
}
...
Want to add that I have sufficient rights to write on the server and I am a sysadmin on the sql server. I also have an another SSIS package using Visual Studio 2012 (SSIS) with .NET 4.0 and deploying to SQL Server 2012. This package is EXACTLY the same (did a copy / paste of the code) and it works just fine.
In my research I found an article that I couldn't help but to see a very close relationship to. Same error and same code. However, this particular scenario is using .NET 4.0 and is deploying or executing in an environment that has .NET 4.0 and 4.5. The article contains a link (at the bottom) claiming to solve the problem (specific to the 4.0/4.5 framework). Claims that the XMLSerialize, in 4.5, is a bit different from the one that runs on my C# compiler hence the incompatibility.
InvalidProgramException with XmlSerializer
Although the article present a close resemblance, this isn't specifically my set up (since I am on 3.5). However, it does allude to the fact that I may be experiencing some incompatiblity with the XMLSerialise implementation. I've hunted down to see if this was the case but I've run dry on my search.
I get an this inner exception error:
System.TypeInitializationException: The type initializer for 'Microsoft.Xml.Serialization.GeneratedAssembly.XmlSerializationWriterList1' threw an exception. ---> System.NullReferenceException: Object reference not set to an instance of an object.
at Microsoft.Xml.Serialization.GeneratedAssembly.XmlSerializationWriterList1..cctor()

Related

Trying to call x86 .Net 3.5 SDK in more recent .Net versions

This may be a long shot... I need to create a simple-ish web app that needs to call an OEM supplied SDK that is 10-15, or more, years old (no updates are possible). The SDK is 32bit and is pegged to .Net Framework 3.5. There is still an active NDA for using the SDK so there is not much that I can share.
I have access to Visual Studio 2015, 2017 and 2019. In each version I can go through the process of creating a C# web app and I can select .Net 3.5 as the target framework but the only available template is a "Blank" one. Are there MVC based templates for 3.5? I have googled and have not found any.
That is my first problem. I decided to just move on and try newer .Net versions that might have a more complete template set. I have tried 4.8 most recently.
Using .Net 4.8 and specifying an "x86" platform I can create a simple app. I can call the SDK lib and get values from the simple library "get" methods like "getHeight", "getWidth", etc. The SDK is for an old computer controlled device that still works like a charm. It has a camera and one of the available functions is GetCameraImage.
The SDK comes with documentation and some sample C# "SLN"s. One of those has the following sample code:
var idata = new byte[numOfBytes];
unsafe {
fixed (byte* fixedPtr = idata) {
channel.GetCameraImage((int) numOfBytes, out idata[0]);
}
}
In my code the above just crashes with an "Access Violation". No exceptions are thrown in the debugger. It just dies.
The docs that come with the SDK suggest that the following is valid:
var idata = new byte[numOfBytes];
channel.GetCameraImage((int) numOfBytes, out idata[0]);
but I get the same Access Violation.
Any ideas about the Access Violation?
EDIT
#Serg: Addressing a couple of comments: var idata = new byte[numOfBytes*3] did not work but was a great idea.
#DekuDesu: I am not sure how to exactly verify that the memory is allocated so I did the following:
var idata = new byte[numOfBytes];
for (var i=0;i<numOfBytes;i++) {
idata[1] += 0;
}
channel.GetCameraImage((int) numOfBytes, out idata[0]);
I also tried this with #Serg's offset idea and the violation is definately triggered by the GetCameraImage call.

Are there any dependencies for UI Automation?

I have a Windows application which captures the details from screen based on the configuration. I am using UI Automation to capture the details from the screen. Everything works fine on the developer's machine where Visual Studio is installed. When I run the same application on another system where we have only .NET Framework 4.5 installed, it started behaving strangely, and it's not able to detect the child element.
My question is why it works fine on the developer's machine where Visual Studio and .NET Framework are installed. What's the difference? Is there anything we are missing as far as prerequisites? Any dependencies of UI Automation or any library we are missing..?
Thanks in advance - please help me out.
It looks like a known bug in .NET wrapper around native UIAutomationCore.dll (yes, its core is not a .NET). And it's included into WinVista+ (.NET Framework also adds it even to WinXP).
Here is a C# example how to use native COM API (UIAutomationCore.dll) from C#. Just copying the code here:
using System;
using interop.UIAutomationCore;
namespace PrintDesktopUiaElementNameViaCom
{
class PrintDesktopUiaElementNameViaComProgram
{
static void Main(string[] args)
{
// Instantiate the UIA object:
IUIAutomation _automation = new CUIAutomation();
// Get the root element
IUIAutomationElement rootElement = _automation.GetRootElement();
// Get its name
string rootName = rootElement.CurrentName;
Console.WriteLine(
"The root automation element's name should be 'Desktop'.");
Console.WriteLine("The actual value is: '{0}'", rootName);
}
}
}
Yeah at last after doing day's reading, i came to know the solution is that der is no dependency on Visual studio.
This behavior is due to lack of privileges to the application. so to overcome this behavior we have to get signed our application and one more thing its very important thing is place your executable file in Program Files
Reference Links : https://msdn.microsoft.com/en-us/library/windows/desktop/ee671610(v=vs.85).aspx

Instance of AggregateException causes TargetInvocationException

I have a problem that an instance of an AggregateException causes a TargetInvocationException after a couple of accesses to an Icon resource.
I broke down the problem to the following steps to reproduce (.Net 4.0 full or client profile):
Create a new WinForms application (a console application will not work)
Add an arbitrary icon (.ico file) to the resources
Add the following code to the constructor:
new AggregateException();
for (var i = 0; ; ++i)
{
var icon = Resources.Certificate;
}
You have to change the resource name to the name of your resource.
That's all. Yes I know that this sample doesn't make sense. It's just to illustrate the problem. My working code is much more complex and all of this code is needed.
Without creating this excection the application will work forewer. But if this exception is created the access to the resource will fail with a TargetInvocationException. The InnerException told me that the operation has been finished successfully(?!?!) having a two-line stack trace in System.Drawing.Icon (ctor + Initialize).
What can I do to prevent this problem?
EDIT
It seems to be a problem using Windows 7. A binary which fails on Win 7 will run correctly in Win 8.1.
I found the reason for that problem:
The following system configuration is needed to reproduce the issue:
Windows 7 German Edition
Microsoft .Net Framework 4.5.2 installed (yes I know my binary is compiled against .Net 4.0)
KB2901983 installed
Having a machine which only contains the .Net Framework without KB2901983 the program works fine. After installing KB2901983 the program fails for the same binary (no recompilation required).
I tried to uninstall KB2901983 but it doesn't help. If it was once installed the program will fail. I tested it on a clean Windows 7 German Edition.

Issue with Linq, NpqSql and Entity Framework on one machine only

We have an environment that has been successfully running a postgresql web application with
Entity Framework running on .net 4.0.
Somehow, on my machine only, a single Linq query fails where on others it is successful. The following is the linq query, context of which doesn't particularly matter. Point is, it works on other machines and not mine. Additionally, my machine can successfully do inserts and the like but not this one specific execution.
((SEntities)Context).c_app.MergeOption = MergeOption.AppendOnly;
app = (((from rec in ((SEntities) Context).c_application
where rec.app_id == CStatus.app_id
select rec) as ObjectQuery<c_application>)
.Include("c_status")
.Include("c_tasks")
.Include("c_product")
.Include("c_acct")
).SingleOrDefault();
On co-workers machines, a fresh checkout running in the same server configuration yields a succsful result.
However, on mine, the following is the exception I get:
System.Exception {System.Data.EntityCommandExecutionException}
InnerException:
{"ERROR: 42601: syntax error at or near \"LEFT\""} System.Exception {Npgsql.NpgsqlException}
If I look at the errorSQL entry in the debug window that comes up, a huge SQL is created with "select .... limit 2 left join..." which is invalid and where the error is occurring.
The question is, why is this only happening on my machine, and my compiled code deployed to the others and/or the same code executed on co-workers working? We have recently upgraded to a 64 bit app pool but the DLLs are deployed with it and from the output, are the correct ones.
Obviously I'm missing something on my machine but it's not in our source so its either an iis config or something in the gac that has caused problems.
Edit:
Despite checking, I had installed 4.5 when I did a developer preview install of Visual Studio. Since 4.5 is an in-place upgrade from 4.0, I was missing everything where it said "target 4.0" despite being 4.5. Apparently Microsoft fixed a bunch of bugs in 4.5 for Entity Framework (or made more) and this was one of them. Other people have had similar issues so not sure if it was a bug in 4.0 or a bug introduced in 4.5.
The following fixed the above for anyone who somehow may have
string sql = "SELECT VALUE a FROM c_application AS a WHERE a.app_id = #appId";
ObjectParameter[] queryParams = { new ObjectParameter("appId", CStatus.app_id) };
ObjectQuery<c_application> query = Context.CreateQuery<c_application>(sql, queryParams).Include("c_status").Include("c_tasks").Include("c_product").Include("c_acct");
CApplicationDao cApplicationDao = new CApplicationDao();
SetupDao<c_application>(cApplicationDao);
query.MergeOption = MergeOption.AppendOnly;
app = credApplicationDao.FindSingle(query);

Connecting to an oracle database in C# .Net

Hi I use the following code to connect to an orace database in Visual Studio 2008 and it is falling over with the exception of ServerVersion threw an invalid operation exception at line 3:
m_strConnectionString = Settings.GetMandatoryConfig("databases", "SourceDB");
m_strQueryTerminator = Settings.GetConfig("databases", "QueryTerminator");
m_odbConn = new OleDbConnection(m_strConnectionString);
m_sql = new SQL(DatabaseType.AnsiSQL, false);
m_strConnectionString is a valid TNS names connection string and is as follows:
Provider=MSDAORA;Data Source=myDS;User
ID=myID;Password=myPW
This has previously worked and is using an IIS web server to host the application. I recently converted it from .Net 1.1 (VS 2003) to .Net 3.5/2 (VS 2008) and can't get it working as yet in VS 2008.
Thanks
Maybe you should consider using the OracleConnection class instead of the generic OleDbConnection. Doing this may give you more specific error information that what you're receiving now - and could help you find your problem quicker.
See here for more information on the System.Data.OracleClient namespace included with the .Net framework.

Categories