Redirecting Assembly Versions in .NET

As Kyle on South Park would say, "I learned something today." While working on one of our internal applications at work, I was getting the following exception:

"System.IO.FileLoadException: Could not load file or assembly 'Microsoft.Practices.ServiceLocation, Version=, 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)
File name: 'Microsoft.Practices.ServiceLocation, Version=, Culture=neutral, PublicKeyToken=31bf3856ad364e35'"

But the funny thing was that all instances of this assembly in the solution were using version 1.3, and not 1.0 as the exception states. In fact, I could find no reference to version 1.0 anywhere in the config files.

I enabled assembly bind failure logging (by setting the registry value [HKLM\Software\Microsoft\Fusion!EnableLog] (DWORD) to 1), and was given a little more info: the assembly which was trying to make use of the older version of Microsoft.Practices.ServiceLocation was one of our internal DLLs.

At my previous job, I would have updated this DLL to use the latest version in use by the other apps and deployed a new NuGet package on our internal NuGet server. But at my current job, we don't have one yet. But one of the other developers clued me in on a different way to fix the issue which I hadn't known about: redirecting the assembly version.

I'd seen these nodes in config files before but didn't exactly know what they were for. Now I do. Adding the following to the web.config file of the project that was encountering the exception fixed the problem.

In the XML above, you'll notice that the dependent assembly is being referenced, and the bindingRedirect element specifies a version number range via the oldVersion attribute, and the version to redirect to via the newVersion attribute. Problem solved. :)


Popular posts from this blog

A Generic Method Using HttpClient to Make a Synchronous Get Request

The Cause and Solution for the "System.Runtime.Serialization.InvalidDataContractException: Type 'System.Threading.Tasks.Task`1[YourTypeHere]' cannot be serialized." Exception

How To Mock Out Child Components In Unit Tests of Angular 2 Code