windows service承载的web api宿主搭建(Microsoft.Owin+service)
今天突然想起改良一下以前搭建的“windows service承载的web api”服务,以前也是直接引用的类库,没有使用nuget包,时隔几年应该很旧版本了吧。所以本次把需要nuget获取的包记录一下。
1 2 3 4 5 6 7 8 9 10 11 12 | <?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的依赖包
作者:数据酷软件
出处:https://www.cnblogs.com/datacool/p/datacool2019001.html
关于作者:20年编程从业经验,持续关注MES/ERP/POS/WMS/工业自动化
本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明。
联系方式: qq:71008973;wx:6857740733
基于人脸识别的考勤系统 地址: https://gitee.com/afeng124/viewface_attendance_ext
自己开发安卓应用框架 地址: https://gitee.com/afeng124/android-app-frame
WPOS(warehouse+pos) 后台演示地址: http://47.239.106.75:8080/
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· 单线程的Redis速度为什么快?
· SQL Server 2025 AI相关能力初探
· 展开说说关于C#中ORM框架的用法!
· AI编程工具终极对决:字节Trae VS Cursor,谁才是开发者新宠?
2014-01-15 sql server 行转列
2013-01-15 .net compact framework2.0 Wince智能设备开发项目经验分享 .net拖空间之进阶篇