Owin Self Host
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的更多相关文章
- 用Web api /Nancy 通过Owin Self Host简易实现一个 Http 服务器
过去做 端游的Http 服务器 用的WebApi 或者Mvc架构,都是放在iis...而我已经是懒出一个地步,并不想去配iis,或者去管理iis,所以我很喜欢 Self host 的启动方式. C#做 ...
- 基于空项目模板创建使用Owin来host的WebApi项目
首先创建一个空的web项目,如下图所示: 项目创建成功以后,安装下面三个package. Install-Package Microsoft.AspNet.WebApi -Version 5.2.2I ...
- ASP.NET Linux部署(2) - MS Owin + WebApi + Mono + Jexus
ASP.NET Linux部署(2) - MS Owin + WebApi + Mono + Jexus 本文承接我的上一篇博文: ASP.NET 5 Linux部署,那篇文章主要是针对最新的ASP. ...
- OWIN 中 K Commands(OwinHost.exe)与 Microsoft.AspNet.Hosting 的角色问题
问题详情:K Commands(OwinHost.exe)是不是 OWIN 中的 Host 角色?如果是,那 Microsoft.AspNet.Hosting 对应的是 OWIN 中的哪个角色? OW ...
- ASP.NET Identity(处理身份数据存储) 与 OWIN主机(实现katana验证授权)原理概括
ASP.NET Identity 是4.5中引入的,支持Clamis(声明)式样登陆[即认证和授权分开模式],结合owin可以实现cookie加密等功能. 1.ASP.NET Identity架构框架 ...
- 【C#】使用OWIN创建Web API
OWIN的介绍 OWIN 的全称是 "Open Web Interface for .NET", OWIN 在 .NET Web 服务器和 .NET Web 应用之间定义了一套标准 ...
- 新建一个self hosted Owin+ SignalR Project(1)
OWIN是Open Web Server Interface for .Net 的首字母缩写,他的定义如下: OWIN在.NET Web Server 与Web Application之间定义了一套标 ...
- Web Api 入门实战 (快速入门+工具使用+不依赖IIS)
平台之大势何人能挡? 带着你的Net飞奔吧!:http://www.cnblogs.com/dunitian/p/4822808.html 屁话我也就不多说了,什么简介的也省了,直接简单概括+demo ...
- 如何安装并简单的使用OwinHost——Katana
微软OWIN的提出必然会引起一场风暴,而我们作为C#阵营中一份子,自然免不了会卷入其中.OWIN是什么东西,我在这里就不解析了,还不知道是OWIN是什么的读者请打开浏览器,然后搜索即可,中文的英文的应 ...
随机推荐
- C#实现Levenshtein distance最小编辑距离算法
Levenshtein distance,中文名为最小编辑距离,其目的是找出两个字符串之间需要改动多少个字符后变成一致.该算法使用了动态规划的算法策略,该问题具备最优子结构,最小编辑距离包含子最小编辑 ...
- C 语言学习 第12次作业总结
作业总结 本次课堂的内容为字符串相关的几个函数还有结构体. 字符串相关函数 在此之前的课程中,输入主要都是使用scanf这个函数.而在这节课上,冯老师讲解了字符串获取函数gets.在不需要控制符的情况 ...
- 【BZOJ 4580】【Usaco2016 Open】248
http://www.lydsy.com/JudgeOnline/problem.php?id=4580 区间dp,f(i,j)表示区间[i,j]全部合成一个数,这个数是多少. 可以归纳证明[i,j] ...
- 第一章 MYSQL的架构和历史
在读第一章的过程中,整理出来了一些重要的概念. 锁粒度 表锁(服务器实现,忽略存储引擎). 行锁(存储引擎实现,服务器没有实现). 事务的ACID概念 原子性(要么全部成功,要么全部回滚). 一致性 ...
- 使用stylelint对CSS/Sass做代码审查
对样式审查?很少人会这么做吧,但实际上开发者应该有这样的态度,尤其是不同团队多人开发时,这一点尤为重要. 在本文中,我将陈述两点:一是为什么我们需要对样式进行审查,二是如何将审查工具融合到整体的构建流 ...
- 分享一些平时测试用的sql payloads
1:BOOL SQLINJECTION \'"%df'%df" and 1=1 and 1=2' and '1'='1' and '1'='2" and "1& ...
- ElasticSearch中bulkProcesser使用
初次接触es,可能对增删改查很熟悉,以为能为得心应手,本次应用场景为 数据库变更一条记录,会触发更新es中的数据,每秒并发大概30条左右,测试环境一切工作正常(数据量较少),上线后发现日志中很多类似于 ...
- docker 配置操作指导
1.下载程序包 安装包 https://github.com/boot2docker/windows-installer/releases(这个地址国内下载很慢) 或这个:http://www.sof ...
- System.Data.OracleClient 需要 Oracle 客户端软件 8.1.7 或更高版本问题
出错的原因: 1.虽然报的是需要安装客户端8.1.7及以上版本,实际是由于.NET账户没有访问Oracle\bin文件夹的权限 2.在 Windows Server 2003/2008 或Window ...
- 【实践】Memcached实例解析
一.关于Memcached Memcached是一个自由开源的,高性能,分布式内存对象缓存系统. Memcached是一种基于内存的Key-Value存储,用来存储小块的任意数据(字符串.对象).这些 ...