【问题标题】:How to do dll bindingRedirect in a Vsix extension?如何在 Vsix 扩展中执行 dll bindingRedirect?
【发布时间】:2015-09-23 14:42:14
【问题描述】:

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

common solution 用于将 bindingRedirect 放在 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 文件,但我的扩展名被其他人使用,我不想强​​迫他们将内容放入他们的机器配置文件中。

还有其他解决方案吗?

【问题讨论】:

    标签: google-api app-config vsix


    【解决方案1】:

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

    【讨论】:

      【解决方案2】:

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

      通过使用 ProvideBindingRedirection 属性,您可以为可扩展组件的升级安装指定绑定重定向。当您发布可扩展的 Visual Studio 组件时,此属性可防止组件的用户必须安装旧版本的依赖组件。如果使用 ProvideBindingRedirection 属性,则无需手动更新 exe.config 文件即可将旧程序集版本的用户重定向到新版本。 添加 ProvideBindingRedirection 程序集属性是将绑定重定向条目添加到 pkgdef 文件的一种简单方法。 pkgdef 文件用于安装扩展。

      以下示例显示了 AssemblyInfo.cs 或 AssemblyInfo.vb 文件中的 ProvideBindingRedirection 条目:

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

      【讨论】:

      • 很遗憾,如果 OldVersionUpperBound > NewVersion 将无法正常工作,您可以在 app.config 文件中执行此操作。
      【解决方案3】:

      从技术上讲,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/

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2022-06-17
        相关资源
        最近更新 更多