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
- Visual Studio 2013 (also works with Visual Studio 2012)
- Web API 2
[!NOTE] 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:
[!code-csharpMain]
1: using Owin;
2: using System.Web.Http;
3:
4: namespace OwinSelfhostSample
5: {
6: public class Startup
7: {
8: // This code configures Web API. The Startup class is specified as a type
9: // parameter in the WebApp.Start method.
10: public void Configuration(IAppBuilder appBuilder)
11: {
12: // Configure Web API for self-host.
13: HttpConfiguration config = new HttpConfiguration();
14: config.Routes.MapHttpRoute(
15: name: "DefaultApi",
16: routeTemplate: "api/{controller}/{id}",
17: defaults: new { id = RouteParameter.Optional }
18: );
19:
20: appBuilder.UseWebApi(config);
21: }
22: }
23: }
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:
[!code-csharpMain]
1: using System.Collections.Generic;
2: using System.Web.Http;
3:
4: namespace OwinSelfhostSample
5: {
6: public class ValuesController : ApiController
7: {
8: // GET api/values
9: public IEnumerable<string> Get()
10: {
11: return new string[] { "value1", "value2" };
12: }
13:
14: // GET api/values/5
15: public string Get(int id)
16: {
17: return "value";
18: }
19:
20: // POST api/values
21: public void Post([FromBody]string value)
22: {
23: }
24:
25: // PUT api/values/5
26: public void Put(int id, [FromBody]string value)
27: {
28: }
29:
30: // DELETE api/values/5
31: public void Delete(int id)
32: {
33: }
34: }
35: }
Start the OWIN Host and Make a Request Using HttpClient
Replace all of the boilerplate code in the Program.cs file with the following:
[!code-csharpMain]
1: using Microsoft.Owin.Hosting;
2: using System;
3: using System.Net.Http;
4:
5: namespace OwinSelfhostSample
6: {
7: public class Program
8: {
9: static void Main()
10: {
11: string baseAddress = "http://localhost:9000/";
12:
13: // Start OWIN host
14: using (WebApp.Start<Startup>(url: baseAddress))
15: {
16: // Create HttpCient and make a request to api/values
17: HttpClient client = new HttpClient();
18:
19: var response = client.GetAsync(baseAddress + "api/values").Result;
20:
21: Console.WriteLine(response);
22: Console.WriteLine(response.Content.ReadAsStringAsync().Result);
23: Console.ReadLine();
24: }
25: }
26: }
27: }
Running the Application
To run the application, press F5 in Visual Studio. The output should look like the following:
[!code-consoleMain]
1: StatusCode: 200, ReasonPhrase: 'OK', Version: 1.1, Content: System.Net.Http.StreamContent, Headers:
2: {
3: Date: Tue, 09 Jul 2013 18:10:15 GMT
4: Server: Microsoft-HTTPAPI/2.0
5: Content-Length: 19
6: Content-Type: application/json; charset=utf-8
7: }
8: ["value1","value2"]
Additional Resources
Host ASP.NET Web API in an Azure Worker Role
|