如何在 Vsix 扩展中执行 dll bindingRedirect?

How to do dll bindingRedirect in a Vsix extension?

我有一个 VS 扩展,它应该使用 Gmail api 向我公司的某些用户发送邮件。 在开发过程中,我遇到了 System.Net.Http.Primitives 版本的一个常见问题,该版本在 Google API 中不知何故搞砸了。

为此,common solutionbindingRedirect 放入 app.config 以将所有调用重定向到新的最新版本的库。如下所示:

<runtime>
  <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
    <dependentAssembly>
      <assemblyIdentity name="System.Net.Http.Primitives" publicKeyToken="b03f5f7f11d50a3a" culture="neutral"/>
      <bindingRedirect oldVersion="0.0.0.0-4.0.10.0" newVersion="4.0.10.0"/>
    </dependentAssembly>
  </assemblyBinding>
</runtime>

但是,当我的输出是 Vsix 包时,这似乎不起作用。生成的 Vsix 甚至没有 app.config.

我知道一个解决方案说要将 bindingRedirect 添加到 machine.config 文件,但我的扩展已被其他人使用,我不想强​​迫他们将东西放入他们的机器配置文件。

是否有其他解决方案?

从技术上讲,app.config 属于进程 (.exe),而不属于 dll。对于 Visual Studio,它是位于 C:\Program Files (x86)\Microsoft Visual Studio \Common7\IDE.

的 devenv.exe.config 文件

但要修改该文件,您的扩展程序应该以管理员权限安装(即 .msi 或类似的安装程序技术)。而且我认为修改该文件不是一个好主意,因为它会影响其他扩展。

您可以尝试的一种方法是通过代码重定向绑定以某种方式强制程序集解析失败,订阅 AppDomain.AssemblyResolveEvent,以获得提供您想要的确切程序集的机会。参见:http://blog.slaks.net/2013-12-25/redirecting-assembly-loads-at-runtime/

一年多以前就有人回答了这个问题,但我找到了使用 ProvideBindingRedirectionAttribute 的更好方法。这会将绑定重定向添加到 devenv,并确定正确的版本。可以找到详细信息here,但相关部分在这里:

By using the ProvideBindingRedirection attribute, you can specify binding redirection for the installation of an upgrade to an extensible component. When you ship an extensible Visual Studio component, this attribute prevents users of the component from having to install an old version of a dependent component. If you use the ProvideBindingRedirection attribute, you don't need to manually update the exe.config file to redirect users of the old assembly version to the new version. Adding a ProvideBindingRedirection assembly attribute is an easy way to add a binding redirection entry to the pkgdef file. The pkgdef file is used to install the extension.

The following example shows a ProvideBindingRedirection entry in the AssemblyInfo.cs or AssemblyInfo.vb file:

[assembly: ProvideBindingRedirection(AssemblyName = "ClassLibrary1", NewVersion = "3.0.0.0", OldVersionLowerBound = "1.0.0.0", OldVersionUpperBound = "2.0.0.0")]

很好的信息,这个 ProvideBindingRedirection。但是,它会影响 Visual Studio 配置,而不仅仅是 VSIX。特别是我们的 VSIX 需要重定向 NuGet 程序集,导致 Visual Studio 中的包恢复支持失败...