Tuesday, June 9, 2015

WebAPI SelfHosting via OWIN v/s HttpSelfHostServer

WebAPI can be hosted via 2 ways. The traditional way using HttpSelfHostServer and the new OWIN based hosted. Below is an attempt to compare both techniques.

API Controller

Below is the controller class I am going to host using both the hosting techniques.
using System.Web.Http;
public class MyWebAPIController : ApiController
{
[AcceptVerbs("GET")]
public string Help()
{
return "This is test WEB API. Supported methods are ../api/MyWebAPI/Help, ../api/MyWebAPI/Square/{number}";
}
[AcceptVerbs("GET")]
public int Square(int id)
{
return id * id;
}
}

Differences between HttpSelfHostServer and OWIN Hosting

using System.Web.Http;
using System.Web.Http.SelfHost;
public class WebAPI_HttpSelfHostServer_Test
{
/// <summary>
/// Exposed to outside to starts the server.
/// </summary>
/// <remarks>Simple function call into server class.start method</remarks>
public void StartServer()
{
new MyHttpServer().StartUsingHttpSelfHostServer();
}
}
internal class MyHttpServer
{ /// <summary>
/// Method to configure the hosting and start server. It define the route as well.
/// </summary>
/// <remarks>No convention here. Everything is pretty much configured
///</remarks>
internal void StartUsingHttpSelfHostServer()
{
var config = new HttpSelfHostConfiguration("http://localhost:8080");
config.Routes.MapHttpRoute(
"API Default",
"api/{controller}/{action}/{id}",
new { id = RouteParameter.Optional });
HttpSelfHostServer server = new HttpSelfHostServer(config);
server.OpenAsync().Wait();
Console.WriteLine("HttpSelfHost-Press Enter to quit...");
}
}
public class WebAPI_OWINSelfHostServer_Test
{
/// <summary>
/// Exposed to starts server using WebApp class which uses OWIN specs.
/// </summary>
public void StartServer()
{
WebApp.Start<MyOWINServer>("http://localhost:8080");
Console.WriteLine("OWIN - Press Enter to quit...");
Console.ReadLine();
}
}
internal class MyOWINServer
{
/// <summary>
/// Method to configure the App to define route and use WebApi
/// </summary>
/// <param name="builder"></param>
/// <remarks>The name of the method should be Configuration and needs to be public.Else there will be exception
/// A first chance exception of type 'System.EntryPointNotFoundException' occurred in Microsoft.Owin.Hosting.dll
/// The following errors occurred while attempting to load the app.
/// - No 'Configuration' method was found in class 'Console45.MyOWINServer, Console45, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null'.
///</remarks>
public void Configuration(IAppBuilder builder)
{
var config = new HttpConfiguration();
config.Routes.MapHttpRoute(
"API Default",
"api/{controller}/{action}/{id}",
new { id = RouteParameter.Optional });
builder.UseWebApi(config);
}
}
In HttpSelfHostServer we can see most of the things as configurable. But in OWIN, its by convention.

References

https://frendsrnd.wordpress.com/2014/02/03/httpselfhostserver-hosted-web-api-with-https-and-windows-authentication-enabled/

No comments: