locked
attempted to access an unloaded appdomain When reading from Active Directory RRS feed

  • Question

  • User1057623887 posted

    I'm reading from Active Directory and about every 3rd try I get an error that says "Attempted to access an unloaded appdomain". The only way to resolve this that I've found is to restart the website in IIS. This won't be real useful in production. Does anyone have any idea what to do about this? This is the code I'm using to access AD:

    ArrayList results = new ArrayList();
    
                PrincipalContext yourDomain = new PrincipalContext(ContextType.Domain);
                using (UserPrincipal user = UserPrincipal.FindByIdentity(yourDomain, userName))
                {
                    if (user != null)
                    {
                        PrincipalSearchResult<Principal> groups = user.GetAuthorizationGroups();
    
                        foreach (Principal principal in groups)
                        {
                            if (principal is GroupPrincipal)
                            {
                                results.Add(principal.SamAccountName);
                            }
                        }
                    }
                }

    Thursday, May 17, 2012 9:40 AM

Answers

  • User-1408249245 posted

    Hi,

    I believe this happens because of the Directory Change Notification feature in IIS/ASP/.Net

    ASP monitors the main application folder and if it determines that something in that folder has changed (ie, maybe someone has touched the web.config file), ASP will force a refresh of the AppDomain and will flush it from memory. When your app goes to access it, it's now unloaded so you'll see the exception. We learned about this through an MSDN developer subscription call to MS support.

    Here's a link to the MS KB and it also talks about how it can be addressed.

    http://support.microsoft.com/kb/911272

    Hope that helps!

    Michael

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, June 28, 2012 11:02 AM

All replies

  • User-1408249245 posted

    Hi,

    I believe this happens because of the Directory Change Notification feature in IIS/ASP/.Net

    ASP monitors the main application folder and if it determines that something in that folder has changed (ie, maybe someone has touched the web.config file), ASP will force a refresh of the AppDomain and will flush it from memory. When your app goes to access it, it's now unloaded so you'll see the exception. We learned about this through an MSDN developer subscription call to MS support.

    Here's a link to the MS KB and it also talks about how it can be addressed.

    http://support.microsoft.com/kb/911272

    Hope that helps!

    Michael

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, June 28, 2012 11:02 AM
  • User1057623887 posted

    Hi,

    I believe this happens because of the Directory Change Notification feature in IIS/ASP/.Net

    ASP monitors the main application folder and if it determines that something in that folder has changed (ie, maybe someone has touched the web.config file), ASP will force a refresh of the AppDomain and will flush it from memory. When your app goes to access it, it's now unloaded so you'll see the exception. We learned about this through an MSDN developer subscription call to MS support.

    Here's a link to the MS KB and it also talks about how it can be addressed.

    http://support.microsoft.com/kb/911272

    Hope that helps!

    Michael

    That would make sense, and also make sense why it usually only happened when running through the debugger. Sometimes I saw it happen on a TEST or PROD server, but I could have made a change to the web.config like you said so that would have made sense.

    Thanks for the information.

    Tuesday, July 31, 2012 5:08 PM