过去作 端游的Http 服务器 用的WebApi 或者Mvc架构,都是放在iis。。。而我已是懒出一个地步,并不想去配iis,或者去管理iis,因此我很喜欢 Self host 的启动方式。html
C#作 http 有2个轻量级的框架, 一个是Nancy ,一个是 微软官方的Web Api 均可以经过owin self host 在应用程序中启动监听git
Web Api
官方教程 :https://www.asp.net/web-api/overview/hosting-aspnet-web-api/use-owin-to-self-host-web-apigithub
新建一个控制台从程序
在Nuget控制台上 安装包 Install-Package Microsoft.AspNet.WebApi.OwinSelfHost
而后添加一个Owin Startup类
以往全部的配置都正常的放在Startup中进行配置就能够web
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 } ); appBuilder.UseWebApi(config); } }
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
而后在Main 函数里面加入 WebApp.Start() 就会在SelfHost 上面运行你的Web Api,十分简洁api
class Program { static void Main(string[] args) { string baseAddress = "http://localhost:9000/"; // Start OWIN host using (WebApp.Start<Startup>(url: baseAddress)) { // Create HttpCient and make a request to api/values HttpClient client = new HttpClient(); var response =client.GetAsync(baseAddress + "api/home").Result; Console.WriteLine(response); Console.WriteLine(response.Content.ReadAsStringAsync().Result); Console.ReadLine(); } Console.ReadLine(); } }
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
尝试添加一个Controller安全
public class HomeController : ApiController { // GET api/values public IHttpActionResult Get() { return Ok(125); } // GET api/values/5 public string Get(int id) { return "value"; } // POST api/values public void Post([FromBody]string value) { } // PUT api/values/5 public void Put(int id, [FromBody]string value) { } // DELETE api/values/5 public void Delete(int id) { } }
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
尝试运行的结果 服务器
Nancy
Nancy是不少人都推荐的一个轻量级的Http 库,能够架在Mono,.net.core 上,也是开源
安装Nancy能够 Install-Package Nancy
而要 在SelfHost 监听则须要安装如下3个架构
Install-Package Microsoft.Owin.Hosting Install-Package Microsoft.Owin.Host.HttpListener Install-Package Nancy.Owin
- 1
- 2
- 3
在OwinStartup中代表使用Nancyapp
using Owin; public class Startup { public void Configuration(IAppBuilder app) { app.UseNancy(); } }
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
入口也是经过WebApp.Start(url)框架
using Microsoft.Owin.Hosting; class Program { static void Main(string[] args) { var url = "http://+:8080"; using (WebApp.Start<Startup>(url)) { Console.WriteLine("Running on {0}", url); Console.WriteLine("Press enter to exit"); Console.ReadLine(); } } }
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
来源: https://github.com/NancyFx/Nancy/wiki/Hosting-nancy-with-owin#katana—httplistener-selfhost
添加一个Module
/// <summary> /// 发现 /// 能够经过 Before 进行 安全的验证 /// </summary> public class HomeModule : NancyModule { public HomeModule() { Get["/"] = x => "Hello"; Get["/login"] = x => { return "person name :" + Request.Query.name + " age : " + Request.Query.age; }; } }
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
运行结果