过去做 端游的Http 服务器 用的WebApi 或者Mvc架构,都是放在iis。。。而我已经是懒出一个地步,并不想去配iis,或者去管理iis,所以我很喜欢 Self host 的启动方式。
C#做 http 有2个轻量级的框架, 一个是Nancy ,一个是 微软官方的Web Api 都可以通过owin self host 在应用程序中启动监听
Web Api
官方教程 :https://www.asp.net/web-api/overview/hosting-aspnet-web-api/use-owin-to-self-host-web-api
新建一个控制台从程序
在Nuget控制台上 安装包 Install-Package Microsoft.AspNet.WebApi.OwinSelfHost
然后添加一个Owin Startup类
以往所有的配置都正常的放在Startup中进行配置就可以
public class Startup
{
// This code configures Web API. The Startup class is specified as a type
// parameter in the WebApp.Start method.
public void Configuration(IAppBuilder appBuilder)
{
// Configure Web API for self-host.
HttpConfiguration config = new HttpConfiguration();
config.Formatters.JsonFormatter.SupportedMediaTypes.Add(new MediaTypeHeaderValue("text/html"));
config.Routes.MapHttpRoute(
name: "DefaultApi",
routeTemplate: "api/{controller}/{id}",
defaults: new { id = RouteParameter.Optional }
);
a