简体   繁体   中英

Handle HTTP request in C# Console application

我创建了一个工作正常的控制台应用程序,但我想添加处理http请求的能力,首先问题是我需要使用另一个端口,如1370,第二个问题:我不想使用ASP.NET Web服务因为它需要IIS。

You have several alternatives:
a) use Microsoft way - self hosted WCF for this. http://msdn.microsoft.com/en-us/library/ms731758.aspx

b) use open source - there's several flavors to choose from - ServiceStack ( http://servicestack.net ), open rasta ( http://openrasta.org/ )

c) do it from scratch, like using HttpListener or even at lower level, like sockets.

使用HttpListener类, 是一篇很好的文章

what about Web APi ? you can self host the web api. it's simple and light weight :)

using System.Web.Http;
using System.Web.Http.SelfHost;      

var config = new HttpSelfHostConfiguration("http://localhost:8080");

config.Routes.MapHttpRoute(
    "API Default", "api/{controller}/{id}", 
    new { id = RouteParameter.Optional });

using (HttpSelfHostServer server = new HttpSelfHostServer(config))
{
    server.OpenAsync().Wait();
    Console.WriteLine("Press Enter to quit.");
    Console.ReadLine();
}

Use WCF . You'll be able to handle HTTP requests.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM