Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to do dll bindingRedirect in a Vsix extension?

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?

like image 426
Grzegorz Sławecki Avatar asked Jul 06 '15 12:07

Grzegorz Sławecki


2 Answers

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")]

like image 67
Taylor Southwick Avatar answered Oct 20 '22 21:10

Taylor Southwick


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/

like image 45
Carlos Quintero Avatar answered Oct 20 '22 22:10

Carlos Quintero