Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Find unused / unnecessary assemblyBinding redirects

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?

like image 380
Jakub Holovsky Avatar asked Apr 26 '18 07:04

Jakub Holovsky


People also ask

What is binding redirection?

1 or a later version, the app uses automatic binding redirection. This means that if two components reference different versions of the same strong-named assembly, the runtime automatically adds a binding redirection to the newer version of the assembly in the output app configuration (app.

Why does NuGet add app config?

The reason that the NuGet package manager adds assembly binding redirects to library projects is because there are types of projects where the output type is a library, but there are special mechanisms in place to assure that the library's app or web config file will be applied at runtime.


2 Answers

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.
like image 83
Jakub Holovsky Avatar answered Oct 05 '22 10:10

Jakub Holovsky


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.

like image 25
NitinSingh Avatar answered Oct 05 '22 11:10

NitinSingh