Click here to Skip to main content
15,893,486 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
Recently, I've learned about AppDomain, But I can't understand how to use it in the real project.
Is the Default AppDomain not enough for one process?
In which case, creating a new AppDomain is more suitable?
Hope someone can solve my question. Thank you!!!!
Posted

I don't personally use AppDomains, but then - I don't interface with tasks that are liable to fail: I prefer to write ones that don't by using defensive programming techniques.

If you look at the MSDN documentation: http://msdn.microsoft.com/en-gb/library/system.appdomain.aspx[^] it's pretty clearly stated:

Application domains, which are represented by AppDomain objects, help provide isolation, unloading, and security boundaries for executing managed code.
  • Use application domains to isolate tasks that might bring down a process. If the state of the AppDomain that's executing a task becomes unstable, the AppDomain can be unloaded without affecting the process. This is important when a process must run for long periods without restarting. You can also use application domains to isolate tasks that should not share data.

  • If an assembly is loaded into the default application domain, it cannot be unloaded from memory while the process is running. However, if you open a second application domain to load and execute the assembly, the assembly is unloaded when that application domain is unloaded. Use this technique to minimize the working set of long-running processes that occasionally use large DLLs.


Covers it pretty well, I think.
 
Share this answer
 
Comments
apthskyi 11-May-14 4:23am    
Got it! The second one and the page is really useful in c# and for understanding the AppDomain! Thank you!
OriginalGriff 11-May-14 4:28am    
You're welcome!
Kornfeld Eliyahu Peter 11-May-14 4:32am    
"I don't interface with tasks that are liable to fail: I prefer to write ones that don't" - I like that! The words a the lonely hunter...
OriginalGriff 11-May-14 4:43am    
:laugh:
I was brought up before exceptions, so if you didn't check first, then whole app would crash. It's a habit: check it, make sure it's right and do something sensible (like at least log it) if it isn't, rather than rely on the exceptions system to catch it later.
I hate code like
int i = int.Parse(myTextBox.Text)
because TryParse lets you tell the user he messed up rather than failing the whole app.

But you know this already! :D
Bernhard Hiller 12-May-14 3:24am    
"interface with tasks that are liable to fail" - from my point of view that's the major reason: when we have to interface with low-quality third-party products (like hospital information systems of really big companies...).
IMO, as far as possible, let the framework manager your application domains.
 
Share this answer
 
Comments
Kornfeld Eliyahu Peter 11-May-14 4:45am    
Let me not to agree with you! Once I wrote a tip about how to host multiple WCF services under the same Windows Service. It came from a real project and it uses Application Domain to isolate the WCF services - the framework was not handle it alone and that could be a disaster...
apthskyi 11-May-14 21:03pm    
Hi Peter,
Do you create a new AppDomain for each WCF Service?
Kornfeld Eliyahu Peter 12-May-14 2:19am    
I load every dll (that represent one WCF service) into a different AppDomain, that way if one of the services crashes the whole business still runs...
apthskyi 12-May-14 20:51pm    
Nice design~

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900