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的四种模式实现完了.官方推 ...
随机推荐
- 拾遗:Git 常用操作回顾
温故而知新,可以为师矣. Git 布局 工作区---->暂存区---->本地仓库---->远程仓库 Create Repository git init PATH git add P ...
- 5、cesium点击面高亮事件
cesium点击面高亮事件 主要功能:比如你加载了json.geojson或者topojson的数据.加载出来后,分为很多个面,现在要实现点击一个面,这个面变颜色:再点击另一个面,另一个面高亮,之前的 ...
- Java各版本的含义
JavaSE(Java Standard Edition):标准版,定位在个人计算机上的应用.这个版本是Java平台的核心,它提供了非常丰富的API来开发一般个人计算机上的应用程序,包括用户界面接口A ...
- js 中typeof 检测数据类型的时候需要注意的小细节
博客搬迁给你带来的不便,敬请谅解! http://www.suanliutudousi.com/2017/10/26/typeof-%E6%A3%80%E6%B5%8B%E6%95%B0%E6%8D% ...
- java-day27
## Bootstrap: 1. 概念: 一个前端开发的框架,Bootstrap,来自 Twitter,是目前很受欢迎的前端框架.Bootstrap 是基于 HTML.CSS.JavaScri ...
- 如何将sql查询出的列名用注释代替?
如何将sql查询出的列名用注释代替? 大家正常的工作的时候,会有这样的要求,客户想要看下原始数据,但是呢.前台导出又麻烦,这时候只能从数据库拷贝出来一份.但是呢,数据库里面的字段客户又看不明白,只能用 ...
- Linux 中 sqlite3 基本操作
https://www.runoob.com/sqlite/sqlite-commands.html 一 .linux 下安装数据库和创建一个数据库 1. Linux 下安装sqlite3 需要两个命 ...
- wp api jwt 403 (Forbidden) -- JWT is not configurated properly, please contact the admin
需要在 wp-config.php 文件中配置 define('JWT_AUTH_SECRET_KEY', 'your-top-secrect-key'); 參考 403 (Forbidden) -- ...
- python代码自动补全
牛逼了!Python代码补全利器,提高效率告别996! Python之禅 Python之禅 微信号 VTtalk 功能介绍 人生苦短,我用Python,这里是一名老程序员分享Python技术的地方,欢 ...
- 【重磅来袭】阿里小程序IDE上线8大功能
时隔两个月,10月10日阿里小程序IDE上线了uni-app 跨平台研发支持.预览和真机调试交互优化.预检测新增代码扫描等8项功能,进一步完善了阿里小程序IDE的功能池,给大家更好的开发体验和环境. ...