Skip to content

Instantly share code, notes, and snippets.

@KevM
Created August 27, 2010 16:09
Show Gist options
  • Select an option

  • Save KevM/553652 to your computer and use it in GitHub Desktop.

Select an option

Save KevM/553652 to your computer and use it in GitHub Desktop.
[TestFixture]
public class topshelf_configuration_extension
{
[Test]
public void required_simple_services_should_be_registed_with_topshelf()
{
var container = new Container(c=>c.For<IRequiredService>().Use<TestRequiredService>());
var logger = MockRepository.GenerateMock<ILogger>();
var runConfiguration = RunnerConfigurator.New(config => config.ConfigureRequiredServices(container, logger));
runConfiguration.Coordinator.GetServiceInfo().First().Name.ShouldStartWith(typeof(TestRequiredService).Name);
}
}
public class TestRequiredService : IRequiredService
{
public void Start()
{
throw new NotImplementedException();
}
public void Stop()
{
throw new NotImplementedException();
}
}
public static void ConfigureRequiredServices(this IRunnerConfigurator configurator, IContainer container, ILogger logger)
{
var simpleServiceInstances = container.Model.InstancesOf<IRequiredService>();
foreach (var simpleServiceInstance in simpleServiceInstances)
{
var requiredServiceType = simpleServiceInstance.ConcreteType;
logger.LogDebug("Configuring Topshelf to manage the lifecycle of a required service. Service type: {0}", requiredServiceType);
configurator.ConfigureService<IRequiredService>(service =>
{
service.Named("{0}:{1}".ToFormat(requiredServiceType.Name, Guid.NewGuid()));
service.HowToBuildService( name => container.GetInstance(requiredServiceType));
service.WhenStarted(s => s.Start());
service.WhenStopped(s => s.Stop());
});
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment