C# - Loading .NET Assembly into Separate AppDomain So You Can Unload It

10,145

Basically, you just create the new AppDomain, and then call AppDomain.CreateInstanceAndUnwrap to create the type. Here's a simple CodeProject article about this process.

There are some tricks, here. You can't ever refer to the Type directly (this will load the type into your current AppDomain), and the objects should all derive from MarshallByRefObj. This will allow you to use the object "remotely", meaning keep it from loading into your AppDomain.

Share:
10,145
BuddyJoe
Author by

BuddyJoe

I like to code C# and work with the web. Still learning.

Updated on June 04, 2022

Comments

  • BuddyJoe
    BuddyJoe almost 2 years

    Possible Duplicate:
    Loading DLLs into a separate AppDomain

    What is the proper way to load a .NET assembly into a separate AppDomain so you can have access to its Types/Classes but still be able to unload it (and reload it).

    This is a tangent of this previous discussion: C# - Correct Way to Load Assembly, Find Class and Call Run() Method

  • Reed Copsey
    Reed Copsey almost 15 years
    It depends. Anything passing between teh appdomains is basically serialized across. (It works very similarly to Remoting.) In general, if all of your types derive from MarshallByRefObj, they will only serialize a "handle" across, so it's quite fast. When you read the data, the data will be serialized, though, which can slow things down a bit. Try to always keep as much "internal" in the second appdomain, and it probably wont' be an issue.
  • Guillaume
    Guillaume almost 13 years
    Note also that remote instance life time is limited. Depending on .Net version, you should use WCF instead of remoting.
  • David Pfeffer
    David Pfeffer about 12 years
    @Guillaume How does that apply? Cross AppDomain is inherently done with Remoting.
  • Guillaume
    Guillaume about 12 years
    msdn.microsoft.com/library/72x4h507.aspx Remoting is deprecated. Nothing prevents you from using WCF between two AppDomain.
  • ilansch
    ilansch over 9 years
    @Guillaume remoting is not recommended for IPC Communication, it is not depcrecated for app domain communication
  • Guillaume
    Guillaume over 9 years
    @ilansch You are right, remoting is not deprecated but legacy and WCF is recommended (also for app domain). stackoverflow.com/questions/1294494/…
  • ilansch
    ilansch over 9 years
    I will add this link brad-smith.info/blog/archives/500 It was very helpful to me !