Asp core wrong assembly redirect on publish

2019-04-21 08:52发布

问题:

When publishing my asp core project using visual studio a .config file is created alongside my executable.

The .config includes a couple of bindingRedirect like this:

<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <runtime>
    <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
      <dependentAssembly>
        <assemblyIdentity name="Newtonsoft.Json" publicKeyToken="30ad4fe6b2a6aeed" culture="neutral" />
        <bindingRedirect oldVersion="6.0.0.0" newVersion="8.0.0.0" />
        <bindingRedirect oldVersion="7.0.0.0" newVersion="8.0.0.0" />
        <bindingRedirect oldVersion="6.0.0.0" newVersion="9.0.0.0" />
        <bindingRedirect oldVersion="8.0.0.0" newVersion="9.0.0.0" />
        <bindingRedirect oldVersion="7.0.0.0" newVersion="9.0.0.0" />
      </dependentAssembly>
      <dependentAssembly>
        <assemblyIdentity name="Serilog" publicKeyToken="24c2f752a8e58a10" culture="neutral" />
        <bindingRedirect oldVersion="1.5.0.0" newVersion="2.0.0.0" />
      </dependentAssembly>
      <dependentAssembly>
        <assemblyIdentity name="System.Collections.Immutable" publicKeyToken="b03f5f7f11d50a3a" culture="neutral" />
        <bindingRedirect oldVersion="1.1.37.0" newVersion="1.2.0.0" />
      </dependentAssembly>
      <dependentAssembly>
        <assemblyIdentity name="System.Reflection.Metadata" publicKeyToken="b03f5f7f11d50a3a" culture="neutral" />
        <bindingRedirect oldVersion="1.2.0.0" newVersion="1.3.0.0" />
      </dependentAssembly>
    </assemblyBinding>
  </runtime>
</configuration>

Here I want to change the bindingRedirect for Newtonsoft.Json to:

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

According to this blog post: http://blog.rytmis.net/2016/03/29/asp-net-core-and-assembly-binding-redirects/ I should be able to create an App.config and specify the binding there. However, I cannot get it to work. When I add one it still produces the same .config.

Any ideas?

NOTE: I could create a .config file in my project with the same name as my executable and with the correct bindingRedirect and then edit the publishOptions in my project.json file to include it. Then I would manually have to add all the other future bindingredirect.

回答1:

You need to create a web.config transformation file.

This answer will come in handy in order to that as it is not easy to add it yourself - https://stackoverflow.com/a/16826106/1147920

You could also do it manually by creating a copy of the web.config file and renaming it to either web.release.config or web.{PublishProfile}.config (if you're using one).

To write the transform rule to edit the bindingRedirect refer to the official docs - https://docs.microsoft.com/en-us/aspnet/web-forms/overview/deployment/visual-studio-web-deployment/web-config-transformations