检查连接是ASP.NET活跃 [英] Check connection is active in ASP.NET

查看:132
本文介绍了检查连接是ASP.NET活跃的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在写在ASP.NET彗星的应用程序。有活动的连接上下文数组(的HttpContext ) 。还有的是,应通过定期收集和重复检查他们的状态的线程。因此,应用程序的架构不是线程每个请求。

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)?

我发现这一点:

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.

也许有一些IIS或ASP.NET功能,允许监视连接,这种方式?

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

推荐答案

简短的答案是...没有。 ASP.NET知道为IsClientConnected返回false的唯一方法是,如果用户向服务器不同的呼叫,或Response.Close已​​经previously调用。它很可能为用户关闭连接并仍然IsClientConnected返回true,在我的经验。想想这是怎么回事潜在的HTTP通讯,你可以看到为什么。

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.

什么是你要完成?

这篇关于检查连接是ASP.NET活跃的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆