Fervent Coder

Coding Towards Utopia...by Rob Reynolds
posts - 278 , comments - 431 , trackbacks - 0

My Links


Rob Reynolds

Subscribe to Fervent Coder RSS
Subscribe to Fervent Coder by Email

About Me

I manage several open source projects. Need...
   ...package management for Windows?
   ...automated builds?
   ...database change management (migrations)?
   ...your application to check email?
   ...a monitoring utility?

I also write for

Like what you are reading? Want to buy me a cup of coffee?
PayPal - The safer, easier way to pay online!


Post Categories


.NET Binding Redirects – Updating Referenced Assemblies Without Recompiling Code

Have you ever seen this error?

System.IO.FileLoadException: Could not load file or assembly ‘nameOfAssembly’, Version=specificVersion, Culture=neutral, PublicKeyToken=publicKey’ or one of it's dependencies. The located assembly’s manifest definition does not match the assembly reference. (Exception from HRESULT: 0x80131040)


This means you’ve replaced the specific version of the third party assembly with either an earlier or an updated version. The assembly that uses it is compiled to point to a specific version of the assembly and now will not load. I’ve seen this the most with applications that use log4net and/or NHibernate and third party assemblies that also use log4net and/or NHibernate.

Sometimes you just can’t recompile code, but you want to update the version the assembly uses. Sometimes it’s troublesome (or even impossible) to go back to all of the third party assemblies and get their source and recompile everything to use the same version. This may be the biggest pain some people see in trying to use and upgrade OSS. But if you know what’s going on, it’s very easy to make everything work happily together without a lot of work.

I’ve fought for hours trying to figure out and correct this error. I’ve found that there is an easier alternative and I wanted to share so that others could see it is not that hard to deal with. It’s possible to make code look for an updated assembly (or an earlier version) by adding some elements to the config file.

This is done through a binding redirect.  In the configuration file for the application or DLL that uses the assembly, you include something like this:

  <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
      <assemblyIdentity name="NHibernate"
                        culture="neutral" />
        <bindingRedirect oldVersion="" newVersion="" />

You are instructing the application or DLL that during runtime, for a particular dependent assembly to use a particular version when an application and/or other assembly is looking for the older version.

You can see here that I pointed the application from all versions of NHibernate to use, even though the application was only looking for This helps other assemblies in the same AppDomain (normally all of the assemblies within your application’s executing code) also update whatever versions they are tied to to also use the same version.

The error in the above picture occurred in Castle.Facilities.NHibernateIntegration.dll, but I didn’t add a Castle.Facilities.NhibernateIntegration.dll.config file. It wasn’t the entry point for my application. I have another asssembly, let’s call it Foo.exe, that references both Castle.Facilities.NHibernateIntegration.dll and NHibernate.dll. Foo.exe itself is actually using an updated reference to NHibernate version The NHibernateIntegration was compiled against NHibernate version I need to add a Foo.exe.confg file and add the code above to it. That way when Castle goes looking for any version of NHibernate between and, the AppDomain instructs it to use version If another assembly was looking for another version of NHibernate, it would also be instructed to use

Keep in mind this is not recommended for use when there are breaking changes between two versions of a dependent assembly (due to errors or inconsistent behaviors). Hopefully this will help you if you ever run into this issue.

kick it on DotNetKicks.com

Print | posted on Sunday, July 19, 2009 3:15 PM | Filed Under [ Code ]



# re: .NET Binding Redirects – Updating Referenced Assemblies Without Recompiling Code

This is an amazingly simple solution to a problem that has happened so many times to me. This almost makes me want to pound my head into the keyboard knowing I could've solved this with a single line added to my config.

A different note, the validation you have on your comment box acts a little strangely on FF3.5 when i start typing and auto fill the rest of my text the validation triggers as invalid and flashes red text then triggers valid and disappears causing the objects to bounce around inside the comment box from the quick dom changes.
8/3/2009 1:43 PM | Chris Marisic

# re: .NET Binding Redirects – Updating Referenced Assemblies Without Recompiling Code

@Chris: Thanks! As for the browser issues, I'll pass that along to my host (geeks with blogs). We run a customized version of SubText.
8/3/2009 5:17 PM | Robz
Comments have been closed on this topic.

Powered by: