/ dotnet core

ASP.NET Core Cannot access a disposed object

System.ObjectDisposedException:“Cannot access a disposed object. A common cause of this error is disposing a context that was resolved from dependency injection and then later trying to use the same context instance elsewhere in your application. This may occur if you are calling Dispose() on the context, or wrapping the context in a using statement. If you are using dependency injection, you should let the dependency injection container take care of disposing context instances.ObjectDisposed_ObjectName_Name

DbContext 的生命周期由 ASP.NET Core 来控制,在 Action 中调用类似 async void Method() 将导致上述异常。

async void Method(){} // 导致异常
async Task Method(){} // 正常