Handling multiple requests with C# HttpListener

后端 未结 3 2058
执笔经年
执笔经年 2020-11-28 09:12

I have a .NET Windows Service which spawns a thread that basically just acts as an HttpListener. This is working fine in synchronous mode example...

<         


        
3条回答
  •  天涯浪人
    2020-11-28 09:38

    If you need a more simple alternative to BeginGetContext, you can merely queue jobs in ThreadPool, instead of executing them on the main thread. Like such:

    private void CreateLListener() {
        //....
        while(true) {
            ThreadPool.QueueUserWorkItem(Process, listener.GetContext());    
        }
    }
    void Process(object o) {
        var context = o as HttpListenerContext;
        // process request and make response
    }
    

提交回复
热议问题