When will I use AppDomain?

I'm new to thinking, and I was wondering why I will use the (second) AppDomain? What practical application would be in a business application?

+7
appdomain
source share
3 answers

There are many uses. The secondary AppDomain can provide a degree of isolation that is similar to that provided by the OS.

One practical application for which I used it is to dynamically load "plug-in" DLL files. I would like to support scanning the directory for the DLL when launching the main executable file, loading and checking their types to see if any particular interface has been implemented (i.e. the Plug-in Contract). Without creating a secondary AppDomain, you have no way to unload a DLL / assembly, which may not have any types that implement the interface. Instead of transferring additional assemblies and types, etc. In your process, you can create an additional AppDomain, load the assembly there, and then examine the types. When you are done, you can get rid of the secondary AppDomain and therefore your types.

+9
source share

In 99% of cases, I would avoid additional AppDomains. These are, in fact, separate processes. You must march data from one domain to another, which adds complexity and performance problems.

People tried to use AppDomains to get around the problem that you cannot unload assemblies after they are loaded into AppDomain. This way you create a second AppDomain where you can load your dynamic assemblies and then unload the full AppDomain to free up memory associated with the assemblies.

If you don’t need to dynamically load and unload assemblies, you don’t really have to worry about them.

+2
source share

AppDomains are useful when you need to have multiple instances of a singleton. For example, you have an assembly that implements a communication protocol on some device, and this assembly uses single points. If you want to instantiate multiple instances of this class (to talk to multiple devices), and you want the instances to not interfere with each other, then AppDomains are ideal for this purpose.

However, this makes programming difficult because you need to work harder to communicate across AppDomain boundaries.

0
source share

All Articles