ASP.NET - Trapping client disconnections on long running HTTP requests
I have a long poll HTTP request using ASP.NET 4, MVC 2 and AsyncController. If a user closes their browser and kills the HTTP connection wit开发者_开发知识库hout the request completing, I'd like to know about it and completely clean up after them. If I don't, the open and incomplete requests just sit there and eventually IIS stops accepting new requests.
You can simulate my long running HTTP request by making a normal ASP.NET application with a page that has a Thread.Sleep. Even if you close the browser, the request carries on as if it hasn't.
There is a property called Response.IsClientConnected that gets switched to false if the client disconnects, and I can poll this to achieve the desired effect but it's not very clean and I'd like to avoid polling. Is there a way of getting notified when this happens rather than having to poll this property?
Thanks
Standard way to do that is to register a callback with ClientDisconnected
event. Unfortunately there is no such an event in ASP.NET and Response.IsClientConnected
is not reliable. Maybe this hack helps:
Create a page and force clients to request that page via a very small image (i.e 1x1 pixel) or a small frame and send a request from clients at specific intervals (use Javascript timers). The request should determine who is the client (use clientId as a parameter passed to the page). If there's no signal from a client, it could be considered as disconnected by your application.
精彩评论