tags:

views:

290

answers:

2

I read that the unit of granularity for static fields in .Net are per AppDomain, not per process. Is it possible to create a process-wide singleton object?

A: 

Have you actually examined if this is definitely the only way to accomplish your goal? I cannot imagine many scenarios where this would be the only way to accomplish something, or even a good way.

Watch out for singletonitis!

Geoffrey Chetwood
+1  A: 

You must use marshalled calls to communicate information across AppDomains. So you need to create the state object in your parent AppDomain and then pass it to any children that want to use it. If you didn't have to do this, you'd be sharing memory across AppDomains, which defeats the purpose.

Within each AppDomain you could have a singleton that holds a reference to the (marshalled) reference to the actual singleton in the primary domain. So your code would still look "singleton-y", but there would be some hidden wiring behind it.

Eric