Loading 2 versions of assembly at runtime. Assembly.Load using different versions of dll - ZB loads, but when the method runs, I get a method not found exception. AppDomain.Load got a file not found error; I even used the delegate to resolve assemblies. Some details regarding ZC: - some methods are public static (some are not).
I have been given 2 pre-compiled dlls:
Example of differences in API:
And I'm trying to load both into a single project to do something like this:
I've set up my project references and
app.config
to the following. I'm also manually copying the dlls into my output folder, matching the appconfig hrefsI've gotten insofar as to build successfully.
But when I try to run it, I get a
MissingMethodException
for UserData.loadData
.I've been through multiple stackoverflow posts, msdn and codeproject articles, but can't seem to get it to work.
Link 1,Link 2,Link 3,Link 4
Think I'm missing an important step but can't figure out what, and could really use some help.
[Edit1]
I've tried using the dlls separately, and they work. (Removed clutter. See version history for screenshots)
[Edit2]
I've tried Mukesh Kumar's proposal, and changed my app.config to:
But it still does not work. I'm now getting
FileNotFoundException
.[Edit3]
Ok, I'm almost certain the
<bindingRedirect>
is not correct, and should be <codeBase>
instead.I tried compiling from the CLI:
csc Program.cs /reference:v10=libs/Common.Data.1_0_0_0.dll /reference:v20=libs/Common.Data.2_0_0_0.dll
And it works fine. I was able to use both APIs at the same time:
But when I try to build it from Visual Studio, it complains about /reference, even though I've already specified the reference alias:
The extern alias 'v10' was not specified in a /reference option
I've tried modifying
<Reference />
to include/exclude <SpecificVersion>True</SpecificVersion>
with no effect.I found this post, where the solution was to remove and re-add the paths. Using that solution, Visual Studio builds fine, but I'm back to
System.MissingMethodException
.Feels like I've almost got it, but not quite. How can I get Visual Studio to build correctly?
[Edit4]
I've tried miguel's method, but still not working.
In this attempt, I've renamed the dlls back to their original names, and store them in different folders instead.
I then updated app.config to do a bindingRedirect as well as codebase.
When running it, I'm getting
MissingMethodException
.I wasn't sure what
4- Find the and put in False
meant, so I tried all combinations of <Private>
, <SpecificVersion>
, as well as setting <Reference Include>
to FQNs, but none of those combinations work.Looking at my 3rd edit, I've managed to compile and run the sample successfully (with my renamed dlls + app.config codebase hrefs) when done via the CLI.
![Assembly Assembly](/uploads/1/2/4/9/124990971/284560255.jpg)
My problem now is, how do I configure my csproj, so it builds the same.
Community♦
jayarsjayars
3 Answers
You need to use a dependentAssembly with bindingRedirect but also you need put dlls in different folder or save with a different name. With this done, you need to put the following in your app.config:
With this code should compile and run, but sometimes VS deletes or overwrite the code in the app.config when compiles it. You need to check it in config file of compilation folder. If this succeeds, you can edit the .csproj. For this you must do:
1- Unload the project affected
2- Right click on project
3- Click Edit project
4- Find the
5- Save changes and reload project
2- Right click on project
3- Click Edit project
4- Find the
<AutoGenerateBindingRedirects>
property and set it to False5- Save changes and reload project
This works for me. In my project, I'm using two versions of Automapper.
Finally, another solution is to use the
AppDomain.CurrentDomain.AssemblyResolve
build event and load the specific dll.For that, you need catch the event:
miguelmiguel
Try to put your config details like as following.
here you can provide you dll old version and new version. You can just follow this article and also this.
Hope this will help you.
Mukesh KumarMukesh Kumar
Missing a complete answer which suits my needs i decided to donate my solution with the
AssemblyResolve
way to use two dlls and load the relevant from file, using reflection to resolve the type . For this demo i created two dlls called MathFuncs
, trying to invoke their Add.add
function will resolve in two different implementations.To see the different results switch the version integer between the values 1 and 2 :To get the complete name of the namespace for the
Type.GetType
you can use powershell : Ohad BittonOhad Bitton
Not the answer you're looking for? Browse other questions tagged c#.netvisual-studio-2015versioning or ask your own question.
Do you ever get these kinds of messages when you compile your project?
The problem is that the build output doesn’t tell me which of my assemblies references version 2.0.0.0 of System.Web.Mvc and which references version 3.0.0.0.
If you’re writing software using lots of 3rd party assemblies like I do, it’s a constant problem. I’ve written a little bit of code that I drag around with me that outputs lists of assemblies that my assemblies reference. I’ve found it very useful for resolving these kinds of issues.
Now I’ve wrapped it up as a little console app, AsmSpy, and put it on github here:
Or you can download a zip file of the compiled tool here:
How it works:
Simply run AsmSpy giving it a path to your bin directory (the folder where your project's assemblies live). E.g:
Simply run AsmSpy giving it a path to your bin directory (the folder where your project's assemblies live). E.g:
It will output a list of all the assemblies referenced by your assemblies. You can look at the
list to determine where versioining conflicts occur.
The output looks something like this:
You can see that System.Web.Mvc is referenced by 7 assemblies in my bin folder. Some reference
version 2.0.0.0 and some version 3.0.0.0. I can now resolve any conflicts.
version 2.0.0.0 and some version 3.0.0.0. I can now resolve any conflicts.