Giter Club home page Giter Club logo

asyncservicebase's Introduction

AsyncServiceBase

An abstract implementation of System.ServiceProcess.ServiceBase aimed at developing Task-based Windows Services. Includes interactive testing facilities.

Supports .NET Framework 4.7.2

Getting started

AsyncServiceBase is an abstraction layer around System.ServiceProcess.ServiceBase and provides a single abstract method called RunServiceAsync() to run any Task-related work.

//your task-based service implementation goes here
public override async Task RunServiceAsync(string[] args, CancellationToken cancellationToken, PauseToken pauseToken)
{
    //define some task
    var task = SomeLongRunningTask(cancellationToken, pauseToken);

    //await completion
    await task;
}

Calls from Service Control Manager (SCM) to OnStart, OnStop, OnPause and OnContinue are handled internally by AsyncServiceBase to appropriately control starting, stopping and pausing of the Task via the CancellationToken and PauseToken.

It's important to await the completion of the internal task.

Upon task-completion, AsyncServiceBase will set the service's ExitCode accordingly based on the completion status of the task and will log any exception to the Windows Application eventlog.

Interactive debugging

AsyncServiceBase provides a static RunInteractive method which can be used to run and debug your specific service implementation interactively within a console application. Depending on the state-changes your service supports, you can start, stop, pause and continue your service.

Usage:

static void Main(string[] args)
{
    var service = new SomeSampleService();
    if (Environment.UserInteractive)
    {
        AsyncServiceBase.RunInteractive(service);
    }
    else
    {
        ServiceBase[] ServicesToRun;
        ServicesToRun = new ServiceBase[] { service };
        ServiceBase.Run(ServicesToRun);
    }
}

Please see the included SampleService application for a sample implementation.

Acknowledgements

A very special thanks goes to Stephan Cleary for his Nito.AsyncEx library, which is heavily used in AsyncServiceBase.

asyncservicebase's People

Contributors

aleroe avatar

Stargazers

 avatar

asyncservicebase's Issues

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.