2020-06-23

ASP.NET Web API Demo OwinSelfHost 自宿主 Swagger Swashbuckle 在线文档

新建Web API工程

 

选Empty,勾选Web API,不要选择Web API,那样会把MVC勾上,这里不需要MVC

Web API工程属性

 

  新建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();  } }}
View Code

 启动Web API服务

using Microsoft.Owin.Hosting;using System;using System.Collections.Generic;using System.ComponentModel;using System.Configuration;using System.Data;using System.Diagnostics;using System.Linq;using System.ServiceProcess;using System.Text;using System.Threading;using System.Threading.Tasks;using Utils;namespace WebApiHost{ public partial class WebApiHostService : ServiceBase {  #region 构造函数  public WebApiHostService()  {   InitializeComponent();  }  

No comments:

Post a Comment