http://owin.org/

Owin 定义了webserver和webapplication之间的标准接口,目标就是为了解耦webapplication对webserver的依赖,

就是说以后可以轻松的建立一个轻量级的HttpServer,

1.Basic Sample  Self Host

下面建立一个Basic Self Host Http Server Via Owin ,全部功能就是获取客户端的Http请求,然后做出回应,当发生Unhandled Exception的时候,会自动跳转到错误页,

Step1:Install-Package Microsoft.Owin.SelfHost

Step2:Configuration in Startup.css

using System;
using System.Threading.Tasks;
using Microsoft.Owin;
using Owin; namespace OwinSelfHostBasic
{
public class Startup
{
public void Configuration(IAppBuilder app)
{
app.UseErrorPage(); // For more information on how to configure your application, visit http://go.microsoft.com/fwlink/?LinkID=316888
app.Run(context => {
if (context.Request.Path.Value == "/fail")
{
throw new Exception("Random exception");
}
context.Response.ContentType = "text/plain";
return context.Response.WriteAsync("Hello, world.");
});
}
}
}

app.UseErrorPage();

为WebApplication指定错误页,

app.Run加入响应逻辑

Step3:指定监听端口

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks; namespace OwinSelfHostBasic
{
class Program
{
static void Main(string[] args)
{
using (Microsoft.Owin.Hosting.WebApp.Start<Startup>("http://localhost:9000"))
{
Console.WriteLine("Press [enter] to quit...");
Console.ReadLine();
}
}
}
}

OK,一切准备就绪,启动这个Console程序,请求 http://localhost:9000/ 这个地址,看起来比NodeJS更加方便。

2.Self Host Web API using Owin

Step1:Install-Package Microsoft.AspNet.WebApi.OwinSelfHost

Step2:Startup.cs

using Owin;
using System.Web.Http; namespace OwinWebAPISelfHost
{
public class Startup
{
// This code configures Web API. The Startup class is specified as a type
// parameter in the WebApp.Start method.
public void Configuration(IAppBuilder appBuilder)
{
// Configure Web API for self-host.
HttpConfiguration config = new HttpConfiguration();
config.Routes.MapHttpRoute(
name: "DefaultApi",
routeTemplate: "api/{controller}/{id}",
defaults: new { id = RouteParameter.Optional }
); appBuilder.UseWebApi(config);
}
}
}

Step3:APIController

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Web.Http; namespace OwinWebAPISelfHost
{
public class ValuesController : ApiController
{
// GET api/values
public IEnumerable<string> Get()
{
return new string[] { "value1", "value2" };
}
}
}

Step4:设置监听端口

using Microsoft.Owin.Hosting;
using System;
using System.Net.Http; namespace OwinWebAPISelfHost
{
public class Program
{
static void Main()
{
string baseAddress = "http://localhost:9000/"; // Start OWIN host
using (WebApp.Start<Startup>(url: baseAddress))
{
// Create HttpCient and make a request to api/values
HttpClient client = new HttpClient(); var response = client.GetAsync(baseAddress + "api/values").Result; Console.WriteLine(response);
Console.WriteLine(response.Content.ReadAsStringAsync().Result); Console.ReadLine();
} }
}
}

Step5:启动Console程序,这里我们看到返回的是Json格式的数据,当我们用火狐浏览器进行请求,会发现返回的是XML格式的数据,因为火狐浏览器默认的Accept为XML

Owin Self Host的更多相关文章

  1. 用Web api /Nancy 通过Owin Self Host简易实现一个 Http 服务器

    过去做 端游的Http 服务器 用的WebApi 或者Mvc架构,都是放在iis...而我已经是懒出一个地步,并不想去配iis,或者去管理iis,所以我很喜欢 Self host 的启动方式. C#做 ...

  2. 基于空项目模板创建使用Owin来host的WebApi项目

    首先创建一个空的web项目,如下图所示: 项目创建成功以后,安装下面三个package. Install-Package Microsoft.AspNet.WebApi -Version 5.2.2I ...

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

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

  4. OWIN 中 K Commands(OwinHost.exe)与 Microsoft.AspNet.Hosting 的角色问题

    问题详情:K Commands(OwinHost.exe)是不是 OWIN 中的 Host 角色?如果是,那 Microsoft.AspNet.Hosting 对应的是 OWIN 中的哪个角色? OW ...

  5. ASP.NET Identity(处理身份数据存储) 与 OWIN主机(实现katana验证授权)原理概括

    ASP.NET Identity 是4.5中引入的,支持Clamis(声明)式样登陆[即认证和授权分开模式],结合owin可以实现cookie加密等功能. 1.ASP.NET Identity架构框架 ...

  6. 【C#】使用OWIN创建Web API

    OWIN的介绍 OWIN 的全称是 "Open Web Interface for .NET", OWIN 在 .NET Web 服务器和 .NET Web 应用之间定义了一套标准 ...

  7. 新建一个self hosted Owin+ SignalR Project(1)

    OWIN是Open Web Server Interface for .Net 的首字母缩写,他的定义如下: OWIN在.NET Web Server 与Web Application之间定义了一套标 ...

  8. Web Api 入门实战 (快速入门+工具使用+不依赖IIS)

    平台之大势何人能挡? 带着你的Net飞奔吧!:http://www.cnblogs.com/dunitian/p/4822808.html 屁话我也就不多说了,什么简介的也省了,直接简单概括+demo ...

  9. 如何安装并简单的使用OwinHost——Katana

    微软OWIN的提出必然会引起一场风暴,而我们作为C#阵营中一份子,自然免不了会卷入其中.OWIN是什么东西,我在这里就不解析了,还不知道是OWIN是什么的读者请打开浏览器,然后搜索即可,中文的英文的应 ...

随机推荐

  1. A*算法

    A*在游戏设计中有它很典型的用法,是人工智能在游戏中的代表. A*算法在人工智能中是一种典型的启发式搜索算法,为了说清楚 A*算法,我看还是先说说何谓启发式算法. 一.何谓启发式搜索算法: 在说它之前 ...

  2. 篇二:MySQL存储过程

    目的:写一个存储过程,往数据库中插入几百条数据,作为识别码给别人用(这里我觉得和验证码的功能相似) BEGIN ); ); ) ; ); ); ; ; while count <= insert ...

  3. Ubuntu14.04无法在var/www内新建文档

    /var/www文件夹的所有者属于www-data用户组. 要想用你自己的帐号在/var/www里面创建文件和文件夹,最好的办法是把自己的帐号纳入到www-data用户组中. 命令:sudo user ...

  4. 知乎一道前端面试题详解,关于this的使用

    请说明要输出正确的myName的值要如何修改程序?并解释原因 foo = function(){ this.myName = "Foo function."; } foo.prot ...

  5. layer.open打开iframe页面的调用父页面方法及关闭

    //调用父类方法 window.parent.exportData($('#shownum').val(),$('#splitstr').val()); //关闭iframe页面var index = ...

  6. kafka集群安装部署

    kafka集群安装 使用的版本 系统:centos6.5 centos6.7 jdk:1.7.0_79 zookeeper:3.4.9 kafka:2.10-0.10.1.0 一.环境准备[只列,不具 ...

  7. [bigdata] hadoop 参数配置解析

    ResourceManager相关配置参数 yarn-site.xml 中配置 yarn.resourcemanager.address ResourceManager 对客户端暴露的地址.客户端通过 ...

  8. 【基础】MVC路由规则

    一.RouteData解析过程 在ASP.NET MVC中,服务器收到来自客户端的请求后,会经过一些列的处理拿到请求的数据,比如在Pipeline 管线事件中,通过订阅适当的事件,将HttpConte ...

  9. Ajax基础

    1 概要 异步JavaScript和XML(Asynchronous Javascript And XML,Ajax)就是使用js来收发来自web服务器的数据,且无需重载整个页面的技术. 注 :xml ...

  10. POJ1390Blocks(DP+好题+抽空再来理解理解)

    Blocks Time Limit: 5000MS   Memory Limit: 65536K Total Submissions: 4744   Accepted: 1930 Descriptio ...