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

前端 未结 3 1189
攒了一身酷
攒了一身酷 2021-02-19 20:31

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 vers

相关标签:
3条回答
  • 2021-02-19 20:54

    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>
    
    0 讨论(0)
  • 2021-02-19 21:08

    Expanding on the other answer from this question, here's a solution that supports incremental builds and uses absolute paths for greater flexibility:

    Add this somewhere in your solution (I named it UpdateBindingRedirect.ps1):

    param ($from, $to)
    
    $fromFileXml = [xml](Get-Content -Path $from -Raw)
    $toFileXml = [xml](Get-Content -Path $to -Raw)
    
    if ( $toFileXml.configuration.runtime.InnerXml -Ne $fromFileXml.configuration.runtime.InnerXml ) {
        $toFileXml.configuration.runtime.InnerXml = $fromFileXml.configuration.runtime.InnerXml
        $toFileXml.Save($to)
    }
    

    Add this to your csproj:

      <Target Name="UpdateBindingRedirects" AfterTargets="Build" Inputs="$(OutDir)$(AssemblyName).dll.config" Outputs="$(ProjectDir)Web.config">
        <Message Text="Update binding redirects from $(ProjectDir)$(OutDir)$(AssemblyName).dll.config" />
        <Exec Command="PowerShell -NoLogo -NoProfile -File ..\UpdateBindingRedirects.ps1 -from $(ProjectDir)$(OutDir)$(AssemblyName).dll.config -to $(ProjectDir)Web.config" />
      </Target>
    
    0 讨论(0)
  • 2021-02-19 21:12

    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>
    
    0 讨论(0)
提交回复
热议问题