Sergey Kucher
Sergey Kucher

Reputation: 4180

Trying to load dll into Application domain

I am trying to do the following:

if(domain != null)
{
    AppDomain.Unload(domain);
}

domain = AppDomain.CreateDomain(appDomainName);

Assembly assembly = domain.Load(location);

and the code throws FileLoadException

but when i do the following there is no exception :

Assembly assembly = Assembly.LoadFrom(location);

Could you please tell me what could be the problem.

Thank you.

Edited:

The reason I want to load the assembly it because i want to create instance of the class is in it using factory method of it could please suggest a solution

Upvotes: 0

Views: 1024

Answers (2)

dtb
dtb

Reputation: 217253

From Suzanne Cook's .NET CLR Notes:

AppDomain.Load() is only meant to be called on AppDomain.CurrentDomain. (It's meant for interop callers only. They need a non-static method, and Assembly.Load() is static.) If you call it on a different AppDomain, if the assembly successfully loads in the target appdomain, remoting will then try to load it in the calling appdomain, potentially causing a FileNotFoundException/SerializationException for you.

If you need to execute an exe, use AppDomain.ExecuteAssembly() or (starting in v2.0) AppDomain.ExecuteAssemblyByName() instead. Otherwise, you should change to use Assembly.Load() from within the target appdomain. See Executing Code in Another AppDomain for more info.

See also this SO question.

Upvotes: 4

Peter
Peter

Reputation: 38455

Make sure the directories are setup correctly on the appdomain use a AppDomainSetup if they arent..

Im not 100% sure but i don't think domain.Load takes a path, i think it wants the assembly name..

Edit:

Look at this page

look at Remarks:

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.

For information that is common to all overloads of this method, see the Load(AssemblyName) method overload.

Upvotes: 0

Related Questions