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. KMP概念上小结

    kmp算法的时间复杂度是O(m+n) 主要作用: 1.最长公共前后缀问题 2.原串中含有几个模式串问题 3.循环节问题

  2. li中下的a元素的字超出了li的宽度

    网站搬迁,给你带来的不便敬请谅解! http://www.suanliutudousi.com/2017/10/21/li%E4%B8%AD%E4%B8%8B%E7%9A%84a%E5%85%83%E ...

  3. java-day26

    ## DOM简单学习:为了满足案例要求     * 功能:控制html文档的内容     * 获取页面标签(元素)对象:Element         * document.getElementByI ...

  4. jquery插件小集合

    一.滑动轮播插件Swiper Swiper官网http://www.swiper.com.cn/, 这款插件移动端,pc端均试用 二.jquery-tmpl----让你从拼接字符串中解放出来 官方下载 ...

  5. mysql数据库 --数据类型、约束条件

    今日内容 表的详细使用 1.创建表的完成语法 2.字段类型 整型.浮点型.字符类型.日期类型.枚举与集合类型 3.约束条件 primary key.unique.not null.default 一. ...

  6. 前端 Vue

    0.渐进式 JavaScript 框架 通过对框架的了解与运用程度,来决定其在整个项目中的应用范围,最终可以独立以框架方式完成整个web前端项目 一.Vue介绍 1.什么是Vue 可以独立完成前后端分 ...

  7. JS实现菜单滚动到一定高度后固定

    在有些网页中我们会发现会有这样的现象:某个div会随着屏幕的滚动达到一定高度的时候位置就固定下来了.例如一下导航条: 那么这里就需要用到JS的逻辑方法来实现了. html <div id=&qu ...

  8. Bootstrap快速入门并且建立常用模板

    对于移动端,能够快速搭建网站,操作相对简单 ,更容易掌握,这篇文章就介绍一下BootStrap 一  什么是Bottstrap 一个前端开发的框架,Bootstrap,来自 Twitter,是目前很受 ...

  9. flyway 管理数据库版本

    Flyway 和 Liquibase 都是 Java 项目中常用的 DB migration 工具, 从使用简便性看,Flyway 比 Liquibase 更简单, 从 github 的 star 数 ...

  10. PHP ftp_rename() 函数

    定义和用法 ftp_rename() 函数重命名 FTP 服务器上的文件或目录. 如果成功,该函数返回 TRUE.如果失败,则返回 FALSE. 语法 ftp_rename(ftp_connectio ...