Monday, October 19, 2009

Assembly binding error

Recently I had an error changing the Enterprise Library version (to 4.1) in a project I was developing.

The error was:

System.IO.FileLoadException was unhandled by user code
Message="Could not load file or assembly 'Microsoft.Practices.EnterpriseLibrary.Logging, Version=4.1.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35' or one of its dependencies. The located assembly's manifest definition does not match the assembly reference.(Exception from HRESULT: 0x80131040)"

I had similar error in the past which I somehow solved and I remembered it was related to the location of the configuration file.

Anyhow...

After reading the above bolded error description I've changed the version number in the configuration file thinking that was the problem - but couldn't pass this error.

Searching for a solution I found a nice & simple tool from Microsoft that clearly tells you what the problem is (if all problems were solved so easily...).
The tool name is Assembly Binding Log Viewer.

Running the application when this tool is in the background will log every assembly binding (you can choose between any and/or only exceptions).




Double clicking the 'bad' binding will open up a detailed log looking something like this:

LOG: This bind starts in default load context.
LOG: Using application configuration file: D:\...\DiegJukeboxManagerLoader.vshost.exe.Config
LOG: Using machine configuration file from C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\config\machine.config.
LOG: Post-policy reference: Microsoft.Practices.EnterpriseLibrary.Logging, Version=4.1.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35
LOG: GAC Lookup was unsuccessful.
LOG: Attempting download of new URL file:///D:/../Microsoft.Practices.EnterpriseLibrary.Logging.DLL.
LOG: Assembly download was successful. Attempting setup of file: D:\...\Microsoft.Practices.EnterpriseLibrary.Logging.dll
LOG: Entering run-from-source setup phase.
LOG: Assembly Name is: Microsoft.Practices.EnterpriseLibrary.Logging, Version=4.1.0.0, Culture=neutral, PublicKeyToken=null
WRN: Comparing the assembly name resulted in the mismatch: PUBLIC KEY TOKEN
ERR: The assembly reference did not match the assembly definition found.
ERR: Failed to complete setup of assembly (hr = 0x80131040). Probing terminated.

As you can see in plain english the error was that the dll was without the publicKeyToken (since I downloaded the sources of EL & the installation program compiled it...) - so the binding failed since the configuration file contained Microsoft's publicToken (remained there from previous version...).

Concuslion: next time you encounter any kind of assembly binding error - don't work too hard, download this tool and use it!!

Diego.

No comments:

Post a Comment