Thursday, February 28, 2013

Running a Windows Service as a Console App

This is a prerequisite post before I can talk about the WindowsServiceTasks library that I recently released.

How do you test windows services?

I hope your first response was unit tests, but even if it was we both know that you still have to integration test the whole application. You could install your service into windows and then attach your debugger to it's process, but that is a lot of work. Why not just run your service as a console application? Then you can test it by just pressing play in Visual Studio!

How do you run a windows service as a console application?

Easy! Just go into your windows service project's settings and set the Output Type to Console Application. This will allow the executable to launch as a console app, but it is still completely executable as a service!

Inside of your code, you can use the Environment.UserInteractive property to determine which mode you are in. With that you can do cool things to help you debug, such as register an alternate logger with your IOC container that will write your long lines to console instead of (or as well as) to file.

static class Program
{
    static void Main(params string[] args)
    {
        var service = new Service1();
 
        if (!Environment.UserInteractive)
        {
            var servicesToRun = new ServiceBase[] { service };
            ServiceBase.Run(servicesToRun);
            return;
        }
 
        Console.WriteLine("Running as a Console Application");
        Console.WriteLine(" 1. Run Service");
        Console.WriteLine(" 2. Other Option");
        Console.WriteLine(" 3. Exit");
        Console.Write("Enter Option: ");
        var input = Console.ReadLine();
 
        switch (input)
        {
            case "1":
                service.Start(args);
                Console.WriteLine("Running Service - Press Enter To Exit");
                Console.ReadLine();
                break;
 
            case "2":
                // TODO!
                break;
        }
 
        Console.WriteLine("Closing");
    }
}
 
public partial class Service1 : ServiceBase
{
    public Service1() { InitializeComponent(); }
 
    public void Start(string[] args) { OnStart(args); }
 
    protected override void OnStart(string[] args) { }
 
    protected override void OnStop() { }
}

What other uses does this offer?

You can then also use your windows services as management utilities. I have worked with applications before where when I needed to make a configuration change to a system I was told to go run windows services with random undiscoverable command line arguments or magic number configuration settings. By launching your service as a console app you can inject simple menu systems to allow users easy access to admin functionality.

Shout it

Enjoy,
Tom

9 comments:

  1. Have you seen TopShelf? https://github.com/Topshelf/Topshelf It works really well with Nlog.

    ReplyDelete
  2. I had not looked at TopShelf, but judging solely by the size of the project I would say that there is a philosophical difference between it and my little WindowServiceTasks package: minimalism. I love pulling in small NuGet packages that help me get the job done, but I also don't to rely on frameworks when I don't have to...hey, this is a great topic for a blog post!

    In short I'm sure that TopShelf is a great tool, but WindowsServiceTasks is literally just two classes and an interface.

    ReplyDelete
  3. If I can make another suggestion, how about just writing a console application and running that console application as a service using a tool like http://runasservice.com/

    ReplyDelete
  4. This was tremendously helpful. Thank you, Tom.

    ReplyDelete
  5. Nice tip on creating the Start() method to get around not being able to call OnStart directly since it is protected.

    ReplyDelete
  6. This was a great tip. Clear and straight to the point also. Thanks very much.

    ReplyDelete
  7. A screenshot is a photograph or photo taken of someone's computer screen. It's been said that people are visual learners, based on the quote 'a portrait is worth a thousand words'. Screenshot software is generally utilized actually by bloggers, webmasters or just the people with visual minds. Windows screenshot capture apps

    ReplyDelete
  8. When I found out about online assistants, I was interested in trying some and I decided not in vain, because I found a professional service where I just pay for writing my essay, where I can also find my essay author

    ReplyDelete

Real Time Web Analytics