I keep getting asked about AppDomains in interviews, and I know the basics:
I still don't get what makes them necessary. I'm looking for a reasonable concrete circumstance when you would use one.
Answers:
Anything else?
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.
AppDomains are created by the . Net runtime when a managed application is initialised. When you start ABC. EXE, it gets an application domain.
Application domains provide a unit of isolation for the common language runtime. They are created and run inside a process. Application domains are usually created by a runtime host, which is an application responsible for loading the runtime into a process and executing user code within an application domain.
Probably the most common one is to load assemblies that contain plug-in code from untrusted parties. The code runs in its own AppDomain, isolating the application.
Also, it's not possible to unload a particular assembly, but you can unload AppDomains.
For the full rundown, Chris Brumme had a massive blog entry on this:
http://blogs.msdn.com/cbrumme/archive/2003/06/01/51466.aspx
https://devblogs.microsoft.com/cbrumme/appdomains-application-domains/
Another benefit of AppDomains (as you mentioned in your question) is code that you load into it can run with different security permissions. For example, I wrote an app that dynamically loaded DLLs. I was an instructor and these were student DLLs I was loading. I didn't want some disgruntled student to wipe out my hard drive or corrupt my registry, so I loaded the code from their DLLs into a separate AppDomain that didn't have file IO permissions or registry editing permissions or even permissions to display new windows (it actually only had execute permissions).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With