Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Convert Web API to use Self Hosting

Tags:

I am trying to convert an existing ASP.NET Web API project (currently hosted in IIS) into one that can use the SelfHost framework. I'm a bit fuzzy on the actual details but understand I can run a self-host server in a console window and then run the service on top of it. The problem I'm having is that my project is an MVC project and not a console one. My familiarity with console/Windows apps is somewhat limited as I generally work with projects to be hosted in IIS.

What I'm a bit confused on is whether I need to convert my existing Web API project in Visual Studio into a new console application, or if there's a way to create another console application Project in the solution which can act as the web server for the Web API services, or rather if there's a way to add a console element with a Main() entry point to the existing MVC project (overriding the Global.asax entry point.)

Search didn't yield much information that helps me fill this knowledge gap. Hoping someone can point me in the right direction. Even at a high level.

like image 589
trnelson Avatar asked Dec 12 '13 19:12

trnelson


People also ask

Can Web API be self hosted?

This tutorial shows how to host a web API inside a console application. ASP.NET Web API does not require IIS. You can self-host a web API in your own host process. New applications should use OWIN to self-host Web API.

Can ASP Net Web API ability to both self hosting?

ASP.NET Web API can be either be hosted in IIS or in a separate host process. The former approach is usually appropriate when the Web API is part of a web application and one or more web applications are going to consume it.

What is self hosting API?

As the name suggests, self hosting, the Web API is hosted independently of the main/client application. So the actual Web API is hosted either in a Windows Service or a console application running on the server.


1 Answers

I recently had to convert a Web API project into a self-hosted service using OWIN (on Visual Studio 2013). I did that as follows:

  1. Manually added Program.cs and Startup.cs files at the root of the project. Both files containing code as described here: http://www.asp.net/web-api/overview/hosting-aspnet-web-api/use-owin-to-self-host-web-api.

  2. Went to the properties of the Web API project. On the "Applications" section, I stated "Output Type" as "Console Application", and set the "Program" class as the "Startup object".

Although not required, I slightly modified the using block within Program.Main() to look as follows:

// Start OWIN host  using (WebApp.Start<Startup>(url: baseAddress))  {    // Create HttpCient and make a request to api/values    HttpClient client = new HttpClient();    var response = client.GetAsync(baseAddress + "api/values").Result;     if (response != null)   {     Console.WriteLine("Information from service: {0}", response.Content.ReadAsStringAsync().Result);   }   else   {     Console.WriteLine("ERROR: Impossible to connect to service");   }    Console.WriteLine();   Console.WriteLine("Press ENTER to stop the server and close app...");   Console.ReadLine(); }  

Finally, instead of calling config.Routes.MapHttpRoute() multiple times within Startup.Configuration(), you can refer to the routes you already wrote for the Web API:

// Configure Web API for self-host.  var config = new HttpConfiguration(); WebApiConfig.Register(config);         app.UseWebApi(config);  
like image 107
Ruben Ramirez Padron Avatar answered Oct 31 '22 01:10

Ruben Ramirez Padron