Yavor Georgiev

Yavor is a Product Manager at goextend.io, a startup that makes in-product extensibility and integrations painless for SaaS developers. Previously at Hulu and Microsoft Azure.

Debugging SGEN LoaderExceptions errors

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:

SGEN error in Visual Studio

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.

  1. In Visual Studio create a dummy Console Application. We won’t actually use this, it’s just a way for us to start sgen.exe in the debugger.
  2. Go to the Debug tab of the project properties and set
    • Start external program= C:\Program Files (x86)\Microsoft SDKs\Windows\v7.0A\Bin\NETFX 4.0 Tools\sgen.exe
    • Command line arguments= /f /a:MyAssembly.dll
    • Working directory= C:\FolderContainingMyAssembly
  3. Configure the debugger to break on all exceptions by going to the Debug menu and selecting Exceptions and then checking Thrown next to Common Language Runtime Exceptions
  4. Also ensure that if you go to Tools > Options > Debugging that Enable Just My Code is unchecked.
  5. Then you are ready to press F5. If everything works correctly, Vistual Studio will break at an exception. Right click on sgen.exe anywhere in the Call Stack window and load the symbols from the Microsoft Symbol Servers.
     Loading sgen.exe symbols
  6. Once the symbols have loaded, press F5 until you get to a ReflectionTypeLoadException. At that point you can select View Details and inspect the LoaderExceptions property yourself. This property will usually reveal the real source of the problem:
    ReflectionTypeLoadException details

Hope this is useful!