Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

AppDomain Creation : Resolving "Could not load file or assembly" error

Tags:

c#

.net

This is just weird. Here's the code :

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

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

This throws the "Could not load file or assembly" error.
I checked my Assembly's dll is located under the specified folder path, and the name of the Assembly is correct.

When I copy myAssembly.dll into the CurrentDomain's main folder, it works !
It behaves as if the ApplicationBase setting for the new AppDomain has absolutely no effect and keep pointing to the Current AppDomain's AppBase.

Any Ideas ?

like image 204
Mehdi LAMRANI Avatar asked Oct 19 '25 14:10

Mehdi LAMRANI


2 Answers

I am not sure if .NET allows loading assemblies from paths that are not relative to the main executable path.

So, I think that this is not allowed:

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

...while this should work:

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

Update:

I had a similar problem with paths when working on the mygeneration project. The only way I found to get it running was to reorganize the folder structure I described in this answer.

like image 146
k3b Avatar answered Oct 21 '25 02:10

k3b


See msdn for appDomain.load() method: *

This method should be used only to load an assembly into the current application domain.

  • This method is provided as a convenience for interoperability callers who cannot call the static Assembly.Load method. To load assemblies into other application domains, use a method such as CreateInstanceAndUnwrap. If a version of the requested assembly is already loaded, this method returns the loaded assembly, even if a different version is requested.
like image 31
hannes neukermans Avatar answered Oct 21 '25 03:10

hannes neukermans



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!