Find unused / unnecessary assemblyBinding redirects
Asked Answered
P

2

24

It seems like there is so many binding redirects in our web.config that I either:

  1. look unnecessary
  2. are for assemblies I don't see being referenced anywhere in our solution

This is just a sample of some portion of the binding redirects:

  <dependentAssembly>
    <assemblyIdentity name="Microsoft.Azure.KeyVault.Core" publicKeyToken="31bf3856ad364e35" culture="neutral" />
    <bindingRedirect oldVersion="0.0.0.0-2.0.0.0" newVersion="2.0.0.0" />
  </dependentAssembly>
  <dependentAssembly>
    <assemblyIdentity name="Microsoft.Data.Services.Client" publicKeyToken="31bf3856ad364e35" culture="neutral" />
    <bindingRedirect oldVersion="0.0.0.0-5.8.3.0" newVersion="5.8.3.0" />
  </dependentAssembly>
  <dependentAssembly>
    <assemblyIdentity name="Microsoft.Data.OData" publicKeyToken="31bf3856ad364e35" culture="neutral" />
    <bindingRedirect oldVersion="0.0.0.0-5.8.3.0" newVersion="5.8.3.0" />
  </dependentAssembly>
  <dependentAssembly>
    <assemblyIdentity name="Microsoft.Data.Edm" publicKeyToken="31bf3856ad364e35" culture="neutral" />
    <bindingRedirect oldVersion="0.0.0.0-5.8.3.0" newVersion="5.8.3.0" />
  </dependentAssembly>
  <dependentAssembly>
    <assemblyIdentity name="System.Diagnostics.DiagnosticSource" publicKeyToken="cc7b13ffcd2ddd51" culture="neutral" />
    <bindingRedirect oldVersion="0.0.0.0-4.0.2.1" newVersion="4.0.2.1" />
  </dependentAssembly>
  <dependentAssembly>
    <assemblyIdentity name="Microsoft.Owin.Security" publicKeyToken="31bf3856ad364e35" culture="neutral" />
    <bindingRedirect oldVersion="0.0.0.0-4.0.0.0" newVersion="4.0.0.0" />
  </dependentAssembly>
  <dependentAssembly>
    <assemblyIdentity name="Microsoft.SqlServer.Types" publicKeyToken="89845dcd8080cc91" culture="neutral" />
    <bindingRedirect oldVersion="10.0.0.0-11.0.0.0" newVersion="14.0.0.0" />
  </dependentAssembly>
  <dependentAssembly>
    <assemblyIdentity name="System.Threading.Tasks.Extensions" publicKeyToken="cc7b13ffcd2ddd51" culture="neutral" />
    <bindingRedirect oldVersion="0.0.0.0-4.1.1.0" newVersion="4.1.1.0" />
  </dependentAssembly>
  <dependentAssembly>
    <assemblyIdentity name="System.ValueTuple" publicKeyToken="cc7b13ffcd2ddd51" culture="neutral" />
    <bindingRedirect oldVersion="0.0.0.0-4.0.2.0" newVersion="4.0.2.0" />
  </dependentAssembly>
  <dependentAssembly>
    <assemblyIdentity name="System.Web.Helpers" publicKeyToken="31bf3856ad364e35" />
    <bindingRedirect oldVersion="1.0.0.0-3.0.0.0" newVersion="3.0.0.0" />
  </dependentAssembly>
  <dependentAssembly>
    <assemblyIdentity name="System.Web.WebPages" publicKeyToken="31bf3856ad364e35" />
    <bindingRedirect oldVersion="1.0.0.0-3.0.0.0" newVersion="3.0.0.0" />
  </dependentAssembly>
  <dependentAssembly>
    <assemblyIdentity name="System.Web.Mvc" publicKeyToken="31bf3856ad364e35" />
    <bindingRedirect oldVersion="1.0.0.0-5.2.4.0" newVersion="5.2.4.0" />
  </dependentAssembly>
  <dependentAssembly>
    <assemblyIdentity name="Microsoft.ApplicationInsights" publicKeyToken="31bf3856ad364e35" culture="neutral" />
    <bindingRedirect oldVersion="0.0.0.0-2.5.1.0" newVersion="2.5.1.0" />
  </dependentAssembly>

I think that at some point Visual Studio decided to add lots of them automatically.

Is there a way to verify if any of the binding redirects are needed or automatically verify / remove them?

Panne answered 26/4, 2018 at 7:5 Comment(1)
They are probably dependencies for other assemblies.Simdars
P
57

The solution to this is actually quite simple and elegant.

  1. Remove all your binding redirects in Web.config / app.config;
  2. Go to Package Manager Console;
  3. Enter the command Add-BindingRedirect (you can also specify a target project using -ProjectName "SpecificProject");
  4. All necessary binding redirects are generated;
  5. Run your application and see if it works properly. If not, add any missing binding redirects that the command missed.
Panne answered 31/1, 2019 at 11:23 Comment(7)
Add-BindingRedirect * for multi-project solutionsParrott
When I try this, the command spits out a bunch of stuff but no web.config or app.config files are modified.. Do you have to manually go through the output and add each binding redirect by hand?Evesham
@MikeChristensen no, it did it all by itself, I only had to add one or two that it didn't catch (quite rare cases)Panne
Weird.. Maybe it's broken on the newer builds or something, or my solution has too many projects..Evesham
@MikeChristensen happened to me a few times until I realized I forgot to select the default project in the Package Manager Console windowExuberance
Why would I NOT do it for all projects?Exuberance
Doesn't work for me on VS2022 latest as of today. The only way it always works is to use GenerateBindingRedirectsOutputType set to true in the project file together with AutoGenerateBindingRedirects. Then in the bin folder of the project, I look for the project dll with a .config suffix and the redirects will all be there and I have to copy paste them in my web.config.Gosport
M
7

Most of them are added as part of default template. You can safely remove many of them based on yr need in the application, from binding as well as project reference. This way, if accidentally they are being used as dependency somewhere, you will get to know instantly. For example: -

  • "Microsoft.ApplicationInsights": Auditing application
  • System.Web.Helpers: Html helpers for MVC
  • System.ValueTuple: Tuple as a data structure where you can access each property by name
  • System.Threading.Tasks.Extensions: TPL extension methods
  • Microsoft.SqlServer.Types: Datatypes registered within SQL server being consumed in app code directly
  • Microsoft.Owin.Security: Owin as identity management
  • Microsoft.Data.Edm: Entity framework data modelling
  • Microsoft.Data.OData: Open Data services

Note that binding redirect is specifically used when your code originally referred/requested an older version and you are providing a newer version. If the version being used is actually the same as the one being provided (primarily for main framework components (rather than updates delivered by NuGet), you can remove bindingRedirect section altogether..

For safety purpose, comment out each section and then run application, if things don't work, you can uncomment the section.

Marxmarxian answered 26/4, 2018 at 10:0 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.