Check connection is active in ASP.NET

久未见 提交于 2019-12-20 04:53:30

问题


I work on a Comet application written in ASP.NET. There is an array of active connection contexts (HttpContext). And there is a thread that should periodically iterate through the collection and check theirs state. So application architecture is not thread-per-request.

What is the best way to check that a connection is active (not closed by the remote host)?

I found this:

context.Response.Write(' ');
context.Response.Flush();

if (!context.Response.IsClientConnected)
{
    // ...
}

But it's not a good solution because it takes a thread time to process (Flush() is blocking operation). I need solution that works very fast for many concurrent connections and don't use blocking operations.

Maybe there is some IIS or ASP.NET functionality that allows to monitor connections this way?


回答1:


The short answer is... no. The only way ASP.NET knows to return false for IsClientConnected is if the user makes a different call to the server, or Response.Close has been previously called. It's quite possible for the user to close the connection and still have IsClientConnected return true, in my experience. Just think about the underlying HTTP communication that's going on and you can see why.

What are you trying to accomplish?



来源:https://stackoverflow.com/questions/761376/check-connection-is-active-in-asp-net

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