Open Web Interface for. NET (OWIN) creates an abstraction layer between the Web server and the Web application. Owin separates the Web application from the Web server and then leaves the application outside of IIS, which is hosted by the Owin program. Use the OWIN to self-host ASP. NET Web API 2
This tutorial shows what 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 Newand 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 Managerand then click Package Manager Console. In the Package Manager Console window, enter the following command:
Install-Package Microsoft.AspNet.WebApi.OwinSelfHost
This would 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(IappbuilderAppBuilder) { Configure Web API for Self-host. HttpconfigurationConfig= New Httpconfiguration();Config.Routes.Maphttproute(Name: "Defaultapi", Routetemplate: api/ {Controller}/{id} ", Defaults: { ID =routeparameter. Optional } ); . Usewebapiconfig} } } /span>
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 class ValuesController
.
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(IntId) { Return "Value"; } POST api/values Public void Post([Frombody]StringValue) { } PUT API/VALUES/5 Public void put (int Id [frombody]string< Span class= "PLN" > Value) {} //DELETE api/values/5 public void delete< Span class= "pun" > (int Id) { } } Span class= "pun" >
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() { StringBaseAddress= "Http://localhost:9000/"; Start OWIN Host Using (WebApp.Start<Startup> (Url:BaseAddress)) { Create Httpcient and make a request to api/values HttpClientClient= New HttpClient(); VarResponse=Client.Getasync(BaseAddress+ "Api/values").Result console. Writelineresponseconsole. Writeline (response. Content. Readasstringasync (). Result } console. Readline} } } /span>
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:15GmtServer: microsoft-httpapi/< Span class= "lit" >2.0 content-length: 19 content -type: Application/< Span class= "PLN" >json; Charset=utf-8 } [< span class= "str" > "value1" , "value2" ]
Additional Resources
An overview of Project Katana
Host ASP. NET Web API in an Azure Worker Role
This article is originally created on July 9, 2013
Author Information
Kanchan Mehrotra
Comments () RSS FeedYou must is logged in to leave a comment. Show Comments
Use the OWIN to self-host ASP. NET Web API 2