Michael Whelan

behaviour driven blog

NuGet Add-BindingRedirect

This is one of those "note to self" posts. I recently came across this NuGet feature which is useful on those infrequent occasions when I need to do binding redirects. Hopefully others will find it useful too!

I was wanting to use AutoSubstitute, the cool little auto mocking container from fellow TestStacker Rob Moore that uses Autofac to resolve unknown dependencies from NSubstitute. The problem was that AutoSubstitute is strongly named and when an assembly has a strong name, the binding to that assembly becomes very strict. Because AutoSubstitute currently binds to NSubstitute 1.4.0 and I was using 1.4.3 I was getting a FileLoadException.

No worries, just need to add a binding redirect. Thankfully, I no longer have to do it by hand. Open the NuGet Package Manager Console, specify the appropriate Default Project and enter the command Add-BindingRedirect.

alt text

As if by magic, an app.config is added to the project (if one doesn't exist already) and the appropriate information added.

<configuration>
  <runtime>
    <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
      <dependentAssembly>
        <assemblyIdentity name="NSubstitute" publicKeyToken="92dd2e9066daa5ca" culture="neutral" />
        <bindingRedirect oldVersion="0.0.0.0-1.4.3.0" newVersion="1.4.3.0" />
      </dependentAssembly>
    </assemblyBinding>
  </runtime>
</configuration>

Sweet!

About Michael Whelan

Michael Whelan is a Technical Lead with over 20 years’ experience in building (and testing!) applications on the Microsoft stack. He is passionate about applying agile development practices, such as BDD and continuous delivery, to agile processes. These days his primary focus is ASP.Net MVC Core and Azure. He contributes to a number of open source frameworks through TestStack.

comments powered by Disqus
Google

Google