Skip to content

Instantly share code, notes, and snippets.

@joymon
Last active August 29, 2015 14:21
Show Gist options
  • Save joymon/e0919e7c618365fdb946 to your computer and use it in GitHub Desktop.
Save joymon/e0919e7c618365fdb946 to your computer and use it in GitHub Desktop.
Sample code to show how WebApi can be hosted in console using HttpSelfHostServer
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...");
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment