I am coding an MVC 5 internet application with a web api 2 web service. Do I need a dispose method for the DbContext class in a web service? It is not there as default.
相关问题
- WebAPI returns corrupted, incomplete file
- Using StoreGeneratedPattern.Identity with database
- Web API Custom Filter not firing
- How to generate URL for the action with attribute
- How to retrieve multiple columns from non-entity t
相关文章
- Why do I need a ToList() to avoid disposed context
- Cannot implicitly convert Web.Http.Results.JsonRes
- .NET Framework MVC and Web Api Auth JWT
- Why do we need Dispose() method on some object? W
- Get expire time of OAuth session
- Web Api HTTPPost not accepting int
- How Can I Use Custom Validation Attributes on Chil
- “Trust relationship between … and the primary doma
Actually,
System.Web.Http.ApiController
already implementsIDisposable
:So, if your controller holds a DbContext, do the following:
In Web Api 2, you can register a component for disposal when the request goes out of scope. The method is called "RegisterForDispose" and it's part of the Request. The component being disposed must implement IDisposable.
The best approach is to create your own extension method as below...
Now (in your api controller) you can register objects you want to dispose when request finalize...
Links... https://www.strathweb.com/2015/08/disposing-resources-at-the-end-of-web-api-request/