How to create a Windows Service HTTP listener in .NET

I want to create a Windows service that works as an HTTP listener and can handle about 500 clients. Are there any special considerations for this kind of service?

I am a bit confused between the HTTPListener class and the TCPListener class. Which one to use for the Windows service, which will:

  • Accept client connection (about 500)
  • Process customer request
  • Call another Http based service
  • Return some value to the calling client

This is what I do to start the listener.

listener = new HttpListener(); listener.Prefixes.Add("http://localhost:8080/"); listener.Start(); listener.BeginGetContext(new AsyncCallback(OnRequestReceive), listener); private void OnRequestReceive(IAsyncResult result) { HttpListener listener = (HttpListener)result.AsyncState; // Call EndGetContext to complete the asynchronous operation. HttpListenerContext context = listener.EndGetContext(result); HttpListenerRequest request = context.Request; } 

Can I process N clients at the same time?

+4
source share
4 answers

If you need to have high responsiveness, the HttpListener will not scale very well (you cannot accept more than one connection at a time). But for little use, trying to be careful to get the context and write the response asynchronously, it may work.

EDIT: It appears that I misunderstood the HttpListener ability to accept connections. You can accept multiple connections at the same time (see Comment). However, other IIS objects for code placement will avoid wheel reuse. Therefore, if there are no special requirements that exclude IIS, why not take a simple path?

For serious use, use IIS by creating an Http handler (a class that implements IHttpHandler or IHttpHandlerFactory or even IHttpAsyncHandler) to process requests (this is the base type that implements .aspx and others).

The correct decision really depends on what you mean by "description of about 500 clients": one at a time or immediately?

Based on the response to the comment: 500 while noting that the processing in step 3 includes another HTTP call, I doubt that the HttpListner will be able to handle the load without guaranteeing that each operation is asynchronous (receiving context and request by performing subsequent HTTP -query, and then sending a response) ... which will lead to even more complex coding.

If I have no reason to prevent IIS, using IIS will be easier because it is designed to support large-scale client requests with rich functionality, while the HttpListener "Provides a simple, programmatically controlled HTTP protocol listener."

EDIT: expanded based on more detailed download information.

+1
source

I would use the HttpListener class. He does a lot of things for you, and you won’t need to reinvent the wheel. It also integrates with the http.sys kernel http.sys in Windows Server 2003 and should provide better performance.

0
source

I created a TcpListener and started the listener using the BeginAcceptTcpClient method. Each incoming request is sent to a separate stream for processing, where the client connection is used continuously to receive / send data from the client. It seems to work great for a large number of customers.

0
source

BeginGetContext accepts only one request. You need to repeat the asynchronous call every time you receive a request so that you make more than one request.

Some people simplify their design by having a listener thread that waits synchronously (i.e. uses a synchronous GetContext) in a loop. This is not a nice design, because in the end you have a thread there waiting (loss of system resources), but this may be acceptable if you are not religious in terms of performance / implementation of the concept of proof of concept.

You also need to place the call to listener.Close () in the method to disable the service. Carefully synchronize this with possible requests for processing asynchronous threads.

0
source

All Articles