locked
web api throws exception 'An asynchronous module or handler completed while an asynchronous operation was still pending' RRS feed

  • Question

  • User1268471917 posted

    I have a .net web api which interacts with couchbase database through couchbaseDAL library project. There is no asynchronous method or code defined but still it throws

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

    this exception is not thrown always but after every 2, 3 calls.

    this exception doesn't get catch in catch block of web api method.

    [HttpGet]
            public string getLogin(string UserId,string pwd)
            {
                string returnMsg = string.Empty;
                CBEmployeeDAL cb = new CBEmployeeDAL();
                DB_Employee emp = new DB_Employee();
                try
                {
                    emp = cb.login(UserId, pwd);                               
                    if (emp.canLogin)
                        returnMsg= emp.Usertype.ToString();
                    else
                        returnMsg=  emp.LoginMsg;
                }
                catch (Exception ex)
                {
                    emp.LoginMsg = "issues while logging  in ! " + ex.ToString();
                    throw ex;
                }
                return returnMsg;
            }

    code for login method is

    public DB_Employee login(string UserId, string Pwd)
            {
                CouchbaseClient cbClient = new CouchbaseClient();
                var Doc_name = "Emp." + UserId;
    
                var doc = cbClient.Get(Doc_name);
                DB_Employee emp = new DB_Employee();
                try
                {
                    emp = JsonConvert.DeserializeObject(doc.ToString());
                }
                catch (Exception ex)
                {
                    DB_Employee loginFailed = new DB_Employee();
                    loginFailed.canLogin = false;
                    loginFailed.LoginMsg = "Please verify Credentials !";
                    return loginFailed;
                }
                if (emp.Pwd == Pwd)
                {
                    if (emp.isActive)
                    {
                        if (emp.ActiveTill > DateTime.Now)
                        {
                            emp.LoginMsg = "Login Successfull !";
                            emp.canLogin = true;
                            return emp;
                        }
                        else
                        {
                            emp.canLogin = false;
                            emp.LoginMsg = "Login has expired !";
                            return emp;
                        }
                    }
                    else
                    {
                        emp.canLogin = false;
                        emp.LoginMsg = "User has been deactivated!";
                        return emp;
    
                    }
                }
                else
                {
                    DB_Employee loginFailed = new DB_Employee();
                    loginFailed.canLogin = false;
                    loginFailed.LoginMsg = "Please verify Credentials !";
                    return loginFailed;
                }
            }

    Wednesday, July 15, 2015 10:13 PM

All replies

  • User753101303 posted

    Hi,

    this exception doesn't get catch in catch block of web api method

    So where do you see that? Do you have some call stack? Could it be that async APIs (or event handlers) are used at a lower level inside CBEmployeeDAL?

    Thursday, July 16, 2015 7:21 AM
  • User1268471917 posted

    Hi, when we invoke web api through browser we get this exception

    http://localhost/hrmsapi/api/Employee/GetLogin?userId=Rudra&password=Test

    Server Error in '/HRMSAPI' 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 in the code. 

    Exception Details: System.InvalidOperationException: An asynchronous module or handler completed while an asynchronous operation was still pending.

    Source Error: 

    An unhandled exception was generated during the execution of the current web request. Information regarding the origin and location of the exception can be identified using the exception stack trace below.


    Stack Trace: 

    [InvalidOperationException: An asynchronous module or handler completed while an asynchronous operation was still pending.]
    

     


    Version Information: Microsoft .NET Framework Version:4.0.30319; ASP.NET Version:4.0.30319.34248

    Thursday, July 16, 2015 11:00 PM
  • User1644755831 posted

    Hello newsachins,

    As @Pactrice suggested please post your full stack. and see if you are not doing some async operation at lower level.

    Unhandled exceptions can be processed via exception filters, but there are a number of cases that exception filters  can’t handle. For example:

    1. Exceptions thrown from controller constructors.
    2. Exceptions thrown from message handlers.
    3. Exceptions thrown during routing.
    4. Exceptions thrown during response content serialization .

    To work around the issues Try handling Errors at Global level and log the exceptions.

    Please see: Global Error Handling in ASP.NET Web API 2

    Hope this helps.

    With Regards,

    Krunal Parekh

    Thursday, July 16, 2015 11:04 PM