Common uses and best practices for application domains in .NET?
Asked Answered
K

3

6

What are some guidelines and best practices for when to create new application domains within an application?

Also, what are some common uses and examples of how multiple application domains are used whithin an application?

Kaleena answered 28/10, 2009 at 18:36 Comment(0)
U
7

The most common scenario I've seen is to be able to provide extensibility with a different security model than the main program.

Loading a plugin in a separate AppDomain allows two things:

  1. You can implement a more restricted security model
  2. You can prevent the plugin from tearing down your application if it's buggy

Another nice use of AppDomains are to load and inspect an assembly. Doing this in a separate AppDomain allows you to glean information (or run code) from a separate assembly, then unload the entire assembly from your process's space. If you load the assembly directly, there is no way to unload it. This is also useful if you want to be able to, at runtime, "upgrade" a type to a new version (ie: load a remote assembly, and reload it later).

Uninterrupted answered 28/10, 2009 at 18:41 Comment(5)
I am quite sure loading a buggy plug-in into a separate application domain cannot prevent the plug-in from crashing your application - an unhandled exception will bring the application domain down and in consequence terminate the whole process.Surfing
It depends on how you do things. It is possible to setup an appdomain so tearing it down does not tear down the process. See first listed benefit on msdn.microsoft.com/en-us/library/2bh4z9hs.aspxUninterrupted
I am not talking about controlled termination of an application domain - but there is no way to prevent process termination if any thread causes an unhandled exception. (It would be great if one could proof me wrong, but I investigated the possibilities of isolating parts of an application against each other quite in depth for a current project in order to improve the reliability of the server in case of failures in subprocesses, hence I really doubt I missed a solution.)Surfing
Daniel: It works, and I've done it. From that link "Faults in one application cannot affect other applications" - if you use unsafe or native code, though, that goes out the window, but with 100% managed code, you can provide some safety via AppDomains.Uninterrupted
It just states faults, not unhandled exceptions. Maybe I should create I question asking for an example...Surfing
N
5

It is recommended to create new domain when you need to host 3-rd party components within your application that are unreliable or you do not trust them (like plug-ins) or you want to be able to unload them.

Northeast answered 28/10, 2009 at 18:41 Comment(0)
B
3

A typical example is for plugin-/addin-like cases. Not only does it allow you to unload the DLL if required, it also gives you better security control over what the plugin is allowed to do.

Also, if you create temporary assemblies (code generation) which you want to unload again, this is a good way to do it. (LCG only allows implementing single methods, if you want to implement a complete class you need to emit to a "real" assembly).

Bissell answered 28/10, 2009 at 18:40 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.