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.
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-126.96.36.199" newVersion="188.8.131.52" /> </dependentAssembly> </assemblyBinding> </runtime> </configuration>