Skip to content

Instantly share code, notes, and snippets.

@idavis
Created May 14, 2010 22:03
Show Gist options
  • Save idavis/401729 to your computer and use it in GitHub Desktop.
Save idavis/401729 to your computer and use it in GitHub Desktop.
#region Using Directives
using System;
using System.ServiceProcess;
using YourCorp.Services;
using Ninject;
using Topshelf;
using Topshelf.Configuration;
using Topshelf.Configuration.Dsl;
#endregion
namespace YourCorp
{
internal static class Program
{
private static void Main( string[] args )
{
using ( IKernel kernel = CreateKernel() )
{
RunConfiguration cfg =
RunnerConfigurator.New(
x =>
{
x.SetDisplayName( "Your Service" );
x.SetServiceName( "YourService" );
x.SetDescription( "Your Service" );
x.ConfigureService<MonitoringService>( kernel );
x.ConfigureService<ReportingService>( kernel );
x.RunAsLocalSystem();
} );
Runner.Host( cfg, args );
}
}
private static IKernel CreateKernel()
{
var kernel = new StandardKernel();
kernel.Bind<IWindowsService>().To<MonitoringService>();
kernel.Bind<IWindowsService>().To<ReportingService>();
return kernel;
}
}
public static class SyntaxExtensions
{
public static void ConfigureService<T>( this IRunnerConfigurator runnerConfigurator, IKernel kernel )
where T : ServiceBase, IWindowsService
{
runnerConfigurator.ConfigureService<T>(
c =>
{
c.HowToBuildService( serviceName => kernel.Get<T>() );
c.Named( typeof (T).Name );
c.WhenStopped( s => s.Stop() );
c.WhenStarted( s => s.Start() );
} );
}
}
public interface IWindowsService
{
void Start();
void Stop();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment