开发者

Serialize type in x64 assembly to XML

I have a .dll that is compiled for 64bit, containing a class which I can serialize to XML. The library is compiled in Visual Studio using the "AnyCPU" flag.

I have another process that is referencing that .dll, creating an object of that type then attempting to serialize it to XML like this:

XmlSerializer serializer = new XmlSerializer(typeof(COLLADA));
using (TextWriter w = new StreamWriter(m_colladaPath, false, System.Text.Encoding.UTF8))
{
serializer.Serialize(w, collada.COLLADA); //exception occurs here
}

This process works correctly on a 32bit machine. When attempting on a 64bit machine, the code compiles without error but at runtime I receive a "Target Invocation" error as follows:

System.Reflection.TargetInvocationException occurred Message="Exception has been thrown by the target of an invocation." Source="mscorlib"

StackTrace: at System.RuntimeMethodHandle._InvokeMethodFast(Object target, Object[] arguments, SignatureStruct& sig, MethodAttributes methodAttributes, RuntimeTypeHandle typeOwner) at System.RuntimeMethodHandle.InvokeMethodFast(Object target, Object[] arguments, Signature sig, MethodAttributes methodAttributes, RuntimeTypeHandle typeOwner) InnerException: System.BadImageFormatException Message="Could not load file or assembly 'goBIM_API, Version=1.0.3922.23514, Culture=neutral, PublicKeyToken=null' or one of its dependencies. An attempt was made to load a program with an incorrect format." Source="xnzfsdbo" FileName="goBIM_API, Version=1.0.3922.23514, Culture=neutral, PublicKeyToken=null" FusionLog= StackTrace: at Microsoft.Xml.Serialization.GeneratedAssembly.XmlSerializationWriterCOLLADA.Write653_COLLADA(Object o) InnerException:

I have triple checked that everything is now compiled for AnyCPU, and have also tried compiling 开发者_如何学Pythoneverything, including the library, to "x64", without success. I am not using any bit specific pointers, i.e. "IntPtr" in the library.

UPDATE: I created a stand alone application using this library, created a test object and serialized it without error (all in x64). The problem seems to stem from the fact that the serialization is happening in a method called by another application. That is, the method is part of a plug-in interface for another application. Something about the application's API is messing with the serialization. The application is Autodesk's Revit. Not sure anyone here is a Revit guru?


Looks like goBIM_API or one of its references are 32 bit. If you have any assembly marked x86 it will not load into a x64 assembly.


Did you compile the DLL using Multi-threaded (/MT) so that all dependencies are put in the binary of your DLL?


Because the serializer was being called in a method called by another process, the goBIM_API.dll needed to be located in the same directory as that processes' executable. After moving the .dll to that location, serialization works fine.


It looks as if you are using a third-party library (goBIM_API.dll) which is only present as a 32-bit version. You get the exception because this 32-bit assembly cannot be loaded into 64-bit process.

To fix this problem, either get a 64-bit version of that library, or, what is easier, set the target platform to x86 for all your executables.

Using x86 on a 64-bit platform usually should not have any noticeable performance hit. The only downside is that you can't get above the 2GB/3GB memory limit of a process.

Update:

Is AutoDesk Revit a 32-bit application? That's what it looks like. Then you will have to compile your libary using the x86 platform target to fix the error.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜