要 Self Hosting Web API,首先需要安裝 Microsoft.AspNet.WebApi.SelfHost 套件。

建立 HttpSelfHostConfiguration 並指定要監聽的位置。

var config =new HttpSelfHostConfiguration("http://localhost:32767");

設定 HttpSelfHostConfiguration,像是 URL Routing 資訊。

config.Routes.MapHttpRoute("API","{controller}/{action}/{id}",
new{ id =RouteParameter.Optional });

如果要被 Host 的 Web API 在其它的組件,這邊也可建立一個實作 IAssembliesResolver 的 SelfHostAssemblyResolve 類型。

    public class SelfHostAssemblyResolver : IAssembliesResolver
    {
        #region  Properties
        /// <summary>
        /// Gets the path.
        /// </summary>
        /// <value>
        /// The path.
        /// </value>
        public string Path { get; private set ; }
        #endregion  Properties


        #region  Constructors
        /// <summary>
        /// Initializes a new instance of the <see cref="SelfHostAssemblyResolver"/> class.
        /// </summary>
        /// <param name="path"> The path.</param>
        public SelfHostAssemblyResolver(string path)
        {
            this.Path = path;
        }
        #endregion  Constructors


        #region  Methods
        /// <summary>
        /// Returns a list of assemblies available for the application.
        /// </summary>
        /// <returns>
        /// An &lt;see cref="T:System.Collections.Generic.ICollection`1" /&gt; of assemblies.
        /// </returns>
        public ICollection <Assembly> GetAssemblies()
        {
            List<Assembly > assemblies = new List<Assembly >();


            assemblies.Add( Assembly.LoadFrom(Path));


            return assemblies;
        }
        #endregion  Methods
    }

帶入外部組件位置建立對應的物件,然後將服務中的 IAssembliesResolver 用該物件取代即可。

config.Services.Replace(typeof(IAssembliesResolver),new SelfHostAssemblyResolver("AgileSlot.API.dll"));

接著建立 HttpSelfHostServer,建立時帶入前面所建立的 HttpSelfHostConfiguration

當開始要使用服務時,叫用 HttpSelfHostServer.OpenAsync 啟動 Web API 服務

httpServer.OpenAsync();

當不在需要服務時,叫用 HttpSelfHostServer.CloseAsync 中止 Web API 服務

httpServer.CloseAsync();

整個程式寫起來會像下面這樣:

var config =new HttpSelfHostConfiguration("http://localhost:32767");
config.Routes.MapHttpRoute("API","{controller}/{action}/{id}",
new{ id =RouteParameter.Optional });

config.Services.Replace(typeof(IAssembliesResolver),newSelfHostAssemblyResolver("AgileSlot.API.dll"));

using(var httpServer =new HttpSelfHostServer(config))
{
    httpServer.OpenAsync().Wait();
    Console.WriteLine("Web API host started...");
    string line =null;
    do
    {
        line =Console.ReadLine();
    }
    while(line !="exit");
    httpServer.CloseAsync().Wait();
}