Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why doesn't AutogenerateBindingRedirects work for a Web.config in Visual Studio 2017

I have a reference to a .Net Standard 2.0 library that requires Microsoft.AspNet.WebApi.Client 5.2.4. This has a lot of dependencies that need to be redirected to use newer versions.

To avoid package/dependency explosion I've updated the first PropertyGroup in the csproj file:

<RestoreProjectStyle>PackageReference</RestoreProjectStyle>
<AutoGenerateBindingRedirects>true</AutoGenerateBindingRedirects>
<GenerateBindingRedirectsOutputType>true</GenerateBindingRedirectsOutputType>

I'm expecting AutoGenerateBindingRedirects to prevent me from needing to change the Web.config to match the versions added.

Why do I still need to add binding redirects to my Web.config to resolve assembly conflicts?

like image 284
Daniel Leach Avatar asked Feb 25 '18 03:02

Daniel Leach


People also ask

How do I enable and disable automatic binding redirection?

Right-click the project in Solution Explorer and select Properties. On the Application page, uncheck the Auto-generate binding redirects option. If you don't see the option, you'll need to manually disable the feature in the project file.

What is binding redirect in web config?

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. config) file.


2 Answers

It appears that AutoGenerateBindingRedirects will not work for web projects per https://docs.microsoft.com/en-us/dotnet/framework/configure-apps/how-to-enable-and-disable-automatic-binding-redirection.

Inspecting the output from the build shows that binding redirects are generated just not in the Web.config. Instead, they are in $(AssemblyName).dll.config. This file has the original configuration from Web.config as well as the binding redirects.

To put it all together you can have MSBuild copy the resulting config back to the Web.config. To do this you would add the following to the csproj:

<Target Name="AfterBuild">
  <Copy SourceFiles="$(TargetDir)\$(AssemblyName).dll.config" DestinationFiles="Web.config" />
</Target>
like image 77
Daniel Leach Avatar answered Sep 17 '22 15:09

Daniel Leach


For iis express: In Web.config replace section assemblyBinding with

  <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
    <linkedConfiguration href="file:{AssemblyName}.dll.config"/>
  </assemblyBinding>

For iis and iis express:

add to project Scripts\CopyRuntimeSection.ps1

param ($from, $to)
$projectPath = Resolve-Path "$($PSScriptRoot)\..\"

$fromFilePath = "$projectPath\$from";
$toFilePath = "$projectPath\$to";

$fromFileXml = [xml](Get-Content -Path $fromFilePath -Raw)
$toFileXml = [xml](Get-Content -Path $toFilePath -Raw)

$toFileXml.configuration.runtime.InnerXml = $fromFileXml.configuration.runtime.InnerXml
$toFileXml.Save($toFilePath)

add to csproj

  <Target Name="CopyRuntimeSection" AfterTargets="Build">
    <Exec Command="PowerShell -File Scripts\CopyRuntimeSection.ps1 -from $(OutDir)\$(AssemblyName).dll.config -to Web.config" />
  </Target>
like image 33
Александр Пушкин Avatar answered Sep 21 '22 15:09

Александр Пушкин