views:

168

answers:

2

I have an application that uses a Microsoft DLL (Microsoft.ComponentStudio.ComponentPlatformImplementation.dll) which is used for OS deployment and accessing the catalog files. Version 6.0.0.0 is specific to the Windows Server 2008 catalog files. The newer version 6.1.0.0 is specific to Windows Server 2008 R2 catalog files. Attempting to access a catalog file with the incorrect version results in an exception.

My application (VB.NET using VS2005) needs to be able to access either version of these catalogs - I'd be happy with two executables (one for each catalog version) but obviously I don't want to maintain two sets of source code for each.

Specifying both sets of DLLs in the project reference is not possible as the DLL names are identical. I'd rather not have to manually add and remove the DLL references each time I want to a build. As far as I know the interfaces etc are effectively identical between the two.

I've read a few articles here and elsewhere about bindingRedirect, Assembly.Load etc but none seem to be bearing fruit.

Any guidance on the best path to follow would be greatly appreciated.

Thanks.

A: 

Assembly.Load should work fine. Make sure to use the variant that expects an AssemblyName, or the string variant, but giving the full assembly name as a string (e.g. "Microsoft.ComponentStudio.ComponentPlatformImplementation, Version=6.1.0.0, Culture=findout, PublicKeyToken=findout"). Verify that this actually allows you to load both DLLs simultaneously.

Then use .GetExportedTypes() to invoke the specific API you are after.

Martin v. Löwis
Thanks for your reply Martin, but given my level of expertise its not enough to move me forward. When using Assembly.Load, do I still need to specify the DLLs in the project's references? If so how do I include the multiple versions of the same DLL without getting the usual error message?If not is there a way to specify the path to the DLLs in the Assembly.Load function? Also, does using Assembly.Load still allow me to use Intellisense with these assemblies?Thanks!
No, you don't specify the library in the references. And no, all static runtime support is gone - you can't declare variables of types from the DLL anymore, and you can't use intellisense - how could this possibly work, if different versions of the DLL have different APIs? See KB 837908 on how to load the assembly from a given path.
Martin v. Löwis
Martin, thanks for your assistance. I eventually found a solution that didn't require me to mess with Assembly.Load etc.Cheers,ShadeSeeker.
A: 

Well, I found the solution myself - I'll post it here just in case its useful to someone.

The solution required that I write a wrapper class for each version of the CPI DLLs. Each wrapper class exists in its own project so that I can add the references for each CPI DLL to the project as well as a copy of the DLLs. This way I retain the early binding, Intellisense etc.

The classes etc within the CPI assemblies are completely hidden from the rest of the projects so I have to add methods to access whatever data etc. I need from the CPI. Copies of the CPI DLLs are also placed in folders beneath the bin\Debug and bin\Release folders of the main project. I use app.config to enable the compiler to locate them and this prevents the DLLs being copied into Debug\Release and overwriting one another. Here's the relevant part of the app.config:

  <runtime>
<assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
  <dependentAssembly>
    <assemblyIdentity name="Microsoft.ComponentStudio.ComponentPlatformInterface"
                      publicKeyToken="31bf3856ad364e35"
                      culture="neutral" />
    <codeBase version="6.0.0.0"
              href="../CPIv6000Bin/Microsoft.ComponentStudio.ComponentPlatformImplementation.dll"/>
    <codeBase version="6.1.0.0"
              href="../CPIv6100Bin/Microsoft.ComponentStudio.ComponentPlatformImplementation.dll"/>
  </dependentAssembly>
</assemblyBinding>

Having multiple copies of the CPI DLLs is probably inadvisable, unnecessary and inelegant and I'll look further into this - this is just my PoC solution at this point.

Whilst there are multiple DLLs in the CPI, testing suggests that I only need to reference this primary DLL in the app.config.

Regards, ShadeSeeker.