【问题标题】:How to get OWIN HttpListener current connections count?如何获取 OWIN HttpListener 当前连接数?
【发布时间】:2015-04-03 09:25:58
【问题描述】:

我在控制台应用程序中使用 OWIN selfhost WebAPI,它使用 HttpListener。

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

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

【问题讨论】:

    标签: asp.net-web-api owin


    【解决方案1】:

    鉴于 HTTP 的性质,实际上不可能监控“活动”连接,因为 HTTP 中并不真正存在该概念。客户端向您的服务器发送请求,但它要么失败,要么收到响应。

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

    【讨论】:

    • 我需要当前接受但不返回结果请求的计数
    • 也许将 Q 的措辞改为“请求”而不是“连接”
    【解决方案2】:

    您可以创建自己的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 等。

    【讨论】:

      【解决方案3】:

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

      【讨论】:

      • 我同意。基本问题不正确,我更改解决方案
      猜你喜欢
      • 2017-08-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-06-15
      • 1970-01-01
      相关资源
      最近更新 更多