Skip to content

Instantly share code, notes, and snippets.

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