Creating AppDomain: Resolving the Error "Failed to load file or assembly"

This is just weird. Here is the code:

 AppDomain newDomain = AppDomain.CreateDomain("newDomain ", null,
            new AppDomainSetup
            {
                ApplicationBase = @"D:\myDLLFolderFullPath\"
            });

 Assembly a = newDomain.Load("myAssembly");

This causes a "Failed to load file or assembly" error.
I checked that my dll assembly is in the specified folder path and the assembly name is correct.

When I copy the file myAssembly.dll to the main folder of CurrentDomain, it works!
It behaves as if the ApplicationBase parameter for the new AppDomain had no effect and continued to point to the Current AppDomain AppBase.

Any ideas?

+3
source share
3 answers

I don’t know what you want to do for sure ...

But in order to load the DLL into AppDomain and create an instance, you can do it like this:

AppDomain. - .

 var appDomain = AppDomain.CreateDomain("A friendly name to identify your application", null, null);

:

   var assemblyName = AssemblyName.GetAssemblyName(@"C:\PathToYourApp\ConsoleApplication1.exe"));

:

var instance = (Program)appDomain.CreateInstanceAndUnwrap(assemblyName.Name, "ConsoleApplication1.Program");

. , , [Serializable] Attrbute!

[Serializable]
class Program{}

appDomain.UnhandledException AppDomain.

+1

, .NET , .

, , :

 D:\myExeFolderFullPath\main.exe
 D:\myDLLFolderFullPath\Mydll.dll

... :

 D:\myExeFolderFullPath\main.exe
 D:\myExeFolderFullPath\myDLLFolderFullPath\Mydll.dll

Update:

mygeneration. , , - , .

+1

. msdn appDomain.load(): *

.

  • This method is provided as a convenience for interaction callers who cannot invoke the static Assembly.Load method. To load assemblies in other application areas, use a method such as CreateInstanceAndUnwrap . If the version of the requested assembly is already loaded, this method returns the loaded assembly, even if another version is requested.
0
source

Source: https://habr.com/ru/post/1789973/


All Articles