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的四种模式实现完了.官方推 ...
随机推荐
- 2、Appium Desktop 使用介绍
1.appium运行界面介绍 默认显示监控的 host 和 port , 这和 Appium-Server 中是一致的. 2.点击 “Start Server V 1.7.2” 按钮启动服务,出现如 ...
- 1010 Radix (25 分)
Given a pair of positive integers, for example, 6 and 110, can this equation 6 = 110 be true? The an ...
- Codeforces 1172A Nauuo and Cards
题目链接:http://codeforces.com/problemset/problem/1172/A 题意:一共有2*n张牌,n张0,n张1到n.现在随机的n张(有0有数字)在手上,另n张再牌堆中 ...
- Javascript 面向对象之继承
本文参考书籍<<Javascript高级程序设计>> js继承方式:实现继承,主要依靠原型链实现. 原型链:基本思想:利用原型让一个引用类型继承另一个引用类型的属性和方法. 这 ...
- 7-vim-移动命令-02-行数跳转和上下翻页
1.行数跳转 命令 英文 功能 gg go 文件顶部 G GO 文件尾部 数字gg 移动到数字对应行数 数字G 移动到数字对应行数 :数字 移动到数字对应行数 2.屏幕移动 命令 英文 功 ...
- sqlserver 如何瞬间执行上万条数据
核心的内容是:使用自定义表类型 第一步:创建存储过程P_T1DeclareInfo_Upload_new 参数: T1DeclareInfo_UploadPNSN_Param 类型 T1Declar ...
- Spark Streaming设计
- Tools: windbg 使用指南
windbg使用 符号表C:\Symbols; SRV*C:\Symbols*http://msdl.microsoft.com/download/symbols 系统变量_NT_SYMBOL_PAT ...
- Dom编程优化
对Dom的访问代价是昂贵,在富网页应用中通常是性能的瓶颈,所以对Dom的优化十分重要. 一.访问和修改Dom元素 浏览器通常要求JavaScript和Dom实现保持独立的.例如,在Internet E ...
- HIVE的高级操作
二.视图 1.Hive 的视图和关系型数据库的视图区别 和关系型数据库一样,Hive 也提供了视图的功能,不过请注意,Hive 的视图和关系型数据库的数据还是有很大的区别: (1)只有逻辑视图,没有物 ...