zoukankan      html  css  js  c++  java
  • 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的依赖包

  • 相关阅读:
    @Value注解读取配置,给静态变量赋值
    SpringBoot中非Controller类调用service方法出现null空指针
    nacos多环境配置
    spring项目将配置迁移至nacos
    链表的翻转(java)
    java.lang.IllegalAccessError: tried to access method org.apache.poi.util.POILogger.log from class org.apache.poi.openxml4j.opc.ZipPackage
    SQL SERVER 存储过程将SELECT 数据集赋值给新表某个字段
    SQLServerException:将截断字符串或二进制数据
    获取mysql数据库表表头所有字段
    软工实践个人总结
  • 原文地址:https://www.cnblogs.com/datacool/p/datacool2019001.html
Copyright © 2011-2022 走看看