Use OWIN to Self-Host ASP.NET Web API

In this guide, I’ll guide you through the process of creating a self-hosted Web API application using OWIN (Open Web Interface for .NET) and the Microsoft.AspNet.WebApi.OwinSelfHost package. This method enables you to run a Web API without the need for IIS (Internet Information Services).

Step 1: Set Up a Console Application

  1. Open Visual Studio.
  2. Create a new project for a console application.

Step 2: Integrate Web API and OWIN Packages

  1. Open the Package Manager Console from the Tools menu.
  2. Execute the following command to install the necessary package:
 Install-Package Microsoft.AspNet.WebApi.OwinSelfHost

Step 3: Configure the Startup Class

  1. Right-click on the project and add a new class named Startup.cs.
  2. Insert the following code into the Startup.cs class:
using Microsoft.Owin;
using Owin;
using System.Web.Http;

[assembly: OwinStartup(typeof(SelfHostWebApi.Startup))]

namespace SelfHostWebApi
{
    public class Startup
    {
        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);
        }
    }
}

Step 4: Create a Web API Controller

  1. Add a new class named HelloController.cs.
  2. Add the following code to the HelloController.cs class:
using System.Web.Http;

namespace SelfHostWebApi
{
    public class HelloController : ApiController
    {
        // GET api/hello
        public string Get()
        {
            return "Hello API";
        }
    }
}

Step 5: Configure the Main Program

  1. Open the Program.cs file.
  2. Insert the following code inside the Main method:
using Microsoft.Owin.Hosting;
using System;
using System.Net.Http;

namespace SelfHostWebApi
{
    class Program
    {
        static void Main(string[] args)
        {
            string baseAddress = "http://localhost:9000/";

            using (WebApp.Start<Startup>(url: baseAddress))
            {
                // Create HttpClient and make a request to the API
                HttpClient client = new HttpClient();

                var response = client.GetAsync(baseAddress + "api/hello").Result;

                Console.WriteLine(response);
                Console.WriteLine(response.Content.ReadAsStringAsync().Result);
            }

            Console.ReadLine();
        }
    }
}

Step 6: Run the Application

  1. Build and run the console application.
  2. If everything functions correctly, you’ll observe the output confirming the successful reachability of the API.

Congratulations! You’ve effectively developed a self-hosted Web API application using OWIN and a console application. This method empowers you to operate your Web API independently without depending on IIS. Feel free to expand on this foundation by incorporating more controllers and features to suit your Web API requirements.

Post a Comment

Please do not post any spam link in the comment box😊

Previous Post Next Post

Blog ads

CodeGuru