Search code examples
c#visual-studiovisual-studio-2017assembly-binding-redirect

Why is visual studio creating app.configs for assembly redirection?


I have a solution with about 100 projects. I don't know exactly what triggers it but Visual Studio occasionally adds an app.config to each of my projects. It usually looks like this:

<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <runtime>
    <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
      <dependentAssembly>
        <assemblyIdentity name="System.Net.Http.Formatting" publicKeyToken="31bf3856ad364e35" culture="neutral" />
        <bindingRedirect oldVersion="0.0.0.0-5.2.3.0" newVersion="5.2.3.0" />
      </dependentAssembly>
      <dependentAssembly>
        <assemblyIdentity name="Newtonsoft.Json" publicKeyToken="30ad4fe6b2a6aeed" culture="neutral" />
        <bindingRedirect oldVersion="0.0.0.0-11.0.0.0" newVersion="11.0.0.0" />
      </dependentAssembly>
    </assemblyBinding>
  </runtime>
</configuration>

As far as I can tell, these app.config files are not needed. Everything appears to work fine if I revert all these changes and delete the app.config files. Why is Visual Studio doing this and how can I make it stop?


Solution

  • Usually VS adds these configs (binding redirect) when you update nuget packages. It helps to prevent some issues when .NET looking for dependencies, e.g.

      <dependentAssembly>
        <assemblyIdentity name="Newtonsoft.Json" publicKeyToken="30ad4fe6b2a6aeed" culture="neutral" />
        <bindingRedirect oldVersion="0.0.0.0-11.0.0.0" newVersion="11.0.0.0" />
      </dependentAssembly>
    

    In this config we talk that if some other dependencies of your app have references to assemblies with version before 11.0.0.0 they should use the version 11.0.0.0. In this case if you omit this binding redirect you will have an exception in runtime.

    If you don't have such dependencies in your app you can remove these configs.