EF. The connection was not closed. The connection's current state is connecting
Trying to use a connection that is already "connecting" - clear sign of some race condition.
- Re-check that
IUserService
is registered with "scope" lifetime, and all it dependencies (userManager, dbContext) too - Do not use
IServiceProvider
you obtained during app startup for scope-bases services resolution - it is NOT related to current request scope and return instances from "some other universe". UseHttpContext.RequestServices
for service resolution. - Check that your are "awaiting" all async methods. If you start second request while still executing first one - you may possibly "catch" dbContext during "connecting" stage.
- Your
JwtMessageHandler
instance is one/single per app. So don't use it's property for storing_userService
(removeprivate IUserService _userService
). Instead, use local variable insideOnMessageReceived
(var _userService = ...
).
You already checked (1), (2) and (3). I think (4) is the last one you need to fix your bug.