Put your apps on the Topshelf, with the power of SimpleInjector! Topshelf.SimpleInjector provides extensions to construct your Topshelf service class from the SimpleInjector IoC container.
You can find the package on NuGet Gallery.
To install Topshelf.SimpleInjector, run the following command in the Package Manager Console:
Install-Package Topshelf.SimpleInjector
You can also find a Quick Start package on NuGet Gallery. This will quickly get you up and running with your Topshelf Windows Service and SimpleInjector IoC Framework, when following these steps:
-
- Create a new Console Application in Visual Studio
-
- Install Topshelf.SimpleInjector.QuickStart, running the following command in the Package Manager Console:
Install-Package Topshelf.SimpleInjector.QuickStart
- Install Topshelf.SimpleInjector.QuickStart, running the following command in the Package Manager Console:
-
- When your asked to overwrite Program.cs click Yes
-
- Click start and you will have a running service
-
- Change the service name and implement your logic :-)
The sample code below, is basically the same as the code you will get in the Quick Start package.
using System;
using SimpleInjector;
namespace Topshelf.SimpleInjector.Sample
{
class Program
{
private static readonly Container _container = new Container();
static void Main(string[] args)
{
//Register services
_container.Register<ISampleDependency, SampleDependency>();
_container.Register<ISampleDependency2, SampleDependency2>();
//This does not need to be explicitly registered
_container.Register<SampleService>();
//Check container for errors
_container.Verify();
HostFactory.Run(config =>
{
// Pass it to Topshelf
config.UseSimpleInjector(_container);
config.Service<SampleService>(s =>
{
// Let Topshelf use it
s.ConstructUsingSimpleInjector();
s.WhenStarted((service, control) => service.Start());
s.WhenStopped((service, control) => service.Stop());
});
});
}
public class SampleService
{
private readonly ISampleDependency _sample;
private readonly ISampleDependency2 _sample2;
public SampleService(ISampleDependency sample, ISampleDependency2 sample2)
{
_sample = sample;
_sample2 = sample2;
}
public bool Start()
{
Console.WriteLine("Sample Service Started.");
Console.WriteLine("Sample Dependency: {0}", _sample);
Console.WriteLine("Sample Dependency2: {0}", _sample2);
return _sample != null && _sample2 != null;
}
public bool Stop()
{
return _sample != null && _sample2 != null;
}
}
public interface ISampleDependency
{
}
public class SampleDependency : ISampleDependency
{
}
public interface ISampleDependency2
{
}
public class SampleDependency2 : ISampleDependency2
{
}
}
}
Copyright 2014-2017 tynor88
Licensed under the MIT License