Skip to content

Latest commit

 

History

History
81 lines (51 loc) · 3.89 KB

File metadata and controls

81 lines (51 loc) · 3.89 KB
uid web-api/overview/hosting-aspnet-web-api/use-owin-to-self-host-web-api
title Use OWIN to Self-Host ASP.NET Web API - ASP.NET 4.x
author rick-anderson
description Tutorial with code showing how to host ASP.NET Web API in a console application.
ms.author tdykstra
ms.date 07/09/2013
ms.assetid a90a04ce-9d07-43ad-8250-8a92fb2bd3d5
msc.legacyurl /web-api/overview/hosting-aspnet-web-api/use-owin-to-self-host-web-api
msc.type authoredcontent
ms.custom sfi-image-nochange

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

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.

Screenshot of the 'create new project' dialog box, showing the menu options to select Windows Desktop then Console App from the dropdown list.

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.

Screenshot of the package manager console, showing licensing information, followed by P M > at the end, signaling where to type the command.

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.

Screenshot of the solution explorer dialog box menu, showing the steps to follow for adding a class to the project.

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

[!code-csharpMain]

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]

Start the OWIN Host and make a request with HttpClient

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

[!code-csharpMain]

Run the application

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

[!code-consoleMain]

Screenshot of the console, showing the status code and information for the application as it's running.

Additional resources

An Overview of Project Katana

Host ASP.NET Web API in an Azure Worker Role