`Assembly.Load` in a separate folder
As a follow-up to my previous question .
I am loading a DLL via this code. Example 1:
var assembly = Assembly.LoadFile("C:\\Temp\\PROCESSOR\\SKM.dll");
And it works great.
But I am using serialization which internally uses this code, example 2:
var ass1 = Assembly.Load("SKM, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null");
And this code throws an exception: System.Runtime.Serialization.SerializationException: Unable to find assembly "SKM, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null".
- this is because the DLL is in a separate folder.
How do I make the CLR see the DLL in a separate directory (not in the subfolders of the main application)?
I've tried this:
-
<codeBase version="1.0.0.0" href="C:\\Temp\\PROCESSOR\\SKM.dll"/>
- doesn't work because it only works for subfolders. -
<probing privatePath="paths"/>
- doesn't work because it only works for subfolders. - Run the first example first and then run the second example. But even if it's
SKM.dll
already loaded, the CLR doesn't see my assembly.
source to share
I found a solution here .
By simply adding the event to AssemblyResolve
:
AppDomain.CurrentDomain.AssemblyResolve += (sender, args) =>
{
string fileName = new AssemblyName(args.Name).Name + ".dll";
string assemblyPath = Path.Combine("C:\\Temp\\PROCESSOR", fileName);
var assembly = Assembly.LoadFile(assemblyPath);
return assembly;
};
And if the DLL cannot be found in a standard way, the event fires and loads the DLL from my folder.
source to share
Is there any particular reason you don't want to go with example 1 if you know where the DLL is?
If you really don't, one option would be to register the DLL with the GAC.
https://msdn.microsoft.com/en-us/library/dkkx7f79%28v=vs.110%29.aspx
source to share