如何在.NET中创建HTTP请求侦听器Windows服务 [英] How to create a HTTP request listener Windows Service in .NET

查看:145
本文介绍了如何在.NET中创建HTTP请求侦听器Windows服务的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想创建充当HTTP侦听器的Windows服务,并且可以处理大约500个客户端。对于这种服务是否有任何特殊注意事项。

I want to create Windows Service that acts as a HTTP listener and can handle around 500 clients. Are there any special considerations for this kind of service.

我在HTTPListener类和TCPListener类之间有点混淆。哪一个用于Windows服务将:

I am a little confused between the HTTPListener class and the TCPListener class. Which one to use for a Windows Service that will:


  1. 接受客户端连接(大约500)

  2. 处理客户端请求

  3. 调用另一个基于Http的服务

  4. 将一些值返回给调用客户端

  1. Accept the client connection (around 500)
  2. Process client request
  3. Call another Http based service
  4. Return some value to the calling client






这就是我正在做的启动监听器。


This is what I am doing 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;
} 

我能同时处理N个客户吗?

Will I be able to handle N clients simultaneously?

推荐答案

如果你需要能够高度响应 HttpListener 将无法很好地扩展(您一次不能接受多个连接)。但是对于小规模使用,小心获取上下文并异步编写响应它可以工作。

If you need to be able to highly responsive HttpListener will not scale very well (you cannot accept more than one connection at a time). But for small scale use, being careful to get context and write the response asynchronously it can work.

编辑:出现我误解 HttpListener 接受连接的能力。您可以同时接受多个连接(请参阅注释)。然而,IIS的其他主机代码设施将避免重新发明轮子。因此,除非有非常具体的要求阻止IIS,为什么不采取简单的路径?

Appears I misunderstood HttpListener's ability to accept connections. You can accept multiple connections concurrently (see comment). However IIS's other facilities to host code would avoid reinventing the wheel. So unless there are very specific requirements that preclude IIS, why not take the easy path?

为了认真使用,使用IIS,创建一个Http Handler(一个实现IHttpHandler的类)或IHttpHandlerFactory,甚至是IHttpAsyncHandler)来处理请求(这是实现.aspx等的基础类型)。

For serious use, use IIS, creating an Http Handler (a class that implements IHttpHandler or IHttpHandlerFactory, or even an IHttpAsyncHandler) to handle the requests (this is the underlying type that implements .aspx et al).

正确的解决方案真的取决于你的意思通过处理大约500个客户:一次一个或一次一个?

The right solution really depends on what you mean by "handle around 500 clients": one at a time or all at once?

基于评论答案:500一次,并注意到处理,一步一步3,包括另一个HTTP调用,我怀疑使用HttpListner将能够处理负载而不确保每个操作都是异步的(获取上下文和请求,执行转发HTTP请求然后发送响应)...这将导致更多难以编码。

Based on the comment answer: 500 at once, and noting that the processing, in step 3, includes another HTTP call, I doubt using HttpListner will be able to handle the load without ensuring every operation is asynchronous (getting context and request, performing the onward HTTP request and then sending the response)... which will lead to some more difficult coding.

除非有充分理由避免使用IIS,否则使用IIS会更容易,因为它旨在支持大规模客户端请求加载具有丰富的功能,而HttpListener提供一个简单的,以编程方式控制的HTTP协议监听器。。

Unless there is a very good reason to avoid IIS, using IIS will be easier as it is designed to support large scale client request loads with rich functionality, whereas HttpListener "Provides a simple, programmatically controlled HTTP protocol listener.".

编辑:根据更多负载细节进行扩展。

Expanded based on more details of load.

这篇关于如何在.NET中创建HTTP请求侦听器Windows服务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆