Active Directory related AppDomainUnloadedException in MVC4

1

I have an ASP.NET MVC application. It's run on a domain and we determine the user by calling UserPrincipal.Current. This works beautifully, most of the time. Every once in a while (maybe 1 out of 5 times), and only right after I publish my app to IIS, it will throw an AppDomainUnloadedException.

The specific line of my code causing the exception is:

if (UserPrincipal.Current.SamAccountName != null && 
    _currentUserId != UserPrincipal.Current.SamAccountName) ...

The remainder of the call stack is:

mscorlib.dll!System.StubHelpers.StubHelpers.GetCOMHRExceptionObject(int hr, System.IntPtr pCPCMD, object pThis) + 0xe bytes 
System.DirectoryServices.AccountManagement.dll!System.DirectoryServices.AccountManagement.ADStoreCtx.LoadDomainInfo() + 0x358 bytes 
System.DirectoryServices.AccountManagement.dll!System.DirectoryServices.AccountManagement.ADStoreCtx.DnsDomainName.get() + 0x5e bytes   
System.DirectoryServices.AccountManagement.dll!System.DirectoryServices.AccountManagement.ADStoreCtx.GetAsPrincipal(object storeObject, object discriminant = {Name = "UserPrincipal" FullName = "System.DirectoryServices.AccountManagement.UserPrincipal"}) + 0x17a bytes 
System.DirectoryServices.AccountManagement.dll!System.DirectoryServices.AccountManagement.ADStoreCtx.FindPrincipalByIdentRefHelper(System.Type principalType, string urnScheme, string urnValue, System.DateTime referenceDate, bool useSidHistory) + 0x576 bytes   
System.DirectoryServices.AccountManagement.dll!System.DirectoryServices.AccountManagement.ADStoreCtx.FindPrincipalByIdentRef(System.Type principalType, string urnScheme, string urnValue, System.DateTime referenceDate) + 0x35 bytes  
System.DirectoryServices.AccountManagement.dll!System.DirectoryServices.AccountManagement.Principal.FindByIdentityWithTypeHelper(System.DirectoryServices.AccountManagement.PrincipalContext context, System.Type principalType, System.DirectoryServices.AccountManagement.IdentityType? identityType, string identityValue, System.DateTime refDate) + 0x9e bytes 
System.DirectoryServices.AccountManagement.dll!System.DirectoryServices.AccountManagement.Principal.FindByIdentityWithType(System.DirectoryServices.AccountManagement.PrincipalContext context, System.Type principalType, System.DirectoryServices.AccountManagement.IdentityType identityType, string identityValue) + 0x5b bytes 
System.DirectoryServices.AccountManagement.dll!System.DirectoryServices.AccountManagement.UserPrincipal.FindByIdentity(System.DirectoryServices.AccountManagement.PrincipalContext context, System.DirectoryServices.AccountManagement.IdentityType identityType, string identityValue) + 0x1e bytes    
System.DirectoryServices.AccountManagement.dll!System.DirectoryServices.AccountManagement.UserPrincipal.Current.get() + 0xc1 bytes

After a great deal of digging around, and due to the random nature of the exception, I concluded the problem was probably related to this:

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

So I installed the hotfix and as expected, the problem went away. That was Tuesday. This morning, the exception started up again. I verified that the hotifx is in fact installed. I turned on module load messages and got:

'w3wp.exe': Loaded 'C:\Windows\SysWOW64\activeds.dll', Cannot find or open the PDB file.

So I verified that that .dll is in fact the one from the hotfix and it is (size and version #s match).

At the same time I get this exception, I get this event in the event log:

A process serving application pool 'WebSitePool' suffered a fatal communication error with the Windows Process Activation Service. The process id was '7404'. The data field contains the error number.

The only data in the error is: 0x8007006D which I believe simply means there was a fatal communication error which the error already said...

Humorously, when the exception dialog pops up it states, "If there is a handler for this exception, the program may be safely continued." Which is true if your app has no need for UserPrincipal.Current, but calling it again after this exception causes the exception to rethrow, so I'd question their "safely continued" assertion there.

Now, if I rerun the app, I won't get a problem. I have yet to have this happen any time, other than right after publishing. Because of our setup and relations to other projects, this app must be debugged under IIS, which means I have to publish it every time I make a code change which means I normally see this throughout the day (Tuesday afternoon and yesterday being the exceptions).

It seems curious to me that, from digging around, it appears this bug is frequently associated with ASP.NET MVC, but I don't believe I've seen it associated with WinForms or ASP.NET... I can't see how that would matter, but maybe there's a relationship there.

I suspect this is an MS bug. I can't imagine that there's any legitimate condition under which UserPrincipal.Current should cause an AppDomainUnloadedException, but I thought I'd take a shot here on Stackoverflow...

asp.net-mvc-4
active-directory
userprincipal
asked on Stack Overflow Jan 17, 2013 by Pete

0 Answers

Nobody has answered this question yet.


User contributions licensed under CC BY-SA 3.0