2010-11-10 74 views
6

我一直在嘗試在我的工作站上使用HTTP.sys/HttpListener進行一些測試,並且似乎有一些限制阻止了更多那1000個併發連接。有沒有人有關於此的更多信息? (因爲1k似乎有點清理是巧合)。Windows 7旗艦版x64上的System.Net.HttpListener限制爲1k併發連接

我試圖找到任何線程/配置/註冊表設置,但已經空了。

在此先感謝。
GJ


貌似我偷步有點。

我似乎錯過了使用http.sys/HttpListener BeginGetContext不是很好的併發連接,因爲新的BeginGetContext只會在先前請求的響應流關閉後觸發。

所以有1000個請求的積壓,在這種情況下積壓正在填滿。無論如何 - 如果任何人有任何意見(或可能的更正),隨意擴大。

感謝
GJ

回答

5

我已經做到了是爲具有對HttpListener偵聽使用阻斷GetContext()方法,但只要它接收到一個請求,通過使用IAsyncResult圖案做一個異步調用將其傳遞給另一個線程的螺紋的方式與這似乎工作正常。

private void Run() 
    { 
     while (true) 
     { 
      if (this._disposed || this._shouldTerminate) return; 

      if (this._listener.IsListening) 
      { 
       try 
       { 
        HttpListenerContext context = this._listener.GetContext(); 

        //Hand it off to be processed asynchronously 
        this._delegate.BeginInvoke(context, new AsyncCallback(this.EndRequest), null); 
       } 
       catch (Exception ex) 
       { 
        this.LogErrors(ex); 
       } 
      } 
     } 
    } 

    private delegate HttpServerContext HandleRequestDelegate(HttpListenerContext context); 

    private HttpServerContext HandleRequest(HttpListenerContext context) 
    { 
     IHttpListenerHandler handler; 
     HttpServerContext serverContext = new HttpServerContext(this, context); 
     try 
     { 
      bool skipHandling = this.ApplyPreRequestModules(serverContext); 
      if (!skipHandling) 
      { 
       handler = this._handlers.GetHandler(serverContext); 
       handler.ProcessRequest(serverContext); 
      } 
     } 
     catch (NoHandlerException noHandlerEx) 
     { 
      this.LogErrors(noHandlerEx); 
      context.Response.StatusCode = (int)HttpStatusCode.MethodNotAllowed; 
     } 
     catch (HttpServerException serverEx) 
     { 
      this.LogErrors(serverEx); 
      context.Response.StatusCode = (int)HttpStatusCode.InternalServerError; 
     } 

     return serverContext; 
    } 

    private void EndRequest(IAsyncResult result) 
    { 
     try 
     { 
      HttpServerContext context = this._delegate.EndInvoke(result); 
      this.ApplyPreResponseModules(context); 
      context.Response.Close(); 
     } 
     catch (Exception ex) 
     { 
      this.LogErrors(ex); 
     } 
    } 
+0

乾杯羅布 - 這讓我過度了! – CameraSchoolDropout 2010-11-17 12:17:31

0

這裏有一個簡單的方法來支持與HttpListener多個併發請求。

 for (int i = 0; i < 50; i++) { 
      _listener.BeginGetContext(GetContextCallback, null); 
     } 

這現在將使您能夠接收50個併發請求。必須承認,我從來沒有嘗試過創造1000!

+0

謝謝達雷爾 - 這個工作,但不幸的是不適合我面臨的問題。 – CameraSchoolDropout 2010-11-14 11:37:59

相關問題