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

  • Question

  • Error Message:

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

     

    Server Error in '/Api' Application. 
An asynchronous module or handler completed while an asynchronous operation was still pending. 
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated 
Exception Details: System.lnvalidOperationExceptiorr An asynchronous module or handler completed while an asynchronous operation was still pending. 
Source Error: 
the eke •E the Be 
Stack Trace: 
[InvalidOperationException: An 
asynchronous module or handler completed while an asynchronous operation was still

    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:

    Friday, November 30, 2018 12:27 PM

All replies

  • Hello,

    Please edit your post and use the code button for code, button second from the right in the editor toolbar. Also fix your images or remove them as they are not being displayed.


    Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.
    VB Forums - moderator
    profile for Karen Payne on Stack Exchange, a network of free, community-driven Q&A sites

    Friday, November 30, 2018 1:49 PM
  • Hi StBePos,

    This forum discusses and asks questions about the C# basic programming language, since your issue is more related to asp.net webAPI, I would suggest that you could post your issue on asp.net web api forum for suitable support.

    https://forums.asp.net/1246.aspx/1?Web+API

    Thanks for your understanding.

    Best regards,

    Zhanglong


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.


    Monday, December 3, 2018 5:28 AM