Fabian's Mix

Mixins, .NET, and more

Could not load something or something else

without comments

Have you ever tried to debug an exception message similar to the following?

Could not load file or assembly ‘XXX, Version=1.11.19.2, Culture=neutral, PublicKeyToken=fee00910d6e5f53b’ or one of its dependencies.

Sometimes, but not always, the message includes additional details. For example, the following might be appended to the exception message: “The system cannot find the file specified.”

To debug this, first of all, try to find out which kind of exception it is.

Then, check the file.

  • FileNotFoundException: Is it actually there?
  • BadImageFormatException: Is it a valid .NET assembly? (Check this via peverify.exe.)
  • FileLoadException: Did you deploy¬†the correct version of the file?

Up to now, this has been very basic, nothing special going on. But what if you’ve checked the above questions, and still can’t figure it out? Ie. the file is there, it is valid, and it is of the version you think is right? (Or peverify.exe won’t tell you what’s wrong, since it can’t load the file either.)

Don’t fret – as the message tells you, the error needn’t be caused by assembly XXX at all! The reason might also be one of its dependencies, ie. one of the assemblies referenced by XXX.

Thank you, dear exception message, but which one of the twenty-something assemblies referenced by XXX do you mean?

To find this out, you can inspect the Fusion log. This is the trace information written by Fusion, .NET’s assembly loader. To view it, run Fuslogvw.exe, the assembly binding log viewer. In the viewer, enable the kind of logging you need (eg. log bind failures), then run your piece of software, then click “Refresh”, and you should see detailed logging information. If you don’t because the log tells you the load result is cached, you need to restart the application causing the error.

Great this, but shouldn’t the same information be contained in the exceptions’ FusionLog property?

Exactly, it should be, and it is; but only if you’ve enabled assembly bind failure logging. (You can do that by by setting the registry key HKLMSoftwareMicrosoftFusionLogFailures to 1 (DWORD value).) Once logging has been enabled, your application or debugger can read the failure information from that property, and the ToString representation of the exception should also contain the log message.

You can find more information about Fusion and assembly loading in Suzanne Cook’s .NET CLR Notes blog, which (among other things) has great information about how Fusion actually works and how to debug it.

Written by Fabian

January 23rd, 2009 at 11:15 am

Posted in .NET development

Leave a Reply