【问题标题】:Assembly binding redirect from a Class Library来自类库的程序集绑定重定向
【发布时间】:2018-10-07 04:19:22
【问题描述】:

我有一个 GitHub 项目 (Test Automation Essentials),它引用了一些 Visual Studio 特定的程序集(Microsoft.VisualStudio.TestTools.UITest.Extension,它是 CodedUI 的一部分;但这对于这个问题并不重要)。此项目作为 NuGet 包发布,其中包含我的类库。

我希望我的项目支持不同版本的 Visual Studio,总而言之,这个程序集在 Visual Studio 的版本之间没有任何明显的差异,所以我预计不会出现任何兼容性问题(它应该是向后兼容的反正)。

但是,如果我在 Visual Studio 的一个版本(例如 2015)中编译我的项目,当我尝试从较新版本的 Visual Studio(例如 2017)中的项目中引用 NuGet 包时,当托管项目运行时,我得到以下异常:

System.IO.FileNotFoundException: Could not load file or assembly 'Microsoft.VisualStudio.TestTools.UITest.Extension, Version=12.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a' or one of its dependencies. The system cannot find the file 

注意:我的库使用 Specific Version=False 引用此程序集。

我发现我可以通过将以下元素添加到应用程序的 app.config 来解决此问题:

  <dependentAssembly>
    <assemblyIdentity name="Microsoft.VisualStudio.TestTools.UITest.Extension" publicKeyToken="b03f5f7f11d50a3a" culture="neutral" />
    <bindingRedirect oldVersion="12.0.0.0-15.0.0.0"  newVersion="15.0.0.0"/>
  </dependentAssembly>

注意:在这种特殊情况下,可执行文件通常是 QTAgent32_40.exe,它本身就是 Visual Studio 的一部分,所以我必须将该元素添加到 QTAgent32_40.exe.config,而不是实际添加到项目的 app.config 文件中。 QTAgent32_40.exe.config 已经有许多类似的 dependentAssembly 元素,但出于某种原因,不适用于这个特定的程序集。

问题:

我不希望我的客户自己添加此设置。如果我可以为我的类库设置这样的设置,我会很高兴,这样任何引用我的库的人都会自动获得这个程序集重定向设置。但是,我没有找到一种方法来做到这一点......

有人知道我该怎么做吗?

【问题讨论】:

    标签: visual-studio .net-assembly assembly-binding-redirect assemblybinding


    【解决方案1】:

    执行此操作的唯一方法是为每个特定版本的 Visual Studio 创建一个 Nuget 包。

    【讨论】:

    • 有没有一种简单的方法可以在不为每个版本复制 csproj 的情况下做到这一点?
    • 同一个项目可以有多个构建配置。
    • 您可以使用 choose/when 构造在 msbuild 中编写选择性引用,当您使用 Visual Studio 15.6+ 时,您还可以包含选择性 packageReference 项以引用特定的 nuget 包和/或包版本。你可以在这里找到一个实现的例子github.com/tfsaggregator/tfsaggregator
    猜你喜欢
    • 1970-01-01
    • 2012-04-27
    • 2017-02-16
    • 2019-12-21
    • 1970-01-01
    • 2021-10-01
    • 2021-10-18
    • 2021-01-15
    • 1970-01-01
    相关资源
    最近更新 更多