Sample Code for Multiple .NET AppDomains Applications

From What is a .NET Application Domain? :

You can run multiple application domains in the same process with the same isolation level that would exist in separate processes, but without additional overhead when making interprocess calls or switching between processes.

I would like to learn more about how / why it is actually possible to use several AppDomains applications in my application. Can someone provide an example with actual code snippets?

+7
appdomain
source share
4 answers

Reading MSDN actually provides some good information.

http://msdn.microsoft.com/en-us/library/system.appdomain.aspx

- Dan

+5
source share

I used this in the following context (no need to have code with me right now to publish)

  • Create a new AppDomain (e.g. appDomainX)
  • Create a new instance of the object using this new domain
  • A new object (living in a new object) loads a bunch of assemblies
  • Reflect them to collect some indicators
  • Get the result
  • Download appDomainX

The advantage of this is that you can unload assemblies loaded into the newly created AppDomain. If you do this on your main AppDomain again and again, loading more builds, your AppDomain will grow monstrously. Creating a separate AppDomain allows you to unload after each check, which in turn unloads all assemblies uploaded to this domain, so the main AppDomain remains clean.

+3
source share

I worked on a part of (mainly) C ++ programs that allowed users to write scripts to automate an application using C # or VB.NET. The application also had several of its components written in C #. He used one AppDomain for program components and another for a script sandbox.

The original script implementation created an AppDomain for each script, but it turned out to be too slow, and this prevented some useful script behaviors, so we moved on to one constant AppDomain for the script mechanism.

+1
source share

You might want to use it to simulate IIS processing. You need a long process that has memory leaks. You can keep track of how many AD requests have been processed, and you have reached the threshold and start a new one. When the old one has completed all the processing, unload it and let the CLR clear part of the unwanted program.

Do not ask me how I know this. :)

You can also do this if you want to run code in different security contexts.

+1
source share

All Articles