本文章僅作者記錄學習所用。
本文章主要內容原文地址爲: https://www.cnblogs.com/datacool/p/datacool2019001.html
//====================================================================================
1.首先創建 WindowsService 項目。
2.(雙擊Service文件)在Service文件中右鍵點擊添加安裝程序
3.在serviceProcessInstaller1 上右鍵屬性並設置Account爲LocalSystem
4.在serviceInstaller1 上右鍵屬性設置服務屬性
5.在程序的生成目錄下需要創建兩個bat文件用於安裝和卸載服務
安裝文件內容
cd /d %~dp0
%SystemRoot%\Microsoft.NET\Framework\v4.0.30319\installutil.exe /u WindowsService1.exe
%SystemRoot%\Microsoft.NET\Framework\v4.0.30319\installutil.exe WindowsService1.exe
Net Start Service1
@echo off
set /p id="Press any key to continue..."
卸載文件內容
cd /d %~dp0
%SystemRoot%\Microsoft.NET\Framework\v4.0.30319\installutil.exe /u WindowsService1.exe
set /p id="Press any key to continue..."
將兩個文件分別保存爲 bat 等待使用。
6.在通過Nuget包管理器安裝如下組件
Microsoft.AspNet.WebApi.Owin
Microsoft.Owin.Hosting
Microsoft.Owin.Host.HttpListener
其他組件會隨着這三個組件的安裝自動安裝。
7.創建 JsonContentNegotiator.cs
public class JsonContentNegotiator : IContentNegotiator
{
private readonly JsonMediaTypeFormatter _jsonFormatter;
public JsonContentNegotiator(JsonMediaTypeFormatter formatter)
{
_jsonFormatter = formatter;
}
public ContentNegotiationResult Negotiate(Type type, HttpRequestMessage request, IEnumerable<MediaTypeFormatter> formatters)
{
var result = new ContentNegotiationResult(_jsonFormatter, new MediaTypeHeaderValue("application/json"));
return result;
}
}
8.創建 RegisterRoutesStartup.cs
public class RegisterRoutesStartup
{
public void Configuration(IAppBuilder appBuilder)
{
HttpConfiguration config = new HttpConfiguration();
//自定義路由
config.Routes.MapHttpRoute(
name: "DefaultApi",
routeTemplate: "api/{controller}/{action}/{id}",
defaults: new { id = RouteParameter.Optional }
);
//只響應Json請求
var jsonFormatter = new JsonMediaTypeFormatter();
config.Services.Replace(typeof(IContentNegotiator), new JsonContentNegotiator(jsonFormatter));
appBuilder.UseWebApi(config);
}
}
9.在服務(Service1)中啓動WebApi
public partial class Service1 : ServiceBase
{
private IDisposable hostObject;
public Service1()
{
InitializeComponent();
}
protected override void OnStart(string[] args)
{
//啓動WebApi
hostObject = WebApp.Start<RegisterRoutesStartup>("http://localhost:8085");
}
protected override void OnStop()
{
//注意釋放
hostObject.Dispose();
}
}
10.創建一個 ApiController 用於測試
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Web.Http;
namespace WindowsService1.WebAPI
{
public class TestAPIController : ApiController
{
[HttpGet]
public string Test1()
{
return "WebApi from Windows Service";
}
[HttpGet]
public Usersx Test2()
{
return new Usersx { Name = "測試", Remark = "這是測試數據內容" };
}
}
public class Usersx
{
public string Name { get; set; }
public string Remark { get; set; }
}
}
//====================================================================================
編輯完代碼後進行項目生成。
然後進入生成目錄,右鍵以管理員身份運行 用於服務安裝的 bat 文件。
顯示成功後 在瀏覽器中輸入
http://localhost:8085/api/TestAPI/Test1
http://localhost:8085/api/TestAPI/Test2
就可以看到返回的結果了。