Created
October 9, 2012 02:33
-
-
Save mauriciogentile/3856242 to your computer and use it in GitHub Desktop.
Mvc Web API on TopShelf
This file contains 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; | |
using System.Web.Http; | |
using System.Web.Http.SelfHost; | |
using Topshelf; | |
namespace MvcOnTopShelf | |
{ | |
class Program | |
{ | |
static void Main(string[] args) | |
{ | |
HostFactory.Run(x => | |
{ | |
x.Service<RestService>(s => | |
{ | |
s.ConstructUsing(name => new RestService("http://localhost:2020")); | |
s.WhenStarted(svc => svc.Start()); | |
s.WhenStopped(svc => svc.Stop()); | |
}); | |
x.RunAsLocalSystem(); | |
x.SetDescription("My rest service"); | |
x.SetDisplayName("My rest service"); | |
x.SetServiceName("RestService"); | |
}); | |
} | |
} | |
public class RestService | |
{ | |
private readonly HttpSelfHostServer server; | |
private readonly HttpSelfHostConfiguration config; | |
public RestService(string baseAddress) | |
{ | |
config = new HttpSelfHostConfiguration(baseAddress); | |
config.Routes.MapHttpRoute("DefaultApi", "api/{controller}/{id}", new { id = RouteParameter.Optional }); | |
server = new HttpSelfHostServer(config); | |
} | |
public void Start() | |
{ | |
server.OpenAsync(); | |
} | |
public void Stop() | |
{ | |
server.CloseAsync(); | |
server.Dispose(); | |
} | |
} | |
public class AccountController : ApiController | |
{ | |
[HttpGet] | |
public string Get(int id) | |
{ | |
return Guid.NewGuid().ToString(); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
where is AccountController configured?