12

I'm using an XmlSerializer to deserialize a particular type in mscorelib.dll

XmlSerializer ser = new XmlSerializer( typeof( [.Net type in System] ) );
return ([.Net type in System]) ser.Deserialize( new StringReader( xmlValue ) );

This throws a caught FileNotFoundException when the assembly is loaded:

"Could not load file or assembly 'mscorlib.XmlSerializers, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089' or one of its dependencies. The system cannot find the file specified."

FusionLog:

=== Pre-bind state information ===
LOG: User = ###
LOG: DisplayName = mscorlib.XmlSerializers, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089, processorArchitecture=x86
 (Fully-specified)
LOG: Appbase = file:///C:/localdir
LOG: Initial PrivatePath = NULL
Calling assembly : System.Xml, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089.
===
LOG: This bind starts in default load context.
LOG: Using application configuration file: C:\localdir\bin\Debug\appname.vshost.exe.Config
LOG: Using machine configuration file from c:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\config\machine.config.
LOG: Post-policy reference: mscorlib.XmlSerializers, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089, processorArchitecture=x86
LOG: Attempting download of new URL file:///C:/localdir/bin/Debug/mscorlib.XmlSerializers.DLL.
LOG: Attempting download of new URL file:///C:/localdir/bin/Debug/mscorlib.XmlSerializers/mscorlib.XmlSerializers.DLL.
LOG: Attempting download of new URL file:///C:/localdir/bin/Debug/mscorlib.XmlSerializers.EXE.
LOG: Attempting download of new URL file:///C:/localdir/bin/Debug/mscorlib.XmlSerializers/mscorlib.XmlSerializers.EXE.

As far as I know there is no mscorlib.XmlSerializers.DLL, I think the DLL name has bee auto generated by .Net looking for the serializer.

You have the option of creating a myApplication.XmlSerializers.DLL when compiling to optimise serializations, so I assume this is part of the framework's checking for it.

The problem is that this appears to be causing a delay in loading the application - it seems to hang for a few seconds at this point.

Any ideas how to avoid this or speed it up?

starblue
  • 55,348
  • 14
  • 97
  • 151
Keith
  • 150,284
  • 78
  • 298
  • 434
  • The type I'm dealing with is `RSAParameters` which is being used as part if some system cryptography stuff. I've worked around this now by storing the encrypted key by another means and creating a new RSAParameters myself. It seems like a relatively common thing to want to serialise (i.e. encryption/decryption keys). – Keith Aug 15 '08 at 13:15
  • I've run into this problem while trying to run the testing tool Ranorex. I have a workaround for now but was not able to fix it and their support has yet to contact me. :-/ – Dan Csharpster Oct 24 '17 at 14:38
  • Check .NET version of generated mscorlib.XmlSerializers.dll and your project. It must be same. Chek this: https://stackoverflow.com/a/52848813/5639198 – Sashus Oct 17 '18 at 06:59
  • @Sashus Thanks for the help, but this question is from over a decade ago, I don't have access to the project that had this issue any more. – Keith Oct 18 '18 at 23:21

3 Answers3

4

The delay is because, having been unable to find the custom serializer dll, the system is building the equivalent code (which is very time-consuming) on the fly.

The way to avoid the delay is to have the system build the DLL, and make sure it's available to the .EXE - have you tried this?

Will Dean
  • 39,055
  • 11
  • 90
  • 118
  • Thanks @Will Dean, that's kinda what I figured, but it seems too slow even for that. If it were my own assembly creating the serialisation assembly shouldn't be an issue, but how would I do that for mscorlib? – Keith Aug 15 '08 at 11:10
4

Okay, so I ran into this problem and have found a solution for it specific to my area.

This occurred because I was trying to serialize a list into an XML document (file) without an XML root attribute. Once I added the following files, the error goes away.

XmlRootAttribute rootAttribute = new XmlRootAttribute();
rootAttribute.ElementName = "SomeRootName";
rootAttribute.IsNullable = true;

Dunno if it'll fix your problem, but it fixed mine.

Nick Craver
  • 623,446
  • 136
  • 1,297
  • 1,155
2

I'm guessing now. but:

  1. The system might be generating a serializer for the whole of mscorlib, which could be very slow.
  2. You could probably avoid this by wrapping the system type in your own type and serialising that instead - then you'd get a serializer for your own assembly.
  3. You might be able to build the serializer for mscorlib with sgen.exe, which was the old way of building serializer dlls before it got integrated into VS.
Will Dean
  • 39,055
  • 11
  • 90
  • 118
  • Thanks again. I think it is (1), but I can't do (2) as it's a struct. I'll try (3) – Keith Aug 15 '08 at 11:49
  • > but I can't do (2) as it's a struct. I know I'm being dim here, but what's the problem with it being a struct - obviously there may be some extra copying going on, but relative to the costs of xml serialisation it seems unlikely that's very significant. What is the system.xx type anyway? – Will Dean Aug 15 '08 at 12:57