Fixing assembly redirects issue quickly

26 Nov 2021

One of the most frustrating issues faced by .NET developers during application development can be narrowed down to two or three top issues - one of it is Assembly Binding redirects issue. And I have been its victim quiet lot of time.

It starts with updating simple / single NuGet package that is critical for your project, then issue spreads into multiple dependencies through new conflicts until you reach a meltdown ;). You might scrap out your recent change and try a different path with no success. 

 Well, you are in luck!

Understanding this dependency graphs is tougher for humans, but a lot easier for binary beings. And all this time, you have a tool that does this and yet you don't seem to notice it yet (It did happen for me)

I've recently found this simple solution and its gem. You can just run following command in Package Manager Console to fix the redirects properly in all your projects within seconds.

PS> Get-Project –All | Add-BindingRedirect

This will solve your miseries in no time.

Update

In case, your issue is not yet resolved after above command let execution. Then your assembly redirects section seems to be messed up too much with different incompatible redirects, which might have resulted in wrong ones even for the PS command.

So, to properly fix all redirects follow below steps

  1. Remove all assemblyBinding redirects from web.config
  2. Add following to the .csproj file
  3. <PropertyGroup>
      <AutoGenerateBindingRedirects>true</AutoGenerateBindingRedirects>
      <GenerateBindingRedirectsOutputType>true</GenerateBindingRedirectsOutputType>
    </PropertyGroup>
  4. Build the project. Go to the bin folder, there should be a (WebAppName).dll.config file.
  5. Open and copy the correct assembly redirects from there, to your web.config
  6. Remove the additional tags added in step 3 from .csproj file

Happy coding!

Related Posts