Use OWIN to Self-Host ASP.NET Web API
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 2017
- Web API 5.2.7
Note
You can find the complete source code for this tutorial at github.com/aspnet/samples.
Create a console application
On the File menu, New, then select Project. From Installed, under Visual C#, select Windows Desktop and then select Console App (.Net Framework). Name the project "OwinSelfhostSample" and select OK.
Add the Web API and OWIN packages
From the Tools menu, select NuGet Package Manager, then select 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 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(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 with 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 HttpClient 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();
}
}
}
}
Run 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"]