How to do dll bindingRedirect in a Vsix extension?
Asked Answered
A

3

12

I have an extension to VS that should use Gmail api to send mails to certain users in my company. During development I step into a common issue with System.Net.Http.Primitives version that is somehow messed up in Google API.

The common solution for this is to put bindingRedirect in app.config to redirectall calls to a new up-to-date version of the library. Like below:

<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>

However, this seems not to work in case when my output is a Vsix package. Generated Vsix doesn't even have an app.config.

I'm aware of a solution that says to add bindingRedirect to machine.config file but my extensions is used by some other people and I would rather not force them to put stuff into their machine configuration files.

Is there another solution for this?

Auden answered 6/7, 2015 at 12:37 Comment(0)
U
4

Technically, the app.config belongs to the process (.exe), not to the dlls. For Visual Studio, it is the devenv.exe.config file located at C:\Program Files (x86)\Microsoft Visual Studio <version>\Common7\IDE.

But to modify that file your extension should be installed with admin rights (that is, .msi or similar installer technology). And I don't think it's a good idea to modify that file since it would affect other extensions.

One approach that you can try is to redirect binding by code somehow forcing an assembly resolution failure, subscribing to the AppDomain.AssemblyResolveEvent, to get a chance of providing the exact assembly that you want. See: http://blog.slaks.net/2013-12-25/redirecting-assembly-loads-at-runtime/

Unnecessary answered 6/7, 2015 at 14:10 Comment(0)
A
27

This was answered over a year ago, but I found a better way to do it by using ProvideBindingRedirectionAttribute. This will add the binding redirects to devenv, and also determine the correct version. Details can be found here, but the relevant part 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")]

Adulteration answered 18/10, 2016 at 22:13 Comment(2)
Unfortunately, this won't work if OldVersionUpperBound > NewVersion, which was something you could do in the app.config file.Jodiejodo
If someone needs OldVersionUpperBound > NewVersion you can just open VSIX (which is a zip file) and modify the PKGDEF file. It's ugly but it's easier than manually adding the whole bindingRedirection block to PKGDEF.Lowland
U
4

Technically, the app.config belongs to the process (.exe), not to the dlls. For Visual Studio, it is the devenv.exe.config file located at C:\Program Files (x86)\Microsoft Visual Studio <version>\Common7\IDE.

But to modify that file your extension should be installed with admin rights (that is, .msi or similar installer technology). And I don't think it's a good idea to modify that file since it would affect other extensions.

One approach that you can try is to redirect binding by code somehow forcing an assembly resolution failure, subscribing to the AppDomain.AssemblyResolveEvent, to get a chance of providing the exact assembly that you want. See: http://blog.slaks.net/2013-12-25/redirecting-assembly-loads-at-runtime/

Unnecessary answered 6/7, 2015 at 14:10 Comment(0)
H
1

Nice info, this ProvideBindingRedirection. It however affects the Visual Studio configuration, not just the VSIX. In particular our VSIX requires redirects for NuGet assemblies, causing the package restore support in Visual Studio to fail...

Hurlyburly answered 23/10, 2019 at 15:41 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.