Runtime assembly redirecting.

We have an application which is split into a number of modules.
Each module is built against continuing changing versions of other modules, so we have a problem when we come to update our client’s software when a bug fix has been provided.

I recently used a method of rebinding which fellow blogger Matthew Reynolds was unaware of, so I thought I would share it with you all.

As some of you may already be aware it is possible to add into the configuration file for an application an assemblyBinding element. This allows you to bind a specific assembly version to use a different version, so the following example will bind 'Some.Assembly.Name' version 1.0.100.0 to use version 1.0.200.0, thus allowing the application with this configuration file to use the later version of 'Some.Assembly.Name' even though the application was built against version 1.0.100.0.

<runtime>
    <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
      <dependentAssembly>
        <assemblyIdentity name="Some.Assembly.Name" publicKeyToken="17ec30d55c1fa739" culture="neutral" />
        <bindingRedirect oldVersion="1.0.100.0" newVersion="1.0.200.0" />
      </dependentAssembly>
    </assemblyBinding>
</runtime>

This method had some problems associated with it:, this will only bind 1.0.100.0 to version 1.0.200.0, if we also had another application that used 1.0.150.0 then this wouldn't be redirected, so we had to come up with an alternative, which was the following:

<runtime>
    <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
      <dependentAssembly>
        <assemblyIdentity name="Some.Assembly.Name" publicKeyToken="17ec30d55c1fa739" culture="neutral" />
        <bindingRedirect oldVersion="0.0.0.0-65535.65535.65535.65535" newVersion="1.0.200.0" />
      </dependentAssembly>
    </assemblyBinding>
</runtime>

As you can see now the version number has been provided as a range, a range for all the possible versions that need to be redirected. So now all references that are made to 'Some.Assembly.Name' will be redirected to the correct version. Problem solved.

Be aware that this method only needs to be used once your assemblies are assigned strong names, without a strong name the framework will bind to a dependent assembly that it wasn't built against without the configuration being there.

No Comments