An asynchronous module or handler completed while an asynchronous operation was still pending.

  • Thread starter Thread starter StBePos
  • Start date Start date
S

StBePos

Guest
Error Message:

An asynchronous module or handler completed while an asynchronous operation was still pending.






Behavior:

The application tries to get the UserPrincipalfor an Active Directory user through System.DirectoryServices.AccountManagement.dll. When for example GetUser("berste") is called once (see code snippet at the end), everything works as expected. When GetUser("reisbru") is called again within the same HTTP request the following error is returned to the calling client:

An asynchronous module or handler completed while an asynchronous operation was still pending.


In the log files we see, that the call (FindByIdentity()) returns with the correct result (existing AD users are found / non existing AD users are not found) also for the 2nd call, but nevertheless the mentioned error is shown when UserPrincipal.FindByIdentity()is called more than once.



In our tests we further figured out, that when the user to query is deactivated (not enabled) in the Active Directory the error (An asynchronous module or handler…) does not occur.




Environment / Application:

Asp.Net WebApi 2 application running in IIS environment. Version: 4.6.2.





Sample Code:

public UserPrincipalInternal GetUser(string sUserName)

{

var principalContext = new PrincipalContext(ContextType.Domain, domain, defaultOU, serviceUser, password);



var userPrincipal = UserPrincipal.FindByIdentity(principalContext, IdentityType.SamAccountName, sUserName);



UserPrincipalInternal userPrincipialInternal = new UserPrincipalInternal()

{

Guid = userPrincipal.Guid,

Sid = userPrincipal.Sid?.ToString(),

EmailAddress = userPrincipal.EmailAddress,

GivenName = userPrincipal.GivenName,

Surname = userPrincipal.Surname,

SamAccountName = userPrincipal.SamAccountName

};



userPrincipal.Dispose();



principalContext.Dispose();



return userPrincipialInternal;

}






public void Working()

{

var user1 = GetUser1("berste");

LOGGER.Debug($"User surname: {user1.Surname}"); // "Berer"


// No error occours. HTTP requests returns as expected.

}



public void NotWorking()

{

var user1 = GetUser1("berste");

LOGGER.Debug($"User surname: {user1.Surname}"); // "Berer"


var user2 = GetUser1("reisbru");

LOGGER.Debug($"User surname: {user2.Surname}"); // "Reisinger"


// Result for user1 and user2 are correct, but ...


// Error occurs: An asynchronous module or handler completed while an asynchronous operation was still pending.

}





System.DirectoryServices.AccountManagement.dll:

Continue reading...
 
Back
Top