none
Issue with AppDomain.Load RRS feed

  • Question

  • Hello All

    MSDN documentation for this method says

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

    If the current AppDomain object represents application domain A, and the Load method is called from application domain B, the assembly is loaded into both application domains. For example, the following code loads MyAssembly into the new application domain ChildDomain and also into the application domain where the code executes:

    The assembly is loaded into both domains because Assembly does not derive from MarshalByRefObject, and therefore the return value of the Load method cannot be marshaled. Instead, the common language runtime tries to load the assembly into the calling application domain. The assemblies that are loaded into the two application domains might be different if the path settings for the two application domains are different.

     

    Now is there any alternative as I don't want to load the assembly in calling domain?

     

     

    Friday, January 23, 2009 6:40 AM

Answers

  • Hi,

     

    This happens because Assembly type is not MarshalByRefObject.

    If you want to load assembly into new application domain and prevent loading it into the current one, define a new type in the new assembly, deriving from MarshalByRefObject. Then use CreateInstance to create an instance of that type in your application.


    Vitaliy Liptchinsky http://dotnetframeworkplanet.blogspot.com/
    Friday, January 23, 2009 9:06 AM
  • You can't create any types from that assembly.  You'll need to write a helper assembly that defines the interface between the ADs and has a class that implements that interface.  Create an instance of that class with CreateInstance().  Getting your GUI assembly loaded is now automatic.
    Hans Passant.
    Friday, January 23, 2009 10:44 AM
    Moderator

All replies

  • It is possible to load the assembly into an alternative application domain and thus be able to unload it again. It depends what you want to do, and it is a bit hard to understand that from what you've written.

    It's a good while since I had a look at it, so I don't quite remember the details. But as soon as you try to move any information between the appdomains that is defined in the loaded assembly, it will be loaded in your "main" application domain as well (and thus you'll be unable to unload it during your applications lifetime). If you load the assembly in the separate appdomain, do some work, and return the results of that work in the likes of a string for instance, you can do it without loading the assembly in your main appdomain.

    Friday, January 23, 2009 9:05 AM
  • Hi,

     

    This happens because Assembly type is not MarshalByRefObject.

    If you want to load assembly into new application domain and prevent loading it into the current one, define a new type in the new assembly, deriving from MarshalByRefObject. Then use CreateInstance to create an instance of that type in your application.


    Vitaliy Liptchinsky http://dotnetframeworkplanet.blogspot.com/
    Friday, January 23, 2009 9:06 AM
  • I don't have the luxury of changing assembly , its a third party GUI assembly
    Friday, January 23, 2009 9:41 AM
  • You can't create any types from that assembly.  You'll need to write a helper assembly that defines the interface between the ADs and has a class that implements that interface.  Create an instance of that class with CreateInstance().  Getting your GUI assembly loaded is now automatic.
    Hans Passant.
    Friday, January 23, 2009 10:44 AM
    Moderator
  • "Getting your GUI assembly loaded is now automatic."

    Can you please explain?

     

    Friday, January 23, 2009 11:09 AM
  • The class you create will use types from that GUI assembly.  The CLR automatically loads the assembly.
    Hans Passant.
    Friday, January 23, 2009 12:00 PM
    Moderator
  • Hi Vitaliy Liptchinsky

    After I use CreateInstance , I would need to UnWrap it to use it which will again load the assembly in calling domain.

    Any solution??

     

    Wednesday, January 28, 2009 6:55 AM
  • You didn't get me , my problem is that when I load a assembly in target domain , it should not get loaded in calling domain
    Wednesday, January 28, 2009 6:56 AM
  • You always need an assembly that gets loaded into both.  If you don't, the assembly essentially becomes unusable, you can't make any method calls on the type you created.  You could only call the constructor, nothing else.  The key is to make the assembly that gets loaded in both a simple one.  It should only contain an interface declaration.  The type you create with CreateInstance must implement that interface so you can then make the method calls.
    Hans Passant.
    Wednesday, January 28, 2009 12:34 PM
    Moderator
  • I used CreateInstance and it just loads the assembly in target domain!

    The mistake I was doing was that I used abstract class instead of interface which caused the assembly loading.

     

     

    Wednesday, January 28, 2009 12:52 PM