Open Web Interface for .NET (OWIN)在Web服务器和Web应用程序之间建立一个抽象层。OWIN将网页应用程序从网页服务器分离出来,然后将应用程序托管于OWIN的程序而离开IIS之外。
 
 
Use OWIN to Self-Host ASP.NET Web API 2
 
 

This tutorial shows how to host ASP.NET Web API in a console application, using OWIN to self-host the Web API framework.

Open Web Interface for .NET (OWIN) defines an abstraction between .NET web servers and web applications. OWIN decouples the web application from the server, which makes OWIN ideal for self-hosting a web application in your own process, outside of IIS.

Software versions used in the tutorial

You can find the complete source code for this tutorial at aspnet.codeplex.com.

Create a Console Application

On the File menu, click New, then click Project. From Installed Templates, under Visual C#, click Windows and then click Console Application. Name the project “OwinSelfhostSample” and click OK.

Add the Web API and OWIN Packages

From the Tools menu, click Library Package Manager, then click Package Manager Console. In the Package Manager Console window, enter the following command:

Install-Package Microsoft.AspNet.WebApi.OwinSelfHost

This will install the WebAPI OWIN selfhost package and all the required OWIN packages.

Configure Web API for Self-Host

In Solution Explorer, right click the project and select Add / Class to add a new class. Name the class Startup.

Replace all of the boilerplate code in this file with the following:

using Owin;
using System.Web.Http; namespace OwinSelfhostSample
{
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);
}
}
}

Add a Web API Controller

Next, add a Web API controller class. In Solution Explorer, right click the project and select Add / Class to add a new class. Name the classValuesController.

Replace all of the boilerplate code in this file with the following:

using System.Collections.Generic;
using System.Web.Http; namespace OwinSelfhostSample
{
public class ValuesController : ApiController
{
// GET api/values
public IEnumerable<string> Get()
{
return new string[] { "value1", "value2" };
} // GET api/values/5
public string Get(int id)
{
return "value";
} // POST api/values
public void Post([FromBody]string value)
{
} // PUT api/values/5
public void Put(int id, [FromBody]string value)
{
} // DELETE api/values/5
public void Delete(int id)
{
}
}
}

Start the OWIN Host and Make a Request Using HttpClient

Replace all of the boilerplate code in the Program.cs file with the following:

using Microsoft.Owin.Hosting;
using System;
using System.Net.Http; namespace OwinSelfhostSample
{
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();
}
}
}

Running the Application

To run the application, press F5 in Visual Studio. The output should look like the following:

StatusCode: 200, ReasonPhrase: 'OK', Version: 1.1, Content: System.Net.Http.StreamContent, Headers:
{
Date: Tue, 09 Jul 2013 18:10:15 GMT
Server: Microsoft-HTTPAPI/2.0
Content-Length: 19
Content-Type: application/json; charset=utf-8
}
["value1","value2"]

Additional Resources

An Overview of Project Katana

Host ASP.NET Web API in an Azure Worker Role

This article was originally created on July 9, 2013

 

Author Information

Kanchan Mehrotra

Use OWIN to Self-Host ASP.NET Web API 2的更多相关文章

  1. 为IIS Host ASP.NET Web Api添加Owin Middleware

    将OWIN App部署在IIS上 要想将Owin App部署在IIS上,只添加Package:Microsoft.OWIN.Host.SystemWeb包即可.它提供了所有Owin配置,Middlew ...

  2. [转] JSON Web Token in ASP.NET Web API 2 using Owin

    本文转自:http://bitoftech.net/2014/10/27/json-web-token-asp-net-web-api-2-jwt-owin-authorization-server/ ...

  3. JSON Web Token in ASP.NET Web API 2 using Owin

    In the previous post Decouple OWIN Authorization Server from Resource Server we saw how we can separ ...

  4. ASP.NET Web API 安全筛选器

    原文:https://msdn.microsoft.com/zh-cn/magazine/dn781361.aspx 身份验证和授权是应用程序安全的基础.身份验证通过验证提供的凭据来确定用户身份,而授 ...

  5. asp.net web api的源码

    从安装的NuGet packages逆向找回去 <package id="Microsoft.AspNet.WebApi.Core" version="5.2.7& ...

  6. asp.net web api的自托管模式HttpSelfHostServer可以以控制台程序或windows服务程序为宿主,不单单依赖于IIS web服务器

    Self-Hosting ASP.NET Web API http://theshravan.net/self-hosting-asp-net-web-api/ http://www.piotrwal ...

  7. 使用 OWIN 作为 ASP.NET Web API 的宿主

    使用 OWIN 作为 ASP.NET Web API 的宿主 ASP.NET Web API 是一种框架,用于轻松构建可以访问多种客户端(包括浏览器和移动 设备)的 HTTP 服务. ASP.NET ...

  8. 购物车Demo,前端使用AngularJS,后端使用ASP.NET Web API(3)--Idetity,OWIN前后端验证

    原文:购物车Demo,前端使用AngularJS,后端使用ASP.NET Web API(3)--Idetity,OWIN前后端验证 chsakell分享了前端使用AngularJS,后端使用ASP. ...

  9. ASP.NET Web API 框架研究 Web Host模式路由及将请求转出到消息处理管道

    Web Host 模式下的路由本质上还是通过ASP.NET 路由系统来进行路由的,只是通过继承和组合的方式对ASP.NET路由系统的内部的类进行了一些封装,产生自己专用一套类结构,功能逻辑基本都是一样 ...

随机推荐

  1. 【转】Java中 List的遍历

    原文网址:http://blog.csdn.net/player26/article/details/3955906 import java.util.ArrayList; import java.u ...

  2. 删除WIN7系统的共享文件

    运行里面输入fsmgmt.msc命令,点开共享目录,选定要取消共享的文件右键,停止共享.

  3. Linux中断分层技术

    一.中断嵌套  当系统正在执行某中断处理函数时,又产生了一个新的中断,这就叫做中断嵌套.当中断为慢速中断时,新的中断会取代当前中断,即当前中断没有执行完就结束 了:当中断为快速中断时,新的终端就不会产 ...

  4. 笔记:java并发实践2

    public interface Executor { void execute(Runnable command); } 虽然Executor是一个简单的接口,但它为灵活且强大的异步任务框架提供了基 ...

  5. hdu&&poj搜索题题号

    搜索 hdu1067 哈希 hdu1401 双向搜索 hdu1430 哈希 hdu1667 跌搜+启发式函数 hdu1685 启发式搜索 hdu1813 启发式搜索 hdu1885 状态压缩搜索 hd ...

  6. Swing UI - 可收起与开展内容面板实现演示

    基于JAVA Swing实现的自定义组件可折叠的JPanel组件 基本思想: 可折叠面板,分为两个部分-头部面板与内容面板 头部面板– 显示标题,以及对应的icon图标,监听鼠标事件决定内容面板隐藏或 ...

  7. 重写boost内存池

    最近在写游戏服务器网络模块的时候,需要用到内存池.大量玩家通过tcp连接到服务器,通过大量的消息包与服务器进行交互.因此要给每个tcp分配收发两块缓冲区.那么这缓冲区多大呢?通常游戏操作的消息包都很小 ...

  8. [深入React] 7.组件生命周期

    生命周期一共分三段:初始化,运行中,销毁.按照顺序: 初始化 getDefaultProps():Object 全局只会调用一次,为当前类生成的默认props,会被父组件传入的同名props覆盖. g ...

  9. Javascript 精髓整理篇之三(数组篇)postby:http://zhutty.cnblogs.com

    今天讲js的数组.数组是js中最基础的数据结构了. 主要讲讲数组实现栈,队列以及其他的基本操作.栈和队列都可以在数组头尾位置处理,所以,都有两种方式. 属性 1.length : 长度,表示数组元素的 ...

  10. 【C#基础】实现URL Unicode编码,编码、解码相关整理

    1.Unicode编码 引用系统 System.Web using System.Web; string postdata = "SAMLRequest=" + HttpUtili ...