6

Can I reference two versions of a dll in the same project without putting them in the GAC?

Thanks

arkina
  • 991
  • 2
  • 11
  • 17

3 Answers3

7

You've got two problems. First one is getting your app to compile. You've got two assembly references that contain types with the same namespace name and type name, the compiler won't know which one to choose. You solve that by using "extern alias", it lets you rename the namespace of one of the assemblies. Review this question for further help.

The second problem is the one you ask about. Without the GAC, you need to help the CLR finding the correct DLL. You must put the DLLs in a separate directory, say a subdirectory of your build directory, so that the CLR cannot find them. Use a post build event to create this directory and copy the DLLs into them. Give them distinct names.

Then implement the AppDomain.CurrentDomain.AssemblyResolve event. The CLR will fire it when it cannot find the DLLs you've hidden. Use Assembly.LoadFrom() to load and return the assembly it asks for. The e.Name property has the full assembly name, use the AssemblyName class to parse that string and retrieve the Version property.

Community
  • 1
  • 1
Hans Passant
  • 922,412
  • 146
  • 1,693
  • 2,536
0

Referring to an assembly without putting it into GAC means copying the dll in the bin folder of the project. And you cannot have two dlls of same name in the bin folder.

Mohammad Nadeem
  • 9,134
  • 14
  • 56
  • 82
-3

You cannot put reference of the same assembly twice.

Anuraj
  • 18,859
  • 7
  • 53
  • 79
  • Your answer is incorrect. This *can* be done using the GAC, but it's ill advised. – FreeAsInBeer Apr 06 '11 at 13:09
  • I don't think we can add reference of same assembly, even if it is in GAC. – Anuraj Apr 06 '11 at 13:19
  • No you cannot do this. Because if you try to do this by selecting multiple versions it will display an error. Assembly already referenced in the project. – Anuraj Apr 06 '11 at 13:24
  • 2
    Even though you should avoid loading multiple versions of the same assembly whenever possible, it's very much doable: [Link](http://geekswithblogs.net/narent/archive/2008/11/11/126940.aspx), [Link](http://dotnet.dzone.com/news/dll-heaven-multiple-versions-o), [Link](http://www.infosysblogs.com/microsoft/2007/04/loading_multiple_versions_of_s.html) – FreeAsInBeer Apr 06 '11 at 13:49