Created
May 14, 2010 22:03
-
-
Save idavis/401729 to your computer and use it in GitHub Desktop.
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
#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