Skip to main content
Email Password   helpLost your password?

Screenshot - hybridservice.jpg

Introduction

Recently, I ran into a problem while developing a client/server application. I needed to turn the server (console application) into a service so that it would run when the server started, without having to log into the machine. In the past, I would have left the console application as-is, started another project as the actual service and have it launch the console application. Then I would to start yet another project to do the actual installing and uninstalling of the service. Many of you might have used a similar technique and I'm sure you can all agree that having three executable files for switching between console and service is a real pain. So, I went scouring the internet and found an excellent article by Chris Mullins which put me in the right direction.

Using the code

If you have already created the console application, you will need to move it from the Program.cs file to the ConsoleApplication.cs class provided in the demo application. This may take some work if you built your entire application in the Program.cs file. If you haven't built your application yet, you're in luck as it's very simple to use this code. I would recommend using the demo project as your new project, as everything you need is already there. All you need to do is build your application in ConsoleApplication.cs, using the constructor as you would use the entry point of Program.cs. Here is a quick example of using the ConsoleApplication.cs file:

using System;
using System.Collections.Generic;
using System.IO;
using System.Text;

namespace HybridService
{
    class ConsoleApplication
    {
        public ConsoleApplication(string applicationPath, bool consoleMode)
        {
            //This is where your console application would be


            Console.WriteLine("Hybrid Service Application");
            Console.WriteLine();

            StreamWriter stream = 
                new StreamWriter(applicationPath + "\\output.txt", true);
            stream.WriteLine(DateTime.Now.ToString("f") + "   It works! \n");
            stream.Flush();
            stream.Close();
            stream.Dispose();

            Console.WriteLine(
                "I have written to " + applicationPath + "\\output.txt");
            Console.WriteLine("Press any key to exit...");
            if (consoleMode) Console.ReadKey();

            //If you need to exit the application before 

            //it reaches this point, you need to use the line below, or 

            //the service will not stop running.

            //But if you are running a socket server or something that 

            //needs to stay alive after this method has been executed, 

            //remove the line below.

            Environment.Exit(0);
        }

        public void Close()
        {
            //Application is exiting. This is where your cleanup code 

            //should be. For example, a socket server would need 

            //"mySocketListener.Close();"

        }
    }
}

You will also need to open the Program.cs file and give your application a name that will be displayed in the Services window.

applicationTitle = "My Hybrid Service";

Running the application

If you were to start debugging this application right now, it would show up as a console window. This is great for testing and debugging, but when you're ready to move it into production as a service, you would normally need to jump through hoops. Then if you needed to debug something, you would have to jump through those hoops all over again, but not anymore! Now it is easier than ever to switch between console and service. You don't even have to change your code. All you need to do is build your project, open a Command Window, navigate to your project's Release folder, and use the following command line argument:

HybridService -service

Your program will now install the service and run it once it's installed. It has also been configured to start when your machine boots. If you need to make changes to the program, stop the service and rebuild your application. That's it. Once you're done testing it in production, you will need to uninstall the service so that it doesn't linger on your machine. Use this command line argument:

HybridService -noservice

Your service has now been stopped (if it was running) and uninstalled.

Points of interest

I personally think this is the greatest thing since sliced bread, but I'm biased. However, there are some things that you need to be aware of:

Final notes

I would, again, like to thank Chris Mullins for posting his code. As for the ServiceInstaller, it is a modified version of some code I found about a year ago. I can't remember who the original author was, but if you know, send me a link to the original code and I'll post a thank-you to him/her as well. I hope this code helps someone. It has helped me quite a bit. Also, I would appreciate some feedback. This is my first post to CodeProject, so I'm interested in learning how I did. If you find any problems, let me know. If you use this in your project, I only ask that you give me credit for it in your application.

History

You must Sign In to use this message board.
 
 
Per page   
 FirstPrevNext
GeneralGetting error: 'System.ServiceProcess.ServiceInstaller' does not contain a definition for 'UnInstallService' Pin
globy
17:40 10 Sep '09  
GeneralCode Pin
dm3281
17:53 10 Dec '08  
GeneralNice, but there is a better solution. Pin
Alexander Stuckenholz
23:30 29 Jun '08  
GeneralWorks Great! Pin
Norbert17
7:24 12 Jun '08  
GeneralGreat article !!! Pin
Hans-Jürgen Schmidt
1:49 28 Jan '08  
GeneralTo anyone having a problem with service stuck at starting Pin
Ross Peoples
3:23 24 Jul '07  
NewsFixed! (Now it Starts) Pin
travislaborde
8:40 11 Jul '07  
GeneralRe: Fixed! (Now it Starts) Pin
Stewart Roberts
11:06 11 Jul '07  
GeneralRe: Fixed! (Now it Starts) Pin
travislaborde
5:56 12 Jul '07  
QuestionRe: Fixed! (Now it Starts) Pin
Stewart Roberts
9:22 12 Jul '07  
AnswerRe: Fixed! (Now it Starts) Pin
travislaborde
3:10 16 Jul '07  
GeneralService stays in "Starting" mode until the Constructor code completes Pin
JamesMMM
11:44 10 Jul '07  
GeneralRe: Service stays in "Starting" mode until the Constructor code completes Pin
travislaborde
8:40 11 Jul '07  
Questionnice, but.... Pin
travislaborde
10:08 10 Jul '07  
AnswerRe: nice, but.... Pin
travislaborde
8:09 11 Jul '07  
GeneralService won't start Pin
Stewart Roberts
5:49 4 Jul '07  


Last Updated 26 Jun 2007 | Advertise | Privacy | Terms of Use | Copyright © CodeProject, 1999-2009