How to free resources and dispose injected service in ASP.NET 5/Core by the end of request?

后端 未结 3 550
粉色の甜心
粉色の甜心 2020-12-09 16:28

I have a service which is injected into a controller using the ASP.NET Core\'s default Dependency Injection Container:

public class FooBarService : IDisposab         


        
3条回答
  •  情歌与酒
    2020-12-09 17:23

    I see no one mentioned this yet, but besides implementing IDisposable in your type, you can also use {HttpContext}.Response.RegisterForDispose(objectToDispose). Typically this is used to register an object at the start of a request (such as a controller action) to be disposed when the request ends.

提交回复
热议问题