WebApi:WebApi的Self Host模式

时间:2023-03-09 08:29:08
WebApi:WebApi的Self Host模式

不用IIS也能執行ASP.NET Web API

转载:http://blog.darkthread.net/post-2013-06-04-self-host-web-api.aspx

在某些情境,桌面環境執行的程式(Console、Windows Form、WPF… 等)也需要提供API管道供外界呼叫,例如: 先前提到的Word轉PDF服務、ERP UI接受外部(如Excel VBA)匯入資料... 等等。

設計API管道時有不少選擇: DDE、Anonymous Pipe/Named Pipe、Socket... 都可行。對轉行寫桌面程式的ASP.NET開發者來說,還有一個溫馨的好選擇 -- 在桌面程式專案裡寫ASP.NET Web API吧!!

是的,即使沒有IIS,ASP.NET Web API也能照跑不誤,在Windows Form、WPF可以繼續用同一招打天下,對跨界寫桌面程式的ASP.NET開發人員,實在是一大福音。

以下使用Console Application專案做個簡單示範。建好新專案後,透過NuGet Packages Manager尋找self host,可以找到"Microsoft ASP.NET Web API Self Host"套件,二話不說立刻安裝。

WebApi:WebApi的Self Host模式

ASP.NET Web API Self Host由多個組件構成,但不用擔心,NuGet會自動一一下載安裝好。

WebApi:WebApi的Self Host模式

安裝完成後,我們要在主程式中加幾行程式,啟動一個小小的Http Server。

第一步要先透過HttpSelfHostConfiguratio宣告提供Web API的URL。由於向Windows註冊特定的TCP Port需要權限,有兩種做法: 以管理者身分執行Visual Studio及應用程式,或是透過netsh http add urlacl url=http://+:port_number/ user=machine\username指令授權。依"永遠只授與足以執行的最小權限"的資安原則,用netsh授權雖然手續較麻煩,但比讓整個應用程式都具有管理者權限安全。

接 著,使用Routes.MapHttpRoute()指定MVC必備的路由設定,就可使用這組設定值宣告一個HttpSelfHostServer並啟 動。由於會動用到網路資源,建議使用using HttpSelfHostServer的寫法,確保結束時會透過Dispose()釋放相關資源。

加上一段迴圈,直到使用者輸入exit才結束HttpSelfHostServer。在這段期間,HttpSelfHostServer便能接收HTTP請求,找到適當的Controller提供服務。

static void Main(string[] args)
{
//指定聆聽的URL
var config = new HttpSelfHostConfiguration("http://localhost:8011");
//注意: 在Vista, Win7/8,預設需以管理者權限執行才能繫結到指定URL,否則要透過以下指令授權
//開放授權 netsh http add urlacl url=http://+:32767/ user=machine\username
//移除權限 netsh http delete urlacl url=http://+:32767/
//設定路由
config.Routes.MapHttpRoute("API", "{controller}/{action}/{id}", new { id = RouteParameter.Optional });
//設定Self-Host Server,由於會使用到網路資源,用using確保會Dispose()加以釋放
using (var httpServer = new HttpSelfHostServer(config))
{
//OpenAsync()屬非同步呼叫,加上Wait()則等待開啟完成才往下執行
httpServer.OpenAsync().Wait();
Console.WriteLine("Web API host started...");
//輸入exit按Enter結束httpServer
string line = null;
do
{
line = Console.ReadLine();
}
while (line != "exit");
//結束連線
httpServer.CloseAsync().Wait();
}
}

Console Application專案沒有Models、Controllers、Views資料夾,要如何加入Web API Controller讓人有些茫然,此時讓我們回歸ASP.NET MVC的"Convension over Configuration"(以慣例取代設定)原則: 在專案中新增一個名為BlahController的類別並繼承ApiController,Self Host自然會依著類別名稱認出它,並在有人呼叫http:// localhost:32767/Blah時派它上場。

為了測試,我宣告了一個很沒營養的Date方法傳回日期字串,標註[HttpGet]是為方便用瀏覽器輸入URL就能直接看結果(否則預設只接受POST,需要寫JavaScript才能測試)。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.Http; namespace SelfHostWebApi
{
public class BlahController : ApiController
{
[HttpGet]
public string Date()
{
return DateTime.Today.ToString("yyyy/MM/dd");
}
}
}

實際執行結果如下:

WebApi:WebApi的Self Host模式

不會寫Socket、不懂Named Pipe,居然也能寫出具有API整合功能的桌面程式~ 衝著這點,讓我們一起呼喊: ASP.NET Web API 好威呀!