06 May 2011
Recently we were contacted by a customer who was building a Release version of their assembly in Visual Studio and encountered the following error:
You could get the same error if you attempt to run the sgen.exe tool on the built assembly:
Microsoft ® Xml Serialization support utility [Microsoft ® .NET Framework, Version 4.0.30319.1] Copyright © Microsoft Corporation. All rights reserved. Error: Unable to load one or more of the requested types. Retrieve the LoaderExceptions property for more information. If you would like more help, please type "sgen /?".</p>
The reason why this happens is that in Release builds, Visual Studio attempts to generate a serialization assembly containing the types in your solution, to improve XmlSerializer serialization performance, should you choose to serialize your types. This can be disabled by going to the Build tab of the project properties and setting “Generate serialization assembly” to “Off”.
However if you legitimately want to take advantage of the serialization assembly, this error can be very frustrating to diagnose. The exception text leads you to believe Fusion is having a problem loading one of the dependent assemblies that your assembly is referencing. That may be the case, and you can easily diagnose that problem by launching the Fusion Log Viewer, then enabling assembly logging, and then rebuilding your project (or rerunning sgen.exe). Scott Hanselman has a good write-up on how to do this.
Sometimes after inspecting the Fusion log, you’ll see that all assemblies were infact loaded correctly, but you’re still getting this error. In recent investigation we found that sometimes sgen.exe will show this error, and it won’t have to do with an assembly loading problem. Here is how to get to the bottom of that case.
Hope this is useful!