Last active
August 29, 2015 14:21
-
-
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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