windows service承載的web api宿主搭建(Microsoft.Owin+service)


今天突然想起改良一下以前搭建的“windows service承載的web api”服務,以前也是直接引用的類庫,沒有使用nuget包,時隔幾年應該很舊版本了吧。所以本次把需要nuget獲取的包記錄一下。

<?xml version="1.0" encoding="utf-8"?>
<packages>
  <package id="log4net" version="2.0.8" targetFramework="net461" />
  <package id="Microsoft.AspNet.WebApi.Client" version="5.2.7" targetFramework="net461" />
  <package id="Microsoft.AspNet.WebApi.Core" version="5.2.7" targetFramework="net461" />
  <package id="Microsoft.AspNet.WebApi.Owin" version="5.2.7" targetFramework="net461" />
  <package id="Microsoft.Owin" version="4.0.0" targetFramework="net461" />
  <package id="Microsoft.Owin.Host.SystemWeb" version="4.0.0" targetFramework="net461" />
  <package id="Microsoft.Owin.Hosting" version="4.0.0" targetFramework="net461" />
  <package id="Newtonsoft.Json" version="12.0.1" targetFramework="net461" />
  <package id="Owin" version="1.0" targetFramework="net461" />
</packages>

 還有幾點需要注意一下:

1、設定webapi僅僅使用json格式

using System;
using System.Collections.Generic;
using System.Net.Http;
using System.Net.Http.Formatting;
using System.Net.Http.Headers;

namespace NetMiddlewareSvr
{
    /// <summary>
    /// Json格式頭部類
    /// </summary>
    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;
        }
    }
}

2、修改默認路由規則:

using Owin;
using System.Net.Http.Formatting;
using System.Web.Http;

namespace NetMiddlewareSvr
{
    public class RegisterRoutesStartup
    {
        public void Configuration(IAppBuilder appBuilder)
        {
            HttpConfiguration config = new HttpConfiguration();
            //自定義路由
            config.Routes.MapHttpRoute(
              name: "CustomApi",
              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);
        }
    }
}

3、啟動監聽

 public partial class NetMiddwareService : ServiceBase
    {
        private IDisposable hostObject;
        public NetMiddwareService()
        {
            InitializeComponent();
        }

        protected override void OnStart(string[] args)
        {
            hostObject = hostObject = WebApp.Start<RegisterRoutesStartup>("http://" + "127.0.0.1" + ":5990");
        }

        protected override void OnStop()
        {
        }
    }

 補充一下nuget的順序:Microsoft.Owin->Microsoft.Owin.Hosting->Microsoft.AspNet.WebApi.Core,剩下的是依賴包自動導入的。當然log4net和Newtonsoft.Json不是owin的依賴包

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM