Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Code example of multiple .NET AppDomains

Tags:

.net

appdomain

From What is a .NET Application Domain?:

You can run several application domains in a single process with the same level of isolation that would exist in separate processes, but without incurring the additional overhead of making cross-process calls or switching between processes.

I would like to understand more about how/why one would actually use multiple AppDomains in their application. Can anyone provide an example with actual code snippets?

like image 471
Justin Ethier Avatar asked Mar 03 '10 03:03

Justin Ethier


2 Answers

Reading the MSDN actually provides some good information.

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

--Dan

like image 130
Dan Avatar answered Sep 23 '22 00:09

Dan


I have used this in the following context (don't have the code handy with me right now to post)

  • Create a new AppDomain (e.g. appDomainX)
  • Create a new instance of an object using this new domain
  • The new object (lived in the new object) loads a bunch of assemblies
  • Reflect on them to collect some metrics
  • Get the result produced
  • Unload appDomainX

The benefit of this is that you can unload assemblies loaded into the newly created AppDomain. If you are doing this on your main AppDomain over and over again loading more assemblies, your AppDomain will grow monstrously. Creating a separate AppDomain allows you to unload after each inspection which in turn unload all the assemblies loaded to that domain hence the main AppDomain remains clean.

like image 38
Fadrian Sudaman Avatar answered Sep 19 '22 00:09

Fadrian Sudaman