InfoLearner
InfoLearner

Reputation: 15608

C# What exactly is application domain?

I understand that an application domain forms:

an isolation boundary for security, versioning, reliability, and unloading of managed code,

but so does a process

  1. Can someone please help me understand the practical benefits of an application domain?
  2. I assumed app domain provides you a container to load one version of an assembly but recently I discovered that multiple versions of strong key assembly can be loaded in an app domain.

My concept of application domain is still not clear. And I am struggling to understand why this concept was implemented when the concept of process is present.

Thank you.

Upvotes: 4

Views: 2803

Answers (2)

TyCobb
TyCobb

Reputation: 9089

I can't tell if you are talking in general or specifically .NET's AppDomain.

I am going to assume .NET's AppDomain and why it can be really useful when you need that isolation inside of a single process.

For instance:
Say you are dealing with a library that had certain worker classes and you have no choice, but to use those workers and can't modify the code. It's your job to build a Windows Service that manages said workers and makes sure they all stay up and running and need to work in parallel.

Easy enough right? Well, you hoped. It turns out your worker library is prone to throwing exceptions, uses a static configuration, and is generally just a real PITA.

You could try to launch them in their own process, but monitor them, you'll need to implement namedpipes or try to thoughtfully parse the STDIN and STDOUT of the process.

What else can you do? Well AppDomain actually solves this. I can spawn an AppDomain for each worker, give them their own configuration, they can't screw each other up by changing static properties because they are isolated, and on top of that, if the library bombs out and I failed to catch the exception, it doesn't bother the workers in their domain. And during all of this, I can still communicate with those workers easily.

Sadly, I have had to do this before

EDIT: Started to write this as a comment response, but got too large

Individual processes can work great in many scenarios, however, there are just times where they can become a pain. I am not saying one should use an AppDomain over another process. I think it's uncommon you would need a separate process or AppDomain, but once you need it, you'll definitely know.

The main problem I see with processes in the scenario I've given above is that processes have their own downfalls that are easier to mitigate with the AppDomain.

A process can go rogue, become unresponsive, and crash or be killed at any point.

If you're managing processes, you need to keep track of the process ID and monitor the status of it. IPCs are great, but it does take time to get proper communication going back and forth as needed.

As an example let's say your process just dies. What do you do? Depending on the mechanism you chose to monitor, maybe the communication thread died, perhaps the work finished and you still show it as "processing". What do you do?

Now what happens when you have 20 processes and your management app dies. You don't have any real information, all you have is 20 "myprocess.exe" and maybe now have to start parsing the command line arguments they were started with to see which workers you actually have. Obviously with an AppDomain all 20 would have died too, but did you really gain anything with the process? You still have to code the ability to recover, however, now you have to also code all of the recovery for your processes instead of just firing the workers back up.

As with anything in programming, there's 1,000 different ways to achieve the same goal. It's up to you to decide which solution you feel is most appropriate.

Upvotes: 8

Dio Phung
Dio Phung

Reputation: 6282

Some practical benefits using app domain:

  • Multiple app domains can be run in a process. You can also stop individual app domain without stopping the entire process. This alone drastically increases the server scalability.

  • Managing app domain life cycle is done programmatically by runtime hosts (you can override it as well). For processes & threads, you have to explicitly manage their life cycle. Initialization, execution, termination, inter-process/multithread communication is complex and that's why it's easier to defer that to CLR management.

Source: https://learn.microsoft.com/en-us/dotnet/framework/app-domains/application-domains

Upvotes: 0

Related Questions