Setup Aspnet Web Api Owin Self Hosting - egnomerator/misc GitHub Wiki
- Get started fairly quickly with a working self-hosted web API application with minimal extraneous files, references, and NuGet packages.
- Unlike most getting started tutorials involving OWIN self-hosting, this solution will work with .NET Framework 4.6.2 rather than .NET Core
- Open Visual Studio
- Create an ASP.NET Web Application (.NET Framework)
- Choose the Empty Template, check the box for Web API, and click OK
- Set the Target Framework to .NET Framework 4.6.2
- Remove IIS-related nuget packages
- Microsoft.AspNet.WebApi
- Microsoft.AspNet.WebApi.WebHost
- Install OWIN Nuget Packages
- Microsoft.Owin.Hosting (and dependencies)
- Microsoft.Owin.Host.HttpListener
- Microsoft.AspNet.WebApi.OwinSelfHost (and dependencies)
- Add an application root-level public
Program.cs
class - In the same file and namespace add a public
Startup
class - Add the code
// Add this method in the Program class static void Main() { const string baseAddress = "http://*:9000/"; using (WebApp.Start<Startup>(baseAddress)) { Console.WriteLine("Started!"); Thread.Sleep(Timeout.Infinite); Console.WriteLine("Stopping"); } } // Add these methods in the Startup class public void Configuration(IAppBuilder app) { var webApiConfig = ConfigureWebApi(); app.UseWebApi(webApiConfig); } private HttpConfiguration ConfigureWebApi() { var config = new HttpConfiguration(); config.Routes.MapHttpRoute( "DefaultApi", "api/{controller}/{id}", new { id = RouteParameter.Optional }); return config; }
Why the Thread.Sleep(Timeout.Infinite);
?
- This could just be a
Console.ReadLine()
, but this guide was originally documented with the intention of eventually running this application in a Docker container - Containers exit as soon as they have no tasks to complete, and this
sleep
line is used to prevent the container from closing so the server can continue to run, allowing the web API to be accessed
In the web application properties
- Set the output type from class library to Console Application
- Set the Startup object to the
Program
class - (optional) In the Web tab, select this option:
Don't open a page. Wait for a request from an external application.
- Selecting this option prevents a browser from automatically opening when starting to debug
- Since this app does not contain a UI component - it's just a web service exposing an API - it might be unnecessary to open the browser along with a tool like Fiddler or Postman
Clean up
- Remove files
Global.asax.cs
andGlobal.asax
- Remove file
WebApiConfig.cs
and its containingApp_Start
folder
- Add a
ItemsController.cs
class inheriting fromApiController
in theControllers
folder - Add a
Item.cs
class in theModels
folder - Add the code
// Add these properties in the Item class public long Id { get; set; } public string Name { get; set; } // Add these methods in the controller // GET api/items [HttpGet] public IEnumerable<Item> Get() { return new List<Item> { new Item {Id = 1, Name = "Item1"}, new Item {Id = 2, Name = "Item2"} }; } // POST api/items [HttpPost] public IEnumerable<string> Create(List<Item> items) { return items.Select(i => i.Name).ToList(); }
Run the app and verify the following:
- A console appears indicating the server has started
- Sending a GET request (via API testing tool of choice - e.g. Fiddler, Postman, etc.) gets the expected response
- Sending a POST (e.g. simply POSTing the same data from the GET response) request gets the expected response