How do I dispose of resources after an ASP.NET Web API request completes?

喜欢而已 提交于 2019-11-29 23:46:48

问题


I am attempting to expose an IQueryable<> over ASP.NET Web API and I find that I need to keep the data source open until the request completes, so that the OData query system built into ASP.NET Web API can do its job.

OK, that sounds perfectly reasonable. But where do I dispose of the data source? I do not see any obvious place for this. Should I manage request state in the Application? What is the standard way to do this?

Is the Dispose() method of the controller the appropriate place? I.e. is there a guarantee that one controller instance serves only one request or is that just an implementation detail?

I am using ASP.NET Web API from ASP.NET MVC 4 RC.


回答1:


You can use the extension called RegisterForDispose on the HttpRequestMessage to register any resources that you would like to dispose off when a request is done.

BTW, you might want to look at the following link to regarding IQueryable support in next release of Web API. http://aspnetwebstack.codeplex.com/discussions/359229

[Edited] Doing things in Dispose() method of the Controller would also be a solution. In Web API ApiController based controllers are always per-instance (one controller instance per one request)



来源:https://stackoverflow.com/questions/11361912/how-do-i-dispose-of-resources-after-an-asp-net-web-api-request-completes

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!