Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Compile same code against two different assembly references?

I have some code which depends on a reference to part of a vendor product which is a COM application, using .NET automatic COM wrapping (I just added a reference using Visual Studio's Add Reference dialogue, and choose the registered COM component from the list). My code targets .NET Framework 4.0.

The latest version of the vendor product has deprecated the COM GUI application, but now ships with a COM interop DLL which implements the same APIs as the original COM application. However, the new COM interop DLL was compiled using .NET Framework 4.5.

Since the APIs are the same, I don't want to maintain two separate code bases for compatibility with both older and newer versions of the vendor product.

How can I set up my Visual Studio project so that I can have one code base which works with both the original VendorApplication COM component (preferably on .NET 4.0 if that's feasible) and the new VendorApplication.Interop.dll (on .NET 4.5)?

like image 230
Hydrargyrum Avatar asked Jun 30 '26 15:06

Hydrargyrum


1 Answers

How can I set up my Visual Studio project so that I can have one code base which works with both the original VendorApplication COM component (preferably on .NET 4.0 if that's feasible) and the new VendorApplication.Interop.dll (on .NET 4.5)?

One way of doing this is to create two versions of the same "thunk" library, exposing identical API. Each versions should have its own strong name.

  • The first version (say, MyInterfaceAssembly.dllversioned as v4.0.0.1) should target .NET 4.0 and use the old vendor library.

  • The second version (MyInterfaceAssembly.dll v4.5.0.1) should target .NET 4.5 and use the new vendor library.

  • You application is build to reference and link with the first version (v4.0.x).

  • The installer will install either v4.0.x or v4.5.x, depending on the end user system.

  • The most important part is app.config file, where you'd use <bindingRedirect>:

<configuration>
  <runtime>
    <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
      <dependentAssembly>
        <assemblyIdentity name="MyCompany.MyInterfaceAssembly" publicKeyToken="xxxxxxxxxxxxxxxx" culture="neutral" />
        <bindingRedirect oldVersion="0.0.0.0-4.0.0.1" newVersion="4.5.0.1" />
      </dependentAssembly>
    </assemblyBinding>
  </runtime>
</configuration>

On .NET 4.5 your app will run using MyInterfaceAssembly.dll v4.5.x, provided v4.0.x has not been installed.

like image 117
noseratio Avatar answered Jul 02 '26 05:07

noseratio



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!