Creating windows service using Topshelf

February 1, 2011

tags: , ,
5 comments

Hi,

I am creating windows services using Topshelf for a while and I forget how “hard” (not that hard but harder) it was to create a windows service without Topshelf.

Topshelf is a lightweight framework for building Windows services using the .NET framework. The idea is to create a console application and “publishing” it as a service with command line. No more dedicated window service project and an installer.

So, how does it work? here is a simple example:

1. Create new “console application”

2. Create your service class

public class MyService : IService

{

    public void Start()

    {

        Console.WriteLine("Running...");

    }

 

    public void Stop()

    {

        Console.WriteLine("Done!");

    }

}

3. write something like the code below: (here we are using topshelf)

static void Main(string[] args)

{

    RunConfiguration cfg = RunnerConfigurator.New(x =>

    {

        x.ConfigureService<MyService>(s =>

        {

            s.Named("MySampleService");

            s.HowToBuildService(service => new MyService());

            s.WhenStarted(service => service.Start());

            s.WhenStopped(service => service.Stop());

        });

        x.RunAsLocalSystem();

 

        x.SetDescription("Sample Topshelf Host");

        x.SetDisplayName("The service");

        x.SetServiceName("MySampleService");

    });

    Runner.Host(cfg, args);

}

That’s it!

now you have a console application that will run the Start() method when you start it and run the Stop() method when you stop it. Very comfortable when you are in the development process and you want to run or debug your service.

Now to install it or uninstall run the command line:

myservice.exe install

myservice.exe uninstall

Enjoy your new service, I personally wrapped Topshelf that I wont need to copy the code above every time:

I created an interface called IService

public interface IService   

{

    void Start();

    void Stop();

}

And a simple method called ServiceCreator

public static void ServiceCreator<T>(string name, string displayName, string description, string[] args) where T : IService, new()

{

    RunConfiguration cfg = RunnerConfigurator.New(x =>

    {

        x.ConfigureService<MyService>(s =>

        {

            s.Named(name);

            s.HowToBuildService(service => new T());

            s.WhenStarted(service => service.Start());

            s.WhenStopped(service => service.Stop());

        });

        x.RunAsLocalSystem();

 

        x.SetDescription(description);

        x.SetDisplayName(displayName);

        x.SetServiceName(name);

    });

    Runner.Host(cfg, args);

}

Now my main looks like this:

static void Main(string[] args)

{

    ServiceCreator<MyService>("Name", "Display Name", "desciption", args);

}

Hope it will help you to create and debug windows services

Update: MAY-2012

The API has changed a bit, now in order to install the server you need to run “{your_exe} install” and to uninstall “{your_exe} uninstall”


Keep Writing, Compiling, and Debugging

Alon Nativ

Add comment
facebook linkedin twitter email

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

5 comments

  1. Moshe KaplanFebruary 11, 2011 ב 4:16 PM

    Hi Alon,

    Great post as always,
    You might have to emphasis two facts that should remove any fear from using this tool:
    1. The result is a regular windows service (just much easier to implement).
    2. Your successful experience with it in a high intense production environment.

    Keep Performing,
    Moshe Kaplan
    http://blogs.microsoft.co.il/blogs/vprnd/

    Reply
  2. Alon NativFebruary 12, 2011 ב 7:44 PM

    Hi Moshe,

    Thanks for the question.

    1. Yes, the result is a "regular" windows service exactly the same result as the installer project.

    2. I am using topshelf for a while and it is used in our production services with mass traffic.

    Topshelf is just a simple wrapper for the service installer project – make it easier to create and debug windows services.

    Thanks,

    Alon

    Reply
  3. rnairMay 3, 2012 ב 1:46 AM

    Hi, I was trying to make the topshelf as the windows service. How can I get into windows services console. Whatever you have explained here will run in a command line.

    Regards,

    Rathish

    Reply
  4. Alon NativMay 4, 2012 ב 12:10 AM

    Hi rnair,

    The API has changed a bit – I update the post. now in order to install / uninstall the service run:

    myservice.exe install

    myservice.exe uninstall

    Thanks for your input

    Alon

    Reply