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的更多相关文章

  1. ASP.NET Linux部署(2) - MS Owin + WebApi + Mono + Jexus

    ASP.NET Linux部署(2) - MS Owin + WebApi + Mono + Jexus 本文承接我的上一篇博文: ASP.NET 5 Linux部署,那篇文章主要是针对最新的ASP. ...

  2. (转)基于OWIN WebAPI 使用OAuth授权服务【客户端模式(Client Credentials Grant)】

    适应范围 采用Client Credentials方式,即应用公钥.密钥方式获取Access Token,适用于任何类型应用,但通过它所获取的Access Token只能用于访问与用户无关的Open ...

  3. 基于OWIN WebAPI 使用OAuth授权服务【客户端验证授权(Resource Owner Password Credentials Grant)】

    适用范围 前面介绍了Client Credentials Grant ,只适合客户端的模式来使用,不涉及用户相关.而Resource Owner Password Credentials Grant模 ...

  4. 基于OWIN WebAPI 使用OAuth授权服务【客户端模式(Client Credentials Grant)】

    适应范围 采用Client Credentials方式,即应用公钥.密钥方式获取Access Token,适用于任何类型应用,但通过它所获取的Access Token只能用于访问与用户无关的Open ...

  5. 【干货】基于Owin WebApi 使用OAuth2进行客户端授权服务

    前言:采用Client Credentials方式,即密钥key/password,场景一般是分为客户端限制必须有权限才能使用的模块,这和微信公众号开放平台很类似. 让用户通过客户端去获取自己的tok ...

  6. Owin WebAPI上传文件

    Owin是微软出了几年的东东了,一直没时间学习.大概了解了下,是一个脱离IIS环境,快速搭建WebAPI服务的东西. 刚好想尝试下尽量脱离IIS创建简单快捷配置的项目,就是用了Nginx+Owin的模 ...

  7. 转:winform_webApiSelfHost及 OWIN WebAPI Service

    winform_webApiSelfHost 窗本构造函数中添加以下代码: var baseAddress = ConfigurationManager.AppSettings["baseA ...

  8. Owin + WebApi + OAuth2 搭建授权模式(授权码模式 Part I)

    绪 最近想要整理自己代码封装成库,也十分想把自己的设计思路贴出来让大家指正,奈何时间真的不随人意. 想要使用 OWIN 做中间件服务,该服务中包含 管线.授权 两部分.于是决定使用 webapi .O ...

  9. 基于OWIN WebAPI 使用OAUTH2授权服务【授权码模式(Authorization Code)】

    之前已经简单实现了OAUTH2的授权码模式(Authorization Code),但是基于JAVA的,今天花了点时间调试了OWIN的实现,基本就把基于OWIN的OAUHT2的四种模式实现完了.官方推 ...

随机推荐

  1. 2019杭电多校第一场hdu6579 Operation(线性基)

    Operation 题目传送门 解题思路 把右边的数尽量往高位放,构造线性基的时候同时记录其在原序列中的位置,在可以插入的时候如果那个位置上存在的数字的位置比新放入的要小,就把旧的往后挤.用这种发现构 ...

  2. CH1201 最大子序和 (单调队列)

    题目链接: AcWing 牛客 题目描述 输入一个长度为n的整数序列,从中找出一段不超过m的连续子序列,使得整个序列的和最大. 例如 1,-3,5,1,-2,3 当m=4时,S=5+1-2+3=7 当 ...

  3. centos7下利用nfs搭建wordpress

    拓扑环境 web1 192.168.198.110 web2 192.168.198.120 mysql 192.168.198.130 DNS 192.168.198.10 NFS 192.168. ...

  4. hadoop命令行

    持续更新中................ 1. 设置目录配额 命令:hadoop dfsadmin -setSpaceQuota 样例:hadoop dfsadmin -setSpaceQuota ...

  5. python学习9—文件基本操作与高级操作

    python学习9—文件基本操作与高级操作 1. 文件基本操作 打开文件,获得文件句柄:f = open('filename',encoding='utf-8'),open会查询操作系统的编码方式,并 ...

  6. B-彻底删除卸载Ubuntu中的MySQL并重新安装(已验证)

    Ubuntu-16.04,MySQL-5.7,寻找多篇有关如何彻底卸载删除MySQL的博文, 最终验证下面转发博文真实有效,推荐! https://www.jianshu.com/p/c76b31df ...

  7. VisualStuido中将C#脚本封装打包DLL并调用

    DLL (Dynamic Link Library)---动态链接库 首先了解下使用DLL的优势,程序运行时不用加载所有代码,只有运行到引用时,才从DLL库中取出.并且使用DLL文件还可以减小程序体积 ...

  8. forEarch 和 for in

    forEarch 遍历数组,遍历的过程中不能被终止,必须每一个值遍历一遍后才能停下来,for  in遍历对象中的属性 代码: <!DOCTYPE html> <html lang=& ...

  9. JS分支结构与循环结构

    1.分支结构 ①if语句 语法结构 if (/* 条件表达式 */) { // 执行语句 } ​ if (/* 条件表达式 */){ // 成立执行语句 } else { // 否则执行语句 } ​ ...

  10. kafka的简单命令

    启动kafka自带的zookeeper ./bin/zookeeper-server-start.sh config/zookeeper.properties & 启动kafka ./bin/ ...