Skip to content

Instantly share code, notes, and snippets.

@dvhthomas
Created October 5, 2009 01:31
Show Gist options
  • Select an option

  • Save dvhthomas/201740 to your computer and use it in GitHub Desktop.

Select an option

Save dvhthomas/201740 to your computer and use it in GitHub Desktop.
TopShelf makes service creation a snap
using System;
using StructureMap;
using Topshelf;
using Topshelf.Configuration;
namespace GisQueryDirectHost
{
internal class Program
{
private static void Main(string[] args)
{
// Set up logging and other application services using StructureMap
ContainerBootstrapper.BootstrapStructureMap();
try
{
IRunConfiguration cfg = RunnerConfigurator.New(x =>
{
x.AfterStoppingTheHost(h => Console.WriteLine("All services are stopping since the host is closing down."));
x.ConfigureService<QueryHost>("qh", s =>
{
// Here comes StructureMap behind the Microsoft ServiceLocator facade
s.CreateServiceLocator(() =>
{
ObjectFactory.Initialize(i =>
{
i.ForConcreteType<QueryHost>().Configure.WithName("qh");
i.ForConcreteType<ServiceConsole>();
});
return new StructureMapServiceLocator();
});
// I have two methods in my QueryHost class...Start and Stop. They simply
// start and stop a WCF service host process
s.WhenStarted(qh => qh.Start());
s.WhenStopped(qh => qh.Stop());
});
x.SetDescription("Supports generation of basic spatial data for clients through an ArcGIS Server query");
x.SetDisplayName("WCS GIS Query");
x.SetServiceName("wcsgisquery");
// You can also specify username/password, or point to app.config in one line of code
x.RunAsLocalSystem();
});
Runner.Host(cfg, args);
}
catch (Exception e)
{
Console.WriteLine(e);
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment