Accessing the default app domain
Asked Answered
G

1

20

I am looking for a way to find the default app domain in my process. Note than the current app domain may be different from the default one, for example when my code is running inside NUnit.

I know that I can list all the app domains in the process using a COM interop trick shown in this answer, and pick the one for which IsDefaultAppDomain() is true. However, this seems like a heavyweight solution.

Is there a way to get the default appdomain that does not require filtering all the domains, preferably without going through COM interop?

Greenish answered 11/6, 2013 at 21:27 Comment(4)
We've used the above COM Interop trick; however, I wonder if it's possible to just store the default app domain as a reference somewhere when your app starts (say in Main), and just look that up?Vinaigrette
Yes, you can create a domain-neutral assembly, and in that assembly you can store a reference to the default AppDomain (in some static property).Adi
@M.A.Hanin Would you mind illustrating this approach, or maybe add an answer that shows how to do that? Thanks!Greenish
After double checking, I found out that domain-neutral assemblies only share their code, not data (see here: #3058610 ), so my previous comment is invalid. Sorry :-(Adi
E
2

To my knowledge, the .NET framework does not support that. You would have to use the unmanaged solution which does support it.

Exeter answered 17/9, 2013 at 10:32 Comment(1)
An MSDN blog post dating back to 2007 says this: As I was showing isolation and unload-ability, I wanted to enumerate the AppDomain's in the current process. Surprisingly there is no managed API in the BCL to show AppDomains. Using Interop however, we can list the AppDomains. blogs.msdn.com/b/jackg/archive/2007/06/11/…Buddybuderus

© 2022 - 2024 — McMap. All rights reserved.