【发布时间】: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