Skip to content

Instantly share code, notes, and snippets.

@mauriciogentile
Created October 9, 2012 02:33
Show Gist options
  • Save mauriciogentile/3856242 to your computer and use it in GitHub Desktop.
Save mauriciogentile/3856242 to your computer and use it in GitHub Desktop.
Mvc Web API on TopShelf
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();
}
}
}
@deepakkumpala
Copy link

where is AccountController configured?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment