Extend TopShelf to be a self-hosted API using OWIN (Open Web Interface for .NET)
NB: Owin requires .NET 4.5 or later.
Install the Nuget package.
Install-Package Topshelf.Owin
Then modify your TopShelf service.
using Topshelf;
using TopShelf.Owin;
namespace YourService
{
internal class Program
{
static void Main(string[] args)
{
HostFactory.Run(c =>
{
c.RunAsNetworkService();
c.Service<YourService>(s =>
{
s.ConstructUsing(() => new YourService());
s.WhenStarted((service, control) => service.Start());
s.WhenStopped((service, control) => service.Stop());
s.OwinEndpoint(app =>
{
app.Domain = "localhost";
app.Port = 8080;
});
});
});
}
}
}
If you want to use with AutoFac, then also add AutoFac.WebApi and TopShelf.AutoFac packages and then set the DependencyResolver
.
s.OwinEndpoint(app =>
{
app.UseDependencyResolver(new AutofacWebApiDependencyResolver(container));
}
By default, it will map routes using attributes on the actions. If you want to change the HttpCongfiguration
to change routes or change the JSON serializer, you can:
s.OwinEndpoint(app =>
{
app.ConfigureHttp(httpConfiguration =>
{
httpConfiguration.MapHttpAttributeRoutes();
httpConfiguration.Formatters.Clear();
httpConfiguration.Formatters.Add(new JsonMediaTypeFormatter());
var jsonSettings = httpConfiguration.Formatters.JsonFormatter.SerializerSettings;
jsonSettings.Formatting = Formatting.Indented;
jsonSettings.ContractResolver = new CamelCasePropertyNamesContractResolver();
});
});
If you want to bind any OWIN app/middleware other than WebAPI, you can do so by accessing the IAppBuilder
instance directly, e.g.:
s.OwinEndpoint(app =>
{
app.ConfigureAppBuilder(appBuilder => appBuilder.UseNancy());
});