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的依赖包
windows service承载的web api宿主搭建(Microsoft.Owin+service)的更多相关文章
- [angularjs] MVC + Web API + AngularJs 搭建简单的 CURD 框架
MVC + Web API + AngularJs 搭建简单的 CURD 框架 GitHub 地址:https://github.com/liqingwen2015/Wen.MvcSinglePage ...
- .Net Core 3.1浏览器后端服务(一) Web API项目搭建
一.前言 基于CefSharp开发的浏览器项目已有一段时间,考虑到后期数据维护需要Server端来管理,故开启新篇章搭建浏览器后端服务.该项目前期以梳理服务端知识为主,后期将配合CefSharp浏览器 ...
- integration asp.net web api with autofac and owin
There is an example project showing Web API in conjunction with OWIN self hosting https://github.com ...
- Web Service ,WCF以及Web API的对比
Web Service 1.基于SOAP和XML形式的返回数据. 2.只支出HTTP协议. 3.只能运行在IIS环境下. 4.不是开源的,但可以由任何支持xml的客户端下使用. WCF 1.基于SOA ...
- [转]Work With Odata in Web API: Create Your First Odata Service
本文转自:http://www.c-sharpcorner.com/UploadFile/dacca2/work-with-odata-in-web-api-create-your-first-oda ...
- (转)【ASP.NET Web API】Authentication with OWIN
概述 本文说明了如何使用 OWIN 来实现 ASP.NET Web API 的验证功能,以及在客户端与服务器的交互过程中,避免重复提交用户名和密码的机制. 客户端可以分为两类: JavaScript: ...
- 在ASP.NET Web API 2中使用Owin基于Token令牌的身份验证
基于令牌的身份验证 基于令牌的身份验证主要区别于以前常用的常用的基于cookie的身份验证,基于cookie的身份验证在B/S架构中使用比较多,但是在Web Api中因其特殊性,基于cookie的身份 ...
- 在ASP.NET Web API 2中使用Owin OAuth 刷新令牌(示例代码)
在上篇文章介绍了Web Api中使用令牌进行授权的后端实现方法,基于WebApi2和OWIN OAuth实现了获取access token,使用token访问需授权的资源信息.本文将介绍在Web Ap ...
- Web Api 宿主的搭建
首先我们要清楚一个概念,宿主.宿主是什么意思?先从了解一下Hosting开始吧! 有关Hosting的基础知识 Hosting是一个非常重要,但又很难翻译成中文的概念.翻译成:寄宿,大概能勉强地传达它 ...
随机推荐
- EF 批量 添加 修改 删除
1批量添加 db.T_Investigator.AddRange(list) 2批量删除 db.T_Investigator.RemoveRange(list) 3批量修改 for 循 ...
- Maven项目POM文件错误,提示“Plugin execution not covered by lifecycle configuration”的解决方案
一. 问题 Plugin execution not covered by lifecycle configuration: org.apache.maven.plugins:maven-depend ...
- js cookie存取
if(getCookie('guide') == 'true'){ window.location.href='' } else { setCookie('guide','true'); } func ...
- 浏览器与Node的事件循环(Event Loop)有何区别?
前言 本文我们将会介绍 JS 实现异步的原理,并且了解了在浏览器和 Node 中 Event Loop 其实是不相同的. 一.线程与进程 1. 概念 我们经常说 JS 是单线程执行的,指的是一个进程里 ...
- sublime 使用快捷键
Goto Anything 快捷键 Ctrl+P (支持模糊匹配) 1,查找文件 在查找框中输入文件目录(知道目录直接输入目录,不知道目录直接输入页面名称即可.支持模糊匹配) index.ht ...
- Web前端 页面功能——点击按钮返回顶部的实现方法
1. 最简单的静态返回顶部,点击直接跳转页面顶部,常见于固定放置在页面底部返回顶部功能 方法一:用命名锚点击返回到顶部预设的id为top的元素 html代码 <a href="#top ...
- alfs学习笔记-自动化构建lfs系统
我的邮箱地址:zytrenren@163.com欢迎大家交流学习纠错! 一名linux爱好者,记录构建Linux From Scratch的过程 经博客园-骏马金龙前辈介绍,开始接触学习lfs,用博客 ...
- linux学习笔记-shell-script相关知识
我的邮箱地址:zytrenren@163.com欢迎大家交流学习纠错! 一.shell script的执行方法 条件:shell脚本文件必须具备可读可执行权限 1.直接命令执行 (1)使用绝对路径执行 ...
- QT解析和组装json
json这个小朋友熟悉又陌生,今天给同学们好好讲讲QT是如何使用json的,一句话:简单 1.什么是json? A:json就是个<key,value>字符串 ①一个json对象 {&qu ...
- CentOS7 设置yum源
1.关闭防火墙 临时关闭防火墙 systemctl stop firewalld 永久防火墙开机自关闭 systemctl disable firewalld 临时打开防火墙 systemctl st ...