Infralution Support Forum Index Infralution Support
Support groups for Infralution products
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

InvalidOperationException Deserializing from XML file

 
Post new topic   Reply to topic    Infralution Support Forum Index -> .NET Encryptor Support
View previous topic :: View next topic  
Author Message
andisa



Joined: 14 Sep 2011
Posts: 5

PostPosted: Wed Sep 14, 2011 10:59 am    Post subject: InvalidOperationException Deserializing from XML file Reply with quote

I am evaluating the .NET Encryptor but got stuck on this error.

I have managed to encrypt our application, embed it in an .exe file together with the bootstrap program. The bootstrap program starts our application correctly but when it comes to loading a data object from an XML file the following error occurs (translated from Swedish):

System.InvalidOperationException occurred
Message=There is an error in the XML document (0, 0).
Source=System.Xml
StackTrace:
at System.Xml.Serialization.XmlSerializer.Deserialize(XmlReader xmlReader, String encodingStyle, XmlDeserializationEvents events)
at System.Xml.Serialization.XmlSerializer.Deserialize(Stream stream)
vid Qutilities.DiskData.Load(String fileName, Type objectType, Type[] subObjectTypes)
InnerException: System.TypeInitializationException
Message=Type initiator for Microsoft.Xml.Serialization.GeneratedAssembly.XmlSerializationReaderApplicationSettingsData throw an exception.
Source=pkz0ew1y
TypeName=Microsoft.Xml.Serialization.GeneratedAssembly.XmlSerializationReaderApplicationSettingsData
StackTrace:
at Microsoft.Xml.Serialization.GeneratedAssembly.XmlSerializationReaderApplicationSettingsData..ctor()
at Microsoft.Xml.Serialization.GeneratedAssembly.XmlSerializerContract.get_Reader()
at System.Xml.Serialization.TempAssembly.InvokeReader(XmlMapping mapping, XmlReader xmlReader, XmlDeserializationEvents events, String encodingStyle)
at System.Xml.Serialization.XmlSerializer.Deserialize(XmlReader xmlReader, String encodingStyle, XmlDeserializationEvents events)
InnerException: System.NullReferenceException
Message= The object reference does not point to an instance of an object.
Source=pkz0ew1y
StackTrace:
at Microsoft.Xml.Serialization.GeneratedAssembly.XmlSerializationReaderApplicationSettingsData..cctor()

This error does not occur when running our application alone.

Deserializing data from XML to a simpler data object does work.

The class ApplicationSettingsData has no constructor other than the implicit one. It may contain a list of objects instaciated from subclasses of the same mother class, but the error would occur even when the list is empty.
Back to top
View user's profile Send private message
Infralution



Joined: 28 Feb 2005
Posts: 5027

PostPosted: Wed Sep 14, 2011 10:57 pm    Post subject: Reply with quote

Are your lists of objects generic lists?

It seems like there may be an issue with Microsoft's XmlSerialization code if you use generic lists inside an XML serialized object if the assembly that defines the list is dynamically loaded from memory. See this Stack Overflow thread:

http://stackoverflow.com/questions/3505965/deserializing-xml-with-dynamically-loaded-assembly

Since .NET Encryptor decrypts your assembly and dynamically loads it you are seeing the issue when using .NET Encryptor.

The solution suggested in the StackOverflow thread is to use sgen to create the serialization assemblies upfront. Then you would either put the generated *.XmlSerializers.dll s in your bootstrap bin directory or possibly encrypt them as well and include them in the bootstrapper (same as other assemblies). Pregenerating the XmlSerializers should actually also give your code a performance boost.
_________________
Infralution Support
Back to top
View user's profile Send private message Visit poster's website
andisa



Joined: 14 Sep 2011
Posts: 5

PostPosted: Thu Sep 29, 2011 7:56 am    Post subject: *.XmlSerializers.dll work only before encryption Reply with quote

Thank you for putting me on the right track. I have now managed to generate *.XmlSerializers.dll files which run fine before encryption...

I had to replace calls to XmlSerializer(Type type, Type[] extraTypes) with
[XmlInclude(type)]s and calls to XmlSerializer(Type type). I did not manage to make the console command sgen work but could include SGen in the .csproj file under <Target Name="AfterBuild".

My problem is now that I get a FileNotFoundException when the encrypted application tries to load the original .exe:

System.IO.FileNotFoundException occurred
Message = Unable to load file or assembly Hotdisk, Version = 7.0.0.1, Culture = neutral, PublicKeyToken = null or one of its dependencies. Can not find file.
Source = Hotdisk.XmlSerializers
filename = Hotdisk, Version = 7.0.0.1, Culture = neutral, PublicKeyToken = null
FusionLog ==== state information prior to bonding ===
LOG: User = AndersI2 \ Anders Isacson
LOG: displayName = Hotdisk, Version = 7.0.0.1, Culture = neutral, PublicKeyToken = null
(Fully-Specified)
LOG: Programbas = file: / / / C: / Hotdisk/SourceCode/ApplicationGUI2007/HotBootstrapx86/bin/x86/Debug /
LOG: Initial private path = NULL
Calling assembly: (Unknown).
===
LOG: This binding starts in default load context.
LOG: Using application configuration file: C: \ Hotdisk \ Source Code \ ApplicationGUI2007 \ HotBootstrapx86 \ bin \ x86 \ Debug \ TPS.exe.Config
LOG: Using host configuration file:
LOG: Configuration file from C: \ Windows \ Microsoft.NET \ Framework \ v4.0.30319 \ config \ machine.config used.
LOG: The principle is not used for reference at this time (private, custom, partial, or location-based assembly binding).
LOG: Trying to retrieve new URL, file: / / / C: / Hotdisk/SourceCode/ApplicationGUI2007/HotBootstrapx86/bin/x86/Debug/Hotdisk.DLL.
LOG: Trying to retrieve new URL, file: / / / C: / Hotdisk/SourceCode/ApplicationGUI2007/HotBootstrapx86/bin/x86/Debug/Hotdisk/Hotdisk.DLL.
LOG: Trying to retrieve new URL, file: / / / C: / Hotdisk/SourceCode/ApplicationGUI2007/HotBootstrapx86/bin/x86/Debug/Hotdisk.EXE.
LOG: Trying to retrieve new URL, file: / / / C: / Hotdisk/SourceCode/ApplicationGUI2007/HotBootstrapx86/bin/x86/Debug/Hotdisk/Hotdisk.EXE.

Stack trace:
at Microsoft.Xml.Serialization.GeneratedAssembly.XmlSerializerContract.CanSerialize (Type type)
at System.Xml.Serialization.TempAssembly.LoadGeneratedAssembly (Type type, String default namespace, XmlSerializerImplementation & Contract)
at System.Xml.Serialization.XmlSerializer .. ctor (Type type, String default namespace)
at System.Xml.Serialization.XmlSerializer .. ctor (Type type)
at Qutilities.DiskData.Load (String filename, Type Object Type)


In the output window I can see that encrypted files are loaded including both my *.XmlSerializers.dll files and that one .xml file was read without any serialiser created at run time:

:
'TPS.exe' (Managed (v4.0.30319)): Loaded 'C:\Hotdisk\SourceCode\ApplicationGUI2007\HotBootstrapx86\bin\x86\Debug\AssemblyLoader4x86.dll'
'TPS.exe' (Managed (v4.0.30319)): Loaded 'Hotdisk'
'TPS.exe' (Managed (v4.0.30319)): Loaded 'Qutilities', Symbols loaded.
'TPS.exe' (Managed (v4.0.30319)): Loaded 'HotLicenseLib', Symbols loaded.
'TPS.exe' (Managed (v4.0.30319)): Loaded 'C:\Windows\assembly\GAC_MSIL\ComponentFactory.Krypton.Toolkit\3.0.0.0__a87e673e9ecb6e8e\ComponentFactory.Krypton.Toolkit.dll'
Trace Information Thermal Constants Analyser 7.0.0.1
Date: 29-09-2011 08:33:52
Computer name: ANDERSI2
User name: Anders Isacson
OS: Windows 7 Service Pack 1 32-bit
Culture: sv-SE
Resolution: {Width=1366, Height=768}
Time Trace type Caption Parameters
'TPS.exe' (Managed (v4.0.30319)): Loaded 'Qutilities.XmlSerializers'
08:33:52 File Load C:\HotDiskCommonHiddenData\Rs.xml
'TPS.exe' (Managed (v4.0.30319)): Loaded 'Hotdisk.XmlSerializers'
'TPS.exe' (Managed (v4.0.30319)): Loaded 'C:\Windows\Microsoft.Net\assembly\GAC_MSIL\mscorlib.resources\v4.0_4.0.0.0_sv_b77a5c561934e089\mscorlib.resources.dll'


One difference is that the problematic assembly Hotdisk was an .exe and the others .dlls. One can also see above that it doesn't say "Symbols loaded" for Hotdisk.

How can I solve this? Do I need to move all code handling xml files into a separate dll that will not be encrypted and embedded?

Regards @nders

P.S. Yes we have serialized generic lists.
Infralution wrote:
Are your lists of objects generic lists?

It seems like there may be an issue with Microsoft's XmlSerialization code if you use generic lists inside an XML serialized object if the assembly that defines the list is dynamically loaded from memory. See this Stack Overflow thread:

http://stackoverflow.com/questions/3505965/deserializing-xml-with-dynamically-loaded-assembly

Since .NET Encryptor decrypts your assembly and dynamically loads it you are seeing the issue when using .NET Encryptor.

The solution suggested in the StackOverflow thread is to use sgen to create the serialization assemblies upfront. Then you would either put the generated *.XmlSerializers.dll s in your bootstrap bin directory or possibly encrypt them as well and include them in the bootstrapper (same as other assemblies). Pregenerating the XmlSerializers should actually also give your code a performance boost.
Back to top
View user's profile Send private message
Infralution



Joined: 28 Feb 2005
Posts: 5027

PostPosted: Thu Sep 29, 2011 9:38 am    Post subject: Reply with quote

Hmm.. sorry I can see that my suggestion didn't really help. Moving all the code that is using XML serialization into a separate unencrypted assembly would definitely work - but that may be a lot of effort and it might not be possible to restructure your code like this. Obviously it would also leave that part of your code exposed to decompilation.

Another option would be to change your serialized code to not use generic collections (use the older non-generic collections instead) - it seems these are the source of the problems with dynmically generated serializers. In this case you would revert to using dynamically generated XML serializers.

A final option would be to write your own custom XML serialization code - this would avoid the issue altogether. Personally I don't like using generated XML Serializers because there is too much magic happening in the background and when you want handle issues like backward or forward compatibility it becomes difficult unless you have full control.
_________________
Infralution Support
Back to top
View user's profile Send private message Visit poster's website
andisa



Joined: 14 Sep 2011
Posts: 5

PostPosted: Fri Oct 21, 2011 8:58 am    Post subject: Reply with quote

Thanks, I chose to implement custom XML serialization based on the XmlDocument class.

That works fin and solved a few other issues!
Back to top
View user's profile Send private message
Display posts from previous:   
Post new topic   Reply to topic    Infralution Support Forum Index -> .NET Encryptor Support All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group