views:

97

answers:

4

I have an application that needs to work with two versions of a dll. I figured I would just compile the app once, copy the exe to two directories, and then copy the two versions of the dlls to each of these directories. FYI, I'm not doing anything with the GAC. However, I'm getting a FileLoadException on the version with the dll it was not compiled with.

In the project, I have the "Specific Version" set to false for the dll reference. But, maybe that is the wrong setting. How do I get this to work with multiple versions?

+1  A: 

If the assembly you are referencing is strongly signed the CLR will not load it if it has a different version than the one it was compiled against. You could in this case use the config file to specify the version you would like to use:

<runtime>
    <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
        <dependentAssembly>
            <assemblyIdentity name="YouAssemblyNameWithoutDotDll"
                              publicKeyToken="your-assembly-token"
                              culture="neutral" />
            <bindingRedirect oldVersion="1.0.0.0"
                             newVersion="2.0.0.0" />
        </dependentAssembly>
    </assemblyBinding>
</runtime>

Of course this assumes that the new version of the assembly has the same method signatures or you will get a MissingMethodException at runtime.

Darin Dimitrov
A: 

This is because every time you recompile dll, adresses of functions are changed. And since you are binding your app on the compile time with your dll, those adresses are set to be fixed in your app. Basically you must recompile your program everytime with new version of dll (or to be more precise, when you mess with datastructures) or use dynamic loading and GetProcAddress.

doc
A: 

Make sure that the two versions of the assembly have the same AssemblyVersion specified in your AssemblyInfo.cs file.

Ex:

[assembly: AssemblyVersion("1.0.0.0")]

I believe the default generated for new projects before VS 2008 is:

[assembly: AssemblyVersion("1.0.0.*")]

which will automatically increment the assembly version every time it is built.

This is particularly important for assemblies that are signed since an assembly's strong name includes its version.

Michael Petito
A: 

I ruled against changing the config file, simply because it's another thing I would need to do. In regards to the address changing for different versions of the dll, I don't think I have that problem since I am actually using an interface to call my methods.

To give a little more detail, I simply need to create an instance of one type in multiple dlls. From there, I treat it as an interface object. So, here is my solution:

Assembly asm = Assembly.LoadFrom("XXX.YYY.ZZZ.Bin.dll");
Type type = asm.GetType("MyType");
IMyType obj = Activator.CreateInstance(type) as IMyType;

This seems to work.

bsh152s