Window sevice +OWIN+webapi
webapi正常都托管在iis上,这里引入owin,将其托管在window服务上。
第一步:创建一个服务,这里就不多描述。
第二步:引入OWIN
2.1引入bll

2.2加入api路由配置
public class Startup
{
public void Configuration(IAppBuilder appBuilder)
{
HttpConfiguration config = new HttpConfiguration(); config.MapHttpAttributeRoutes(); config.Routes.MapHttpRoute(
name: "DefaultApi",
routeTemplate: "api/{controller}/{id}",
defaults: new { id = RouteParameter.Optional }
); //添加允许跨域
var CorsConfig = new EnableCorsAttribute(origins: "*", headers: "*", methods: "*")
{
PreflightMaxAge = ,
SupportsCredentials = true
};
config.EnableCors(CorsConfig); appBuilder.UseWebApi(config); }
}
2.3监听ip和端口
protected override void OnStart(string[] args)
{
_server = WebApp.Start<Startup>(url: "http://192.168.200.84:2000/");
Console.WriteLine("Service start success...");
}
第三步:加入webapi接口
namespace WebSocketService.Controllers
{
public class HomeController : HomeControllerBase
{
}
}
基类:
namespace GameCommon.Controllers
{
public class HomeControllerBase : ApiController
{
[HttpGet]
public object Get()
{
return new { Age=,Name="Holle world"};
} [HttpPost]
public object Post()
{
return new { Age = , Name = "Holle world" };
}
}
}
我这里将接口单独提取了一个DLL文件来处理。
项目结构:

另外这里再贴上webSocket的代码:
using GameCommon.Controllers;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks; namespace WebSocketService.Controllers
{
public class ConnectController:ConnectControllerBase
{
}
}
基类:
using Microsoft.Owin;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Net.WebSockets;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Web.Http; namespace GameCommon.Controllers
{
using WebSocketAccept = System.Action<System.Collections.Generic.IDictionary<string, object>,System.Func<
System.Collections.Generic.IDictionary<string, object>, System.Threading.Tasks.Task>>;
public class ConnectControllerBase : ApiController
{
[HttpGet]
public HttpResponseMessage Connect()
{
try
{
if (Request.Method != HttpMethod.Get)
{
return new HttpResponseMessage(HttpStatusCode.NotFound);
} IOwinContext owinContext = Request.GetOwinContext();
WebSocketAccept acceptToken = owinContext.Get<WebSocketAccept>("websocket.Accept");
if (acceptToken != null)
{
acceptToken(null, ProcessSocketConnection);
}
else
{
return new HttpResponseMessage(HttpStatusCode.BadRequest);
}
return new HttpResponseMessage(HttpStatusCode.SwitchingProtocols);
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
return null;
}
} public async Task ProcessSocketConnection(IDictionary<string, object> wsEnv)
{
WebSocket wSocket = ((HttpListenerWebSocketContext)wsEnv["System.Net.WebSockets.WebSocketContext"]).WebSocket; while (true)
{
ArraySegment<byte> buffer = new ArraySegment<byte>(new byte[]);
WebSocketReceiveResult receivedResult; receivedResult = await wSocket.ReceiveAsync(buffer, CancellationToken.None); if (receivedResult.MessageType == WebSocketMessageType.Close)
{
await wSocket.CloseAsync(WebSocketCloseStatus.Empty, string.Empty, CancellationToken.None);
break;
} if (wSocket.State == WebSocketState.Open)
{
string receivedJson = Encoding.UTF8.GetString(buffer.Array, , receivedResult.Count);
}
} }
}
}
Window sevice +OWIN+webapi的更多相关文章
- ASP.NET Linux部署(2) - MS Owin + WebApi + Mono + Jexus
ASP.NET Linux部署(2) - MS Owin + WebApi + Mono + Jexus 本文承接我的上一篇博文: ASP.NET 5 Linux部署,那篇文章主要是针对最新的ASP. ...
- (转)基于OWIN WebAPI 使用OAuth授权服务【客户端模式(Client Credentials Grant)】
适应范围 采用Client Credentials方式,即应用公钥.密钥方式获取Access Token,适用于任何类型应用,但通过它所获取的Access Token只能用于访问与用户无关的Open ...
- 基于OWIN WebAPI 使用OAuth授权服务【客户端验证授权(Resource Owner Password Credentials Grant)】
适用范围 前面介绍了Client Credentials Grant ,只适合客户端的模式来使用,不涉及用户相关.而Resource Owner Password Credentials Grant模 ...
- 基于OWIN WebAPI 使用OAuth授权服务【客户端模式(Client Credentials Grant)】
适应范围 采用Client Credentials方式,即应用公钥.密钥方式获取Access Token,适用于任何类型应用,但通过它所获取的Access Token只能用于访问与用户无关的Open ...
- 【干货】基于Owin WebApi 使用OAuth2进行客户端授权服务
前言:采用Client Credentials方式,即密钥key/password,场景一般是分为客户端限制必须有权限才能使用的模块,这和微信公众号开放平台很类似. 让用户通过客户端去获取自己的tok ...
- Owin WebAPI上传文件
Owin是微软出了几年的东东了,一直没时间学习.大概了解了下,是一个脱离IIS环境,快速搭建WebAPI服务的东西. 刚好想尝试下尽量脱离IIS创建简单快捷配置的项目,就是用了Nginx+Owin的模 ...
- 转:winform_webApiSelfHost及 OWIN WebAPI Service
winform_webApiSelfHost 窗本构造函数中添加以下代码: var baseAddress = ConfigurationManager.AppSettings["baseA ...
- Owin + WebApi + OAuth2 搭建授权模式(授权码模式 Part I)
绪 最近想要整理自己代码封装成库,也十分想把自己的设计思路贴出来让大家指正,奈何时间真的不随人意. 想要使用 OWIN 做中间件服务,该服务中包含 管线.授权 两部分.于是决定使用 webapi .O ...
- 基于OWIN WebAPI 使用OAUTH2授权服务【授权码模式(Authorization Code)】
之前已经简单实现了OAUTH2的授权码模式(Authorization Code),但是基于JAVA的,今天花了点时间调试了OWIN的实现,基本就把基于OWIN的OAUHT2的四种模式实现完了.官方推 ...
随机推荐
- CoreDump analyse
1 Found core dump files: -rw------- 1 root root 1019645952 Dec 14 01:56 core.7002-rw------- 1 root r ...
- 4.2.1 Vector bit-select and part-select addressing
Frm:IEEE Std 1364™-2001, IEEE Standard Verilog® Hardware Description Language Bit-selects extract a ...
- 【转】手机web页面制作时的注意事项
一.手机页面的标准头规范 字符编码使用utf-8:指定页面手机内存缓存中的存储时间段 device-width:通知浏览器使用设备的宽度作为可视区的宽度 initial-scale - 初始的缩放比例 ...
- 优雅地使用 VSCode 来编辑 vue 文件
javascript visual-studio-code vue.js 当然 vscode 对 vue 也不是原生支持的,今天来扒一扒如何配置 vscode 以便优雅地编辑 vue 文件 先来扒一扒 ...
- 关于redis闪退的案例
我需要恢复之前备份的文件:dump.rdb,文件大小2.2G 于是将dump.rdb放在redis的安装目录下,然后启动redis. ./redis-server redis.conf 然后我在另一个 ...
- 【转】console.dir()和console.log()的区别
原文链接:https://blog.csdn.net/ky1in93/article/details/80828499 console对象详解:https://segmentfault.com/a/1 ...
- vue实现curd功能
一.实现效果 二.实现 (一)实现增加用户功能 Vuserlist组件中 <template> <div class="panel panel-default"& ...
- ThreadLocal知识点
ThreadLocal是什么 ThreadLocal 表面上看他是和多线程,线程同步有关的一个工具类,但其实他与线程同步机制无关.线程同步机制是多个线程共享同一个变量,而ThreadLocal是为每个 ...
- WPF 模仿 UltraEdit 文件查看器系列一 用户控件
WPF 模仿 UltraEdit 文件查看器系列一 用户控件 运行环境:Win10 x64, NetFrameWork 4.8, 作者:乌龙哈里,日期:2019-05-10 章节: 起步 添加用户控件 ...
- Q: 微信小程序登录
这里使用的mpvue 第一步组件DOM部分 /pages/index <button class="app_btn" open-type="getUserInfo& ...