用Web api /Nancy 經過Owin Self Host簡易實現一個 Http 服務器

過去作 端遊的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

運行結果 
這裏寫圖片描述 
這裏寫圖片描述

相關文章
相關標籤/搜索