views:

1043

answers:

3

Hi, I have an application that loads dlls dynamically. The application and the dlls use a Functions.dll that can be a diferent version for the application an for each dll, but in execution the application and the dlls all use the same dll version (the one used by the EXE) and share the static variables...

How can i force them to use their own Functions.dll(n-version)?

-Details:

  • I tried loading the dlls by "Assembly dll = Assembly.LoadFile(" and by "Assembly dll=domaindll.Load("
  • In Functions.dll, al the methods and objects are Static
  • I use Functions.dll "statically" by referencing it throught VS in all cases not dynamically
  • The dlls and Functions.dll are developed in C# too

-Folder Estructure:

Application:

Application.EXE
Functions.dll(version 1.2)
DLLS:
    EXAMPLEDLL1:
        EXAMPLEDLL1.DLL
        Functions.dll(version 1.1)
 EXAMPLEDLL2:
        EXAMPLEDLL2.DLL
        Functions.dll(version 1.0)
 EXAMPLEDLL3:
        EXAMPLEDLL3.DLL
        Functions.dll(version 1.2)
+4  A: 

You can enforce binding to a specific version of a DLL by strong-signing it. You can also try setting "Specific Version" to true on the reference properties, but as far as I'm aware that only affects compile-time binding and a different version can be loaded at runtime if the assembly isn't strong-signed.

This should get you started: Strong-Name Signing for Managed Applications

Be aware, though, that any types declared in this dll will not be type-equivalent to the same type in a different version of the assembly. For instance, if I declare a class called Foo in Functions.dll, then an instance of Foo from version 1.0 won't be the same type as an instance of Foo from version 1.1. As far as the CLR is concerned, these are completely different types.

If all you have are static functions in the assembly and no types are defined, then you should be OK. Otherwise you need to look into a different approach.

Adam Robinson
Its a good solution...but i cant sign the Functions.dll because when i use the signed Fuctions.dll in a test (referencing it statically to a project) it gives random errors in every method of the dll. (This doesnt happen with the unsigned one) Examples: The best overloaded method match for -'Funciones.FunSAP.comboboxvaciar(SAPbouiCOM.ComboBox)' has some invalid arguments -'x.Application' does not contain a definition for 'Formx' and no extension method 'Formx' accepting a first argument of type 'x.Application' could be found (are you missing a using directive or an assembly reference?)
ase69s
Perhaps you should edit your question and post an actual code example. That may make the issue more obvious.
Adam Robinson
A: 

To be able to do that I think you'll have to load your (Example) DLLs into separate AppDomains. Making cross-AppDomain calls incurs a bit of a performance penalty, but that is kinda unavoidable in the scenario you highlight.

jerryjvl
The problem is that this is not compatible with the development/debuggin proyect (see my other question: "C# dll version conflict") so I havent tested this solution...i dont know if it would work for the especific problem (Using appdomains would be useful too for being able to unload the dlls dinamically...)If I have time to test it i will post the results for other people to know...(I found a good reference for testing it when i can.. http://www.experts-exchange.com/Programming/Languages/C_Sharp/Q_21283005.html)
ase69s
Within a single app-domain I do not think you can load multiple different versions of the same library unless perhaps by signing it so that it has a strong identity.
jerryjvl
A: 

At the end I solved it renaming the Functions.dll to match the EXAMPLEDLL that uses it....Ex: Application.EXE-->FunctionsApplication.dll EXAMPLEDLL1.dll-->FunctionsEXAMPLEDLL1.dll Thanks for the answers anyway..

Postdata: In another case where I could sign correctly the dlls i think Adam Robinson answer would be the correct one(and jerryjvl the second anwser).

ase69s