新建Web API工程
选Empty,勾选Web API,不要选择Web API,那样会把MVC勾上,这里不需要MVC
Web API工程属性
XML文件用于生成在线文档
新建Windows服务作为Web API的宿主
WebApiHost工程属性
控制台应用程序方便调试
Windows服务安装Microsoft.AspNet.WebApi.OwinSelfHost
工程WebApiDemo需要引用Microsoft.Owin.dll
WebApiDemo安装Swashbuckle
应用程序入口
using System; using System.Collections.Generic; using System.Diagnostics; using System.Linq; using System.ServiceProcess; using System.Text; using System.Threading.Tasks; namespace WebApiHost { static class Program { /// <summary> /// 应用程序的主入口点。 /// </summary> static void Main(string[] args) { RunDebug(); StartService(); } private static void StartService() { ServiceBase[] ServicesToRun; ServicesToRun = new ServiceBase[] { new WebApiHostService() }; ServiceBase.Run(ServicesToRun); } [Conditional("DEBUG")] private static void RunDebug() { new WebApiHostService().Start(); Console.WriteLine("启动成功"); Console.ReadLine(); } } }