Coder Social home page Coder Social logo

dingwd / topshelf.integrations Goto Github PK

View Code? Open in Web Editor NEW

This project forked from markhuber/topshelf.integrations

0.0 2.0 0.0 12.11 MB

Topshelf.Integrations is a collection of packages that extend the Topshelf Project. These packages handle the boiler plate code necessary for several use cases that we have found very useful for quickly developing small self contained services on Windows.

License: MIT License

C# 100.00%

topshelf.integrations's Introduction

What is it?

Topshelf.Integrations is a collection of packages that extend the Topshelf Project. These packages handle the boiler plate code necessary for several use cases that we have found very useful for quickly developing small self contained services on Windows.

These use cases include the following:

  • HTTP/REST Communication from and to a Windows Service
  • Scheduled activities hosted within a Windows Service
  • Integration of your preferred IoC Container

These packages solve these problems by integrating the following technologies with Topshelf and providing extensions to quickly integrate them.

Getting Started

These packages are available on Nuget and can be used in any combination desired. The Ninject package is directly integrated with Topshelf, and also has accompanying packages for the WebAPI and Quartz.Net projects to make it possible to instantiate ApiControllers via the container and initiate Quartz.NET IJob instances via the container.

Topshelf.Ninject

To get the package: Install-Package Topshelf.Ninject

To use Ninject with your Topshelf service, all you need is three lines:

using Topshelf.Ninject;

...

class Program
{
    static void Main()
    {
        HostFactory.Run(c =>
        {
            c.UseNinject(new SampleModule()); //Initiates Ninject and consumes Modules

            c.Service<SampleService>(s =>
            {
                //Specifies that Topshelf should delegate to Ninject for construction
                s.ConstructUsingNinject(); 
                s.WhenStarted((service, control) => service.Start());
                s.WhenStopped((service, control) => service.Stop());
            });
        });
    }
}

Topshelf.Quartz & Topshelf.Quartz.Ninject

To get the package: Install-Package Topshelf.Quartz

To add Ninject Support: Install-Package Topshelf.Quartz.Ninject

There are two options for using Quartz.NET with Topshelf, you may schedule any number of Quartz jobs along with your service like this:

using System;
using Ninject.Modules;
using Quartz;
using Topshelf;
using Topshelf.Ninject;
using Topshelf.Quartz;
using Topshelf.Quartz.Ninject;

...

class Program
{
    static void Main()
    {
        HostFactory.Run(c =>
        {
        	// Topshelf.Ninject (Optional) - Initiates Ninject and consumes Modules
            c.UseNinject(new SampleModule());

            c.Service<SampleService>(s =>
            {
                //Topshelf.Ninject (Optional) - Construct service using Ninject
                s.ConstructUsingNinject();

                s.WhenStarted((service, control) => service.Start());
                s.WhenStopped((service, control) => service.Stop());

                // Topshelf.Quartz.Ninject (Optional) - Construct IJob instance with Ninject
                s.UseQuartzNinject(); 

                // Schedule a job to run in the background every 5 seconds.
                // The full Quartz Builder framework is available here.
                s.ScheduleQuartzJob(q =>
                    q.WithJob(() =>
                        JobBuilder.Create<SampleJob>().Build())
                    .AddTrigger(() =>
                        TriggerBuilder.Create()
                            .WithSimpleSchedule(builder => builder
	                            .WithIntervalInSeconds(5)
	                            .RepeatForever())
                            .Build())
                    );
            });
        });
    }
}

You can also schedule a job as your service if no continually running service implementation is needed

using System;
using Ninject.Modules;
using Quartz;
using Topshelf;
using Topshelf.Ninject;
using Topshelf.Quartz;
using Topshelf.Quartz.Ninject;


...

class Program
{
    static void Main(string[] args)
    {
        HostFactory.Run(c =>
        {

                // Topshelf.Ninject (Optional) - Initiates Ninject and consumes Modules
                c.UseNinject(new SampleModule());
                // Topshelf.Quartz.Ninject (Optional) - Construct IJob instance with Ninject
                c.UseQuartzNinject();

            c.ScheduleQuartzJobAsService(q =>
                    q.WithJob(() =>
                        JobBuilder.Create<SampleJob>().Build())
                    .AddTrigger(() =>
                        TriggerBuilder.Create()
                            .WithSimpleSchedule(builder => builder
                                .WithIntervalInSeconds(5)
                                .RepeatForever())
                            .Build())
            );
        });

    }
}

Topshelf.WebApi & Topshelf.WebApi.Ninject

To get the package: Install-Package Topshelf.WebApi

To add Ninject Support: Install-Package Topshelf.WebApi.Ninject

The WebAPI endpoint can be initialized alongside your service like this:

using System;
using System.Web.Http;
using Ninject.Modules;
using Topshelf;
using Topshelf.Ninject;
using Topshelf.WebApi;
using Topshelf.WebApi.Ninject;

...

static void Main()
{
    HostFactory.Run(c =>
    {
        c.UseNinject(new SampleModule()); //Initiates Ninject and consumes Modules

        c.Service<SampleService>(s =>
        {
            //Specifies that Topshelf should delegate to Ninject for construction
            s.ConstructUsingNinject();

            s.WhenStarted((service, control) => service.Start());
            s.WhenStopped((service, control) => service.Stop());

            //Topshelf.WebApi - Begins configuration of an endpoint
            s.WebApiEndpoint(api => 
                //Topshelf.WebApi - Uses localhost as the domain, defaults to port 8080.
                //You may also use .OnHost() and specify an alternate port.
                api.OnLocalhost()
                    //Topshelf.WebApi - Pass a delegate to configure your routes
                    .ConfigureRoutes(Configure)
                    //Topshelf.WebApi.Ninject (Optional) - You may delegate controller 
                    //instantiation to Ninject.
                    //Alternatively you can set the WebAPI Dependency Resolver with
                    //.UseDependencyResolver()
                    .UseNinjectDependencyResolver()
                    //Instantaties and starts the WebAPI Thread.
                    .Build());
        });
    });
}

private static void Configure(HttpRouteCollection routes)
{
    routes.MapHttpRoute(
            "DefaultApiWithId", 
            "Api/{controller}/{id}", 
            new { id = RouteParameter.Optional }, 
            new { id = @"\d+" });
}

topshelf.integrations's People

Contributors

markhuber avatar philrowan avatar michaelyoung1981 avatar sitereactor avatar

Watchers

James Cloos avatar tradewind avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.