Debugging Assembly Loading Failures
There are situations sometimes when .NET loads wrong assembly or can’t load one at all. Usually we can see “Could not load file or assembly” in that case.
In one of my project we had a situation that gave a real world example for post I was thinking about quite long time. There was a reference of enterprise library in the project and everything worked fine in local & production environment. But one day due to the power problem, production server was rebooted and system stopped picking assembly of enterprise library. There can be many situations like that, when in our opinion all is perfect but isn’t and .NET fails with loading assembly or loads incorrect one.
.Net framework has given a wonderful tool “Fusion – Assembly Binding Log” to diagnose such problems. If you will take a look into .NET Framework directory you will see a fusion.dll. This inconspicuous file is responsible for finding locating the correct assembly and, what is more important, can produce a log of that. As a pair for that we have simple tool named fuslogvw.exe that can be found in Visual Studio directory. This small tool is an Assembly Binding Log Viewer and is able to get logs created by fusion.dll, save them on disk and of course display.
Turning on Fusion Logging
Type “Fusion” in the Start Menu. The Assembly Binding Log Viewer, or “Fusion Log Viewer” which will tell the CLR to load assembling binding/loading activities to a folder, then let you see them. Be sure to run it as Administrator if you want to change the Settings, otherwise they’ll be grayed out. Alternatively, just set the Registry keys your self. (I just memorized them, as I set them all the time.) Set HKLM\Software\Microsoft\Fusion\ForceLog registry value to 1 and HKLM\Software\Microsoft\Fusion\LogPath registry value to C:\FusionLogs or some path that exists.
Now you can run code/program that causes the problem and click Refresh when it will finish. You should see a log file on the path specified for fusion logs. Double click on any log entry will open whole log for that assembly. Information you can find there is sometimes amazing. By analyzing that information you should be able to find the problem and don’t forget to turn logging of when you finish.