Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to unload an assembly from the primary AppDomain?

Tags:

c#

.net

appdomain

I would like to know how to unload an assembly that is loaded into the main AppDomain.

I have the following code:

var assembly = Assembly.LoadFrom( FilePathHere ); 

I need/want to be able to unload this assembly when I am done.

Thanks for your help.

like image 650
Derik Whittaker Avatar asked Sep 23 '08 19:09

Derik Whittaker


People also ask

How do I unload an assembly in powershell?

To unload an assembly in the . NET Framework, you must unload all of the application domains that contain it. To unload an application domain, use the AppDomain. Unload method.

What is AppDomain C#?

The AppDomain class implements a set of events that enable applications to respond when an assembly is loaded, when an application domain will be unloaded, or when an unhandled exception is thrown. Advantages. A single CLR operating system process can contain multiple application domains.

What is assembly C#?

C# Assembly is a standard library developed for . NET. Common Language Runtime, CLR, MSIL, Microsoft Intermediate Language, Just In Time Compilers, JIT, Framework Class Library, FCL, Common Language Specification, CLS, Common Type System, CTS, Garbage Collector, GC.


2 Answers

For .net versions core 3.0 and later:

You can now unload assemblies. Note that appdomains are no longer available in .net core. Instead, you can create one or more AssemblyLoadContext, load your assemblies via that context, then unload that context. See AssemblyLoadContext, or this tutorial that simulates loading a plugin then unloading it.

For .net versions before .net core 3, including netframework 4 and lower

You can not unload an assembly from an appdomain. You can destroy appdomains, but once an assembly is loaded into an appdomain, it's there for the life of the appdomain.

See Jason Zander's explanation of Why isn't there an Assembly.Unload method?

If you are using 3.5, you can use the AddIn Framework to make it easier to manage/call into different AppDomains (which you can unload, unloading all the assemblies). If you are using versions before that, you need to create a new appdomain yourself to unload it.

like image 145
Philip Rieck Avatar answered Sep 25 '22 02:09

Philip Rieck


I also know this is very old, but may help someone who is having this issue! Here is one way I have found to do it! instead of using:

var assembly = Assembly.LoadFrom( FilePathHere ); 

use this:

var assembly = Assembly.Load( File.ReadAllBytes(FilePathHere)); 

This actually loads the "Contents" of the assembly file, instead of the file itself. Which means there is NOT a file lock placed on the assembly file! So now it can be copied over, deleted or upgraded without closing your application or trying to use a separate AppDomain or Marshaling!

PROS: Very Simple to fix with a 1 Liner of code! CONS: Cannot use AppDomain, Assembly.Location or Assembly.CodeBase.

Now you just need to destroy any instances created on the assembly. For example:

assembly = null; 
like image 20
Kirk Herron Avatar answered Sep 22 '22 02:09

Kirk Herron