自动绑定重定向

时间:2017-10-19 15:10:09

标签: c# visual-studio

运行VS 2017,最新更新。

documentation表示要启用自动绑定重定向,请修改csproj文件并在相应的<PropertyGroup>下添加以下内容:

<AutoGenerateBindingRedirects>true</AutoGenerateBindingRedirects>

历史上我的项目在app.config中有一些绑定重定向条目。我最近更新了解决方案的所有nuget包,并在提交中修改了几个绑定重定向条目以匹配新版本。

我想确保所有内容都适合QA,所以我删除了App.Config中的所有条目,认为它们将在下一个版本中重新生成(正如文档建议的那样)。

令我惊讶的是,在clean / build上的app.config中没有添加任何内容。

所以我的问题是,假设通过整合解决方案中的依赖关系我不再需要它们是否安全?

或者有什么问题,VS应该生成重定向并且失败。

5 个答案:

答案 0 :(得分:17)

查看这篇文章

https://weblog.west-wind.com/posts/2014/Nov/29/Updating-Assembly-Redirects-with-NuGet

有一条命令可以强制Nuget重新编写解决方案的配置文件中的所有程序集重定向。

只需在Package Manager控制台上尝试一下:

Sub ReplaceColorNew(OldColor As String, NewColor As String)

Dim oeff As Effect
Dim i As Integer
Dim t As Integer
Dim oSld As Slide
Dim oShp As Shape
Dim x, y As Integer
Dim oPP As Placeholders

For Each oSld In ActivePresentation.Slides
    For Each oShp In oSld.Shapes
    'groups
        If oShp.Type = msoGroup Then
            For x = 1 To oShp.GroupItems.Count
                With oShp.GroupItems(x)
                    If .Fill.ForeColor.ObjectThemeColor = Conversion.CLng(OldColor) + 5 Then
                       .Fill.ForeColor.ObjectThemeColor = Conversion.CLng(NewColor) + 5
                    End If

                    If .Line.Visible Then
                        If .Line.ForeColor.ObjectThemeColor = Conversion.CLng(OldColor) + 5 Then
                           .Line.ForeColor.ObjectThemeColor = Conversion.CLng(NewColor) + 5
                        End If
                    End If

                    If .HasTextFrame Then
                        If .TextFrame.HasText Then
                            For y = 1 To .TextFrame.TextRange.Runs.Count
                                If .TextFrame.TextRange.Runs(y).Font.Color.ObjectThemeColor = Conversion.CLng(OldColor) + 5 Then
                                   .TextFrame.TextRange.Runs(y).Font.Color.ObjectThemeColor = Conversion.CLng(NewColor) + 5
                                End If
                            Next
                        End If
                    End If
                End With
            Next
        Else
            With oShp 'other shapes
            ' Fill
                If .Fill.ForeColor.ObjectThemeColor = Conversion.CLng(OldColor) + 5 Then
                    .Fill.ForeColor.ObjectThemeColor = Conversion.CLng(NewColor) + 5
                End If

                ' Line
                If Not .Type = msoTable Then
                    If .Line.Visible = msoTrue Then
                        If .Line.ForeColor.ObjectThemeColor = Conversion.CLng(OldColor) + 5 Then
                            .Line.ForeColor.ObjectThemeColor = Conversion.CLng(NewColor) + 5
                        End If
                    End If
                End If

                ' Text
                If .HasTextFrame Then
                    If .TextFrame.HasText Then
                        For y = 1 To .TextFrame.TextRange.Runs.Count
                            If .TextFrame.TextRange.Runs(y).Font.Color.ObjectThemeColor = Conversion.CLng(OldColor) + 5 Then
                                .TextFrame.TextRange.Runs(y).Font.Color.ObjectThemeColor = Conversion.CLng(NewColor) + 5
                            End If
                        Next
                    End If
                End If
            End With
        End If
'oShp = Nothing
    Next oShp
Next oSld


End Sub

答案 1 :(得分:5)

这是因为源app.config没有被修改,只输出(在编译步骤中创建并放在Debug / Release文件夹中的应用程序配置文件)。

  

从Visual Studio 2013开始,面向.NET Framework 4.5.1的新桌面应用程序使用自动绑定重定向。这意味着如果两个组件引用同一个强名称程序集的不同版本,则运行时会自动在输出应用程序配置(app.config)文件中将绑定重定向添加到程序集的较新版本。此重定向会覆盖可能发生的装配统一。源app.config文件未被修改。

Source

源app.config中的重定向是由Nuget自己生成的。您已通过更改包来触发它。

答案 2 :(得分:1)

有一条警告会自动生成您丢失的重定向。

  1. 清理您的项目
  2. 构建项目
  3. 转到“错误列表”过滤器并激活警告过滤器
  4. 过滤主项目的警告
  5. 使用以下消息查找警告:
      

    发现相同从属程序集的不同版本之间存在冲突。在Visual Studio中,双击该警告(或选择它并按Enter)以解决冲突;否则,将以下绑定重定向添加到应用程序配置文件中的“运行时”节点

  6. 关注消息。点击它!

更新: 您必须在项目属性->应用程序->自动生成绑定重定向中激活警告

答案 3 :(得分:0)

我通过编辑我的 .csproj 文件并添加其中之一成功生成了重定向:

<Target Name="ForceGenerationOfBindingRedirects"
          AfterTargets="ResolveAssemblyReferences"
          BeforeTargets="GenerateBindingRedirects"
          Condition="'$(AutoGenerateBindingRedirects)' == 'true'">
    <PropertyGroup>
      <GenerateBindingRedirectsOutputType>true</GenerateBindingRedirectsOutputType>
    </PropertyGroup>
</Target>

<PropertyGroup>
    <AutoGenerateBindingRedirects>true</AutoGenerateBindingRedirects>
    <GenerateBindingRedirectsOutputType>true</GenerateBindingRedirectsOutputType>
</PropertyGroup>

答案 4 :(得分:0)

在使用 package.config 的解决方案中使用 VS2019 编译 dll 项目,我尝试将以下标记添加到 PropertyGroup 部分内的 csproj:

<GenerateBindingRedirectsOutputType>true</GenerateBindingRedirectsOutputType>
<AutoGenerateBindingRedirects>true</AutoGenerateBindingRedirects>
<AutoUnifyAssemblyReferences>true</AutoUnifyAssemblyReferences>

它没有任何区别。我删除了标签。我现在确信 AutoGenerateBindingRedirects 在默认情况下实际上是 true。

我使用的解决方案是这样的:

对于与自动生成的绑定冲突的显式绑定的错误/警告或“考虑重新映射”绑定的建议,只需使用绑定重定向定义中的建议版本更新配置文件:

例如,如果消息是这样的:

The explicit binding redirect on "Microsoft.Bcl.AsyncInterfaces, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51" conflicts with an autogenerated binding redirect. Consider removing it from the application configuration file or disabling autogenerated binding redirects. The build will replace it with: "< bindingRedirect oldVersion="0.0.0.0-5.0.0.0" newVersion="5.0.0.0" xmlns="urn:schemas-microsoft-com:asm.v1" />"

您需要相应地更新配置文件中的现有 bindingredirect:

<configuration>
  <runtime>
    <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
      <dependentAssembly>
        <assemblyIdentity name="Microsoft.Bcl.AsyncInterfaces" culture="neutral" publicKeyToken="cc7b13ffcd2ddd51" />
        <bindingRedirect oldVersion="0.0.0.0-5.0.0.0" newVersion="5.0.0.0" />
      </dependentAssembly>
    </assemblyBinding>
  </runtime>
</configuration>

在某些情况下,您的项目引用的项目中的最新版本已经存在包。在这种情况下,删除项目中的包引用和 bindingRedirect 意味着将通过传递引用使用最新版本,并且错误/警告将得到解决。

在解决了所有这些问题后,构建可能会给出关于包冲突的错误,而不会告诉您是哪个导致了问题。

Found conflicts between different versions of the same dependent assembly that could not be resolved. These reference conflicts are listed in the build log when log verbosity is set to detailed. C:\Program Files (x86)\MSBuild\12.0\bin\Microsoft.Common.CurrentVersion.targets

在工具>选项>项目和解决方案>构建和运行中,您可以将 MSBuild 项目输出详细程度设置为详细。然后通过在“输出”窗口中按 Ctrl+S,您可以将输出保存到文件并使用文本编辑器进行搜索。我发现那里列出了一些冲突以寻找字符串:“无法解决冲突”。然后,您可以尝试弄清楚如何处理列出的冲突。最有可能有更多绑定重定向。

最后,我的构建仍然抱怨冲突,但没有任何关于导致冲突的原因的信息,我可以在详细的构建输出中清楚地确定这些信息。

灵丹妙药是打开为bin文件夹中的dll生成的配置文件,名称类似于mydll.config。在那里,展示了我创建的所有显式绑定重定向以及一些自动生成的。我将它们复制粘贴到项目 app.config 文件中,错误就消失了。

所以,总而言之,VS默认正确识别并解决了黑白包版本冲突,无需费心将AutoGenerateBindingRedirects和其他标签添加到csproj。然后,一旦它这样做了,它就会给你一条错误消息,根本没有任何有用的信息,只会毁了你的一天。

相关问题