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).