Introduction
In this article we will learn how to host an ASP.NET Web API in a console application, using the Open Web Interface for .NET (OWIN) to self-host the Web API framework.
OWIN defines the abstraction between .NET web servers and web applications. In a general scenario the OWIN decouples with the web application from the server, that makes OWIN as a self-hosting a web application for our own process, that is absent of IIS.
Before the start of OWIN Self Hosting applications let's try to understand the self hosting using my old article. According to this article we will be able to understand the basics of Self Hosting.
Now we are able to start learning the OWIN self hosting application step-by-step.
Step 1
Create a Console Application.
To create a Console application we need to use a basic procedure. Click on the File menu and choose New, then click Project. After getting the project we need to specify the language from the Installed Templates, so we will choose Visual C#, and click Windows and then click Console Application. Now we can provide a nice name for the application.
Step 2
Add the Web API and OWIN Packages
There are two ways to install the OWIN package using the Nuget Package manager. First we need to only right-click on the Project from the Solution Explorer and select the NuGet Package Manager and search the OWIN self-host package. We will be able to see many applications listed below. In those we need to select the appropriate package. Secondly we need to click on the Tools menu, then 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
According to this command we will able to install the WebAPI OWIN selfhost package and all the required OWIN packages.
Step 3
Add the student class.
In this class we will set the property of the student.
- public class Student
- {
- public int Id { get; set; }
- public string Name { get; set; }
- public string CollegeName { get; set; }
- public string Address { get; set; }
- }
Step 4 Configure Web API for Self-Host.
In this step we will add a class that handles the route as well as make the compatible travel the route according to the URL parameter. For that right-click on the project and select Add / Class to add a new class. Name the class Startup.
- using Owin;
- using System.Web.Http;
-
- namespace OWinHost
- {
- public class Startup
- {
- public void Configuration(IAppBuilder appBuilder)
- {
-
- HttpConfiguration config = new HttpConfiguration();
- config.Routes.MapHttpRoute(
- name: "DefaultApi",
- routeTemplate: "api/{controller}/{id}",
- defaults: new { id = RouteParameter.Optional }
- );
-
- appBuilder.UseWebApi(config);
- }
- }
- }
Step 5 Add a Web API Controller.
Now add a Web API controller class. Right-click the project and select Add / Class to add a new class. Name the class StudentController.
- namespace OWinHost
- {
- public class StudentController : ApiController
- {
- private static List<Student> student = new List<Student>{
- new Student{Id=1,Name="rajeev", CollegeName="MIT", Address="New Delhi"},
- new Student{Id=2,Name="ranjan", CollegeName="MIT", Address="pune"},
- new Student{Id=3,Name="Jasmine", CollegeName="VIT", Address="Banglor"},
- new Student{Id=4,Name="Manish", CollegeName="JNU", Address="New Delhi"}
- };
- public IEnumerable<Student> GetAll()
- {
- return student;
- }
- public Student GetById(int id)
- {
- var stud = student.FirstOrDefault(x => x.Id == id);
- if (stud == null)
- {
- throw new HttpResponseException(HttpStatusCode.NotFound);
- }
- return stud;
- }
- }
- }
Step 6Start the OWIN host and make a request using HttpClient.
- using Microsoft.Owin.Hosting;
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Net.Http;
- using System.Text;
- using System.Threading.Tasks;
-
- namespace OWinHost
- {
- public class Program
- {
- static void Main()
- {
- string baseAddress = "http://localhost:9000/";
-
-
- using (WebApp.Start<Startup>(url: baseAddress))
- {
-
- HttpClient client = new HttpClient();
-
- var response = client.GetAsync(baseAddress + "api/student").Result;
-
- Console.WriteLine(response);
- Console.WriteLine(response.Content.ReadAsStringAsync().Result);
- }
-
- Console.ReadLine();
- }
- }
- }
OutputSummaryIn this article we have learned Self Hosting using OWIN.
<< Getting Started With ASP.Net Web API 2 : Day 10