標籤:
webapi問世已久,稀裡糊塗的人哪它都當mvc來使,畢竟已mvc使用層級的經驗就可以應對webapi。
webapi和mvc在asp.net5時代合體了,這告訴我們,其實 它倆還是有區別的,要不現在也不會取兩個名字,但是由於本人歸納總結能力較差,各種不同也無法一一列出了。
在webapi中 HelpPage是個突出而又實用的東西,它尼瑪會把我們code中的注釋產生xml,然後以web頁面的方式把介面文檔展示出來,這尼瑪無形就下崗一批文案了,以社會責任感角度來講,ms的這個HelpPage挺不地道的,但我表示就喜歡這樣的東西。。
步驟也很簡單:
1、negut 搜 helppage ,認準Id是Microsoft.AspNet.WebApi.HelpPage就Install它;
2、建立一個xml檔案,在項目屬性-產生中 xml文檔檔案路徑指向它 勾選;
3、引入產生的HelpPage中HelpPageConfig.cs中,此代碼“config.SetDocumentationProvider(new XmlDocumentationProvider(HttpContext.Current.Server.MapPath("~/App_Data/ApiDocument.xml")));”,修改xml路徑 然後解注釋;
4、最後一產生就會發現你寫的並沒有卵用的注釋被一坨一坨塞入了那個xml檔案當中,當訪問help/index 就會看到配圖如下:
(附詳細參考一份:http://www.asp.net/web-api/overview/getting-started-with-aspnet-web-api/creating-api-help-pages)
5、Negut認準Id:WebApiTestClient,Install它,你會發現多了一個js、一個cs、兩個cshtml檔案,都是TestClient開頭的,別處用的時候不要忘了靠它。
Api.cshtml檔案中加入兩行代碼
@Html.DisplayForModel("TestClientDialogs")@section Scripts{ <link href="~/Areas/HelpPage/TestClient.css" rel="stylesheet" /> @Html.DisplayForModel("TestClientReferences")}
點入某一個介面以後,戳那個Test Api,你的項目就可以進行介面測試了。此刻你會更堅定自己身為碼農的存在,我們並不是coder,我們不生產代碼,我們只是代碼的搬運工。
(附詳細參考一份:https://blogs.msdn.microsoft.com/yaohuang1/2012/12/02/adding-a-simple-test-client-to-asp-net-web-api-help-page/)
微幅扯淡之後,言入主題,畢竟文章精髓的要藏在後面。
從文章標題來看,就可以分成兩個部分,webapi可以寄宿iis、console、winform、winservice等,外掛程式開發大家也不陌生,所以精髓的部分也言簡意賅了,這也是很無奈的事情啊。
1、使用Self Host自託管方式,negut-Id:Microsoft.AspNet.WebApi.SelfHost
1.1 首先準備一個contrller和model,代碼很簡單
public class Product { public int Id { get; set; } public string Name { get; set; } public string Category { get; set; } public decimal Price { get; set; } }public class ProductsController : ApiController { Product[] products = new Product[] { new Product { Id = 1, Name = "Tomato Soup", Category = "Groceries", Price = 1 }, new Product { Id = 2, Name = "Yo-yo", Category = "Toys", Price = 3.75M }, new Product { Id = 3, Name = "Hammer", Category = "Hardware", Price = 16.99M } }; public IEnumerable<Product> GetAllProducts() { return products; } public Product GetProductById(int id) { var product = products.FirstOrDefault((p) => p.Id == id); if (product == null) { throw new HttpResponseException(HttpStatusCode.NotFound); } return product; } public IEnumerable<Product> GetProductsByCategory(string category) { return products.Where(p => string.Equals(p.Category, category, StringComparison.OrdinalIgnoreCase)); } }
1.2 寄宿於console,監聽port,配置route,啟動service,一目瞭然
class Program { static void Main(string[] args) { Console.WriteLine("請輸入監聽連接埠"); string port = Console.ReadLine(); var config = new HttpSelfHostConfiguration(string.Format("http://localhost:{0}", port ?? "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 ESC to quit."); while ( !Console.ReadKey().Key.Equals(ConsoleKey.Escape)) { } } } }
1.3 用winservice來承載webapi服務
首先: Install-Package Microsoft.AspNet.WebApi.OwinSelfHost (提到Asp.Net Owin一句話可形容,Asp.Net5跨平台,此逼功不可沒啊)
建立好winserver項目後就可以建立一個Owin的啟動類別,在其當中構建webapi的配置
public class Startup { public void Configuration(IAppBuilder appBuilder) { // Configure Web API for self-host. HttpConfiguration config = new HttpConfiguration(); config.Routes.MapHttpRoute( name: "DefaultApi", routeTemplate: "api/{controller}/{id}", defaults: new { id = RouteParameter.Optional } ); //config.Services.Replace(typeof(IAssembliesResolver), new PluginsResolver()); appBuilder.UseWebApi(config); } }
在service中啟動,over。
private IDisposable _apiserver = null; protected override void OnStart(string[] args) { //Services URI string serveruri = string.Format("http://localhost:{0}/", System.Configuration.ConfigurationManager.AppSettings["port"]); // Start OWIN host _apiserver = WebApp.Start<Startup>(url: serveruri); base.OnStart(args); } protected override void OnStop() { if (_apiserver != null) { _apiserver.Dispose(); } base.OnStop(); }
2、外掛程式式服務,看似逼格很高,其實只是標題黨,畫龍點睛的代碼到了...
重寫DefaultAssembliesResolver的GetAssemblies,反射載入指定檔案下的dll中的controller,在WebApiConifg添加其實現
public class PluginsResolver : DefaultAssembliesResolver { public override ICollection<Assembly> GetAssemblies() { //動態載入dll中的Controller,類似於外掛程式服務,在WebApiConifg中添加配置 // config.Services.Replace(typeof(IAssembliesResolver), new PluginsResolver()); List<Assembly> assemblies = new List<Assembly>(base.GetAssemblies()); string directoryPath = Path.Combine(System.AppDomain.CurrentDomain.BaseDirectory, "dynamic_services"); string[] files = Directory.GetFiles(directoryPath); foreach (var fileName in files) { assemblies.Add(Assembly.LoadFrom(Path.Combine(directoryPath, fileName))); } return assemblies; } }
需要什麼介面服務直接寫好dll仍進dynamic_services檔案夾下就有了。
asp.net webapi 自託管外掛程式式服務