如何获得OWIN HttpListener当前连接数?

时间:2015-04-03 09:25:59

标签: asp.net-web-api owin

我在控制台应用程序中使用OWIN自主Web API,它使用了HttpListener。

_owinApplication = WebApp.Start<Startup>(new StartOptions(baseUri));

如何在某个时间间隔内监控当前活动连接到我的应用程序?

3 个答案:

答案 0 :(得分:0)

鉴于HTTP的性质,它不太可能监控&#34;活跃&#34;连接,因为这个概念在HTTP中并不存在。客户端向您的服务器发送请求,该请求失败或收到响应。

当您看到网站报告当前活跃用户时,该数字通常是合格的猜测,或者他们可能使用websockets或某种ajax轮询来监控客户端。

答案 1 :(得分:0)

您可以创建自己的DelegatingHandler,在WebApi管道中注册它,并使用重写的SendAsync方法监控当前连接:

protected override async Task<HttpResponseMessage> SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)
{
    // TODO: add this request to some static container (like ConcurrentDictionary)

    // let the request to be processed
    HttpResponseMessage response;
    try
    {
        response = await base.SendAsync(request, cancellationToken);
    }
    finally
    {
        // TODO: remove the request from the static container registered above
    }

    // return the response
    return response;
}

这样,您不仅可以监控当前的连接数,还可以监控所有请求信息,如URL,IP等。

答案 2 :(得分:0)

我正在创建自定义中间件,它解决了我的问题