Solving development problems  |  About this blog

Archive for the ‘manual installation windows service’ tag

Visual Studio 2008 and Windows Service project and setup

Problem is that Visual Studio 2008 does not have Windows Service project and you can not add it and create a setup for this project.

To override this use manual approach:

1. Create your service class manually:

using System;
using System.Diagnostics;
using System.ServiceProcess;

namespace WindowsService
{
    class WindowsService : ServiceBase
    {
        ///
        /// Public Constructor for WindowsService.
        /// - Put all of your Initialization code here.
        ///
        public WindowsService()
        {
            this.ServiceName = "My Windows Service";
            this.EventLog.Log = "Application";

            // These Flags set whether or not to handle that specific

            //  type of event. Set to true if you need it, false otherwise.

            this.CanHandlePowerEvent = true;
            this.CanHandleSessionChangeEvent = true;
            this.CanPauseAndContinue = true;
            this.CanShutdown = true;
            this.CanStop = true;
        }

        ///
        /// The Main Thread: This is where your Service is Run.
        ///
        static void Main()
        {
            ServiceBase.Run(new WindowsService());
        }

        ///
        /// Dispose of objects that need it here.
        ///
        ///
Whether
        ///    or not disposing is going on.
        protected override void Dispose(bool disposing)
        {
            base.Dispose(disposing);
        }

        ///
        /// OnStart(): Put startup code here
        ///  - Start threads, get inital data, etc.
        ///
        ///

        protected override void OnStart(string[] args)
        {
            base.OnStart(args);
        }

        ///
        /// OnStop(): Put your stop code here
        /// - Stop threads, set final data, etc.
        ///
        protected override void OnStop()
        {
            base.OnStop();
        }

        ///
        /// OnPause: Put your pause code here
        /// - Pause working threads, etc.
        ///
        protected override void OnPause()
        {
            base.OnPause();
        }

        ///
        /// OnContinue(): Put your continue code here
        /// - Un-pause working threads, etc.
        ///
        protected override void OnContinue()
        {
            base.OnContinue();
        }

        ///
        /// OnShutdown(): Called when the System is shutting down
        /// - Put code here when you need special handling
        ///   of code that deals with a system shutdown, such
        ///   as saving special data before shutdown.
        ///
        protected override void OnShutdown()
        {
            base.OnShutdown();
        }

        ///
        /// OnCustomCommand(): If you need to send a command to your
        ///   service without the need for Remoting or Sockets, use
        ///   this method to do custom methods.
        ///
        ///
Arbitrary Integer between 128 & 256
        protected override void OnCustomCommand(int command)
        {
            //  A custom command can be sent to a service by using this method:
            //#  int command = 128; //Some Arbitrary number between 128 & 256
            //#  ServiceController sc = new ServiceController("NameOfService");
            //#  sc.ExecuteCommand(command);

            base.OnCustomCommand(command);
        }

        ///
        /// OnPowerEvent(): Useful for detecting power status changes,
        ///   such as going into Suspend mode or Low Battery for laptops.
        ///
        ///
The Power Broadcast Status
        /// (BatteryLow, Suspend, etc.)
        protected override bool OnPowerEvent(PowerBroadcastStatus powerStatus)
        {
            return base.OnPowerEvent(powerStatus);
        }

        ///
        /// OnSessionChange(): To handle a change event
        ///   from a Terminal Server session.
        ///   Useful if you need to determine
        ///   when a user logs in remotely or logs off,
        ///   or when someone logs into the console.
        ///
        ///
The Session Change
        /// Event that occured.
        protected override void OnSessionChange(
                  SessionChangeDescription changeDescription)
        {
            base.OnSessionChange(changeDescription);
        }
    }
}

2. Add installer class:

using System;
using System.ComponentModel;
using System.Configuration.Install;
using System.ServiceProcess;

namespace WindowsService
{
    [RunInstaller(true)]
    public class WindowsServiceInstaller : Installer
    {
        ///
        /// Public Constructor for WindowsServiceInstaller.
        /// - Put all of your Initialization code here.
        ///
        public WindowsServiceInstaller()
        {
            ServiceProcessInstaller serviceProcessInstaller =
                               new ServiceProcessInstaller();
            ServiceInstaller serviceInstaller = new ServiceInstaller();

            //# Service Account Information
            serviceProcessInstaller.Account = ServiceAccount.LocalSystem;
            serviceProcessInstaller.Username = null;
            serviceProcessInstaller.Password = null;

            //# Service Information
            serviceInstaller.DisplayName = "My New C# Windows Service";
            serviceInstaller.StartType = ServiceStartMode.Automatic;

            //# This must be identical to the WindowsService.ServiceBase name
            //# set in the constructor of WindowsService.cs
            serviceInstaller.ServiceName = "My Windows Service";

            this.Installers.Add(serviceProcessInstaller);
            this.Installers.Add(serviceInstaller);
        }
    }
}

3. Use Framework 2.0 instalutil.exe to create Installation:

Install.bat

@ECHO OFF

REM The following directory is for .NET 2.0
set DOTNETFX2=%SystemRoot%\Microsoft.NET\Framework\v2.0.50727
set PATH=%PATH%;%DOTNETFX2%

echo Installing WindowsService...
echo ---------------------------------------------------
InstallUtil /i WindowsService.exe
echo ---------------------------------------------------
echo Done.

Uninstall.bat

@ECHO OFF

REM The following directory is for .NET 2.0
set DOTNETFX2=%SystemRoot%\Microsoft.NET\Framework\v2.0.50727
set PATH=%PATH%;%DOTNETFX2%

echo Installing WindowsService...
echo ---------------------------------------------------
InstallUtil /u WindowsService.exe
echo ---------------------------------------------------
echo Done.

Written by Avivo

October 21st, 2009 at 2:19 pm