Skip to content

Put your apps on the Topshelf, with the power of SimpleInjector & Quartz.NET!

License

Notifications You must be signed in to change notification settings

tynor88/Topshelf.SimpleInjector

Repository files navigation

Topshelf.SimpleInjector

Build status Build Status NuGet Version NuGet Downloads Coverage Status

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.

Project Status Stories in Ready

Throughput Graph

Install

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

Quick Start

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:

    1. Create a new Console Application in Visual Studio
    1. Install Topshelf.SimpleInjector.QuickStart, running the following command in the Package Manager Console: Install-Package Topshelf.SimpleInjector.QuickStart
    1. When your asked to overwrite Program.cs click Yes
    1. Click start and you will have a running service
    1. Change the service name and implement your logic :-)

Example Usage

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
        {
        }
    }
}

References

Copyright & License

Copyright 2014-2017 tynor88

Licensed under the MIT License

About

Put your apps on the Topshelf, with the power of SimpleInjector & Quartz.NET!

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published