3

As a continuation of my previous question.

I load DLL through this code. Example 1:

var assembly = Assembly.LoadFile("C:\\Temp\\PROCESSOR\\SKM.dll");

And that's work fine.

But I use serialization that internally use 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". - that's because the DLL in a separate folder.

How to force CLR to see DLL in separate directory (not in subfolders of main application)?

I tried this:

  1. <codeBase version="1.0.0.0" href="C:\\Temp\\PROCESSOR\\SKM.dll"/> - do not work because it works only for subfolders.
  2. <probing privatePath="paths"/> - do not work because it works only for subfolders.
  3. First run first example, and then run second example. But even if the SKM.dll already loaded, CLR does not see my assembly.
Community
  • 1
  • 1
Stas BZ
  • 1,184
  • 1
  • 17
  • 36
  • 1
    Side note but you're getting the FullPath() of a full path. – H H Jun 05 '15 at 07:56
  • @HenkHolterman thanks, I fixed it )) – Stas BZ Jun 05 '15 at 07:58
  • 1
    Maybe catching AppDomain.AssemblyResolve and calling Assembly.LoadFrom with the fullPath from there is an option. – Ralf Jun 05 '15 at 07:59
  • 1
    This may be helpful: http://stackoverflow.com/questions/1373100/how-to-add-folder-to-assembly-search-path-at-runtime-in-net – Nazmul Jun 05 '15 at 08:00
  • 1
    The correct way to mark your question as "solved" is not to change the title, but to post the answer to your question (which you've done) and mark that answer as the accepted one (which you have not done). – Peter Duniho Dec 02 '15 at 06:30

2 Answers2

3

I found resolution here.

Just adding an 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 DLL can not be found standard way, the event fired and load DLL from my folder.

Community
  • 1
  • 1
Stas BZ
  • 1,184
  • 1
  • 17
  • 36
0

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 then one option would be to register the DLL in the GAC.

https://msdn.microsoft.com/en-us/library/dkkx7f79%28v=vs.110%29.aspx

Justin Harvey
  • 14,446
  • 2
  • 27
  • 30