System.InvalidOperationException: 'Invalid operation. The connection is closed.'

Anjali Agarwal 1,471 Reputation points
2023-02-06T03:09:17.0033333+00:00

hello All,

I am getting this error when calling a small function. I am calling this function twice. Below is my code:

public async Task<EmployeeInfo?> GetEmployeeByEmployeeNumber(string employeeId)

    {

        List<int> emplyoeeInfoId = new List<int>();

        UserPrincipal up = utilities.UserADIdentity.GetADUserInfo(WindowsIdentity.GetCurrent().Name.ToString());

        emplyoeeInfoId = _ackContext.EmployeeInfos.Where(e => e.EmployeeNumber == employeeId).OrderBy(e => e.DateFormFilled).Select(e => e.EmployeeInfoId).ToList();

        var employee = await _ackContext.EmployeeInfos.Include(e => e.EmergencyInfos.Where(p => p.EmployeeInfoId.Equals(emplyoeeInfoId.LastOrDefault()))).Where(e=>e.EmployeeInfoId.Equals(emplyoeeInfoId.LastOrDefault())).FirstOrDefaultAsync();

return employee;

}

First, I am calling this function from a employeeController and then later, I am calling the same function from a different controller. Below is the screen shot of the error. when i am calling this method from employeecontroller then i am not getting any exception, but later when I am calling a same method from a different controller then i am getting an exception.

User's image

ASP.NET Core
ASP.NET Core
A set of technologies in the .NET Framework for building web applications and XML web services.
4,611 questions
ASP.NET
ASP.NET
A set of technologies in the .NET Framework for building web applications and XML web services.
3,507 questions
{count} votes

Accepted answer
  1. Michael Taylor 54,806 Reputation points
    2023-02-06T17:35:04.64+00:00

    The problem is that the DbContext is being released by the DI container before the call to your code which results in the error. Normally the DI container will handle the lifetime properly for a request. At the end of the HTTP request then it releases the resources. In your case the call is occurring after the request has been completed.

    Note that each HTTP request gets a new instance of all your objects including the controller, DbContext and services you've registered in the DI container. I assume your BindingToAppServices call is responsible for registering your EmployeeService with the container. Therefore your service and the DbContext given to it are valid only during the HTTP request associated with it.

    So where is it failing? My gut instinct is that you are calling GetEmployeeByEmployeeNumber either without awaiting the result (because it is async) or something that is running outside the context of the HTTP request is calling it. Find all references to that method. Then ensure you are awaiting calls to the method. You can post relevant code if it helps.

    If everything looks correct there then you need to walk your way up the call stack and ensure any calling code (of the code that calls the method) are also awaiting all the way to your controller. If you still don't find anything then ensure that you are not calling your service anywhere outside the HTTP request pipeline (such as a callback to something that might run after the request).

    2 people found this answer helpful.

0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.