About Me

Wednesday, February 16, 2011

Dynamic windows service name

Today i had to install few instances of the same windows service.
Of course they should be named differently.

I found that the name should be set to the ServiceName property of service installer.
This means, that i need to pass a name of the service when I installing it.

I found few solutions, but the most usefull for me, is when the installing is asking me for exact name before it installs the service.
Here is the code that should be placed into the service installer codebehind file (in my case it is ProjectInstaller.cs):
[RunInstaller(true)]
    public partial class ProjectInstaller : Installer
    {
        public ProjectInstaller()
        {
            InitializeComponent();
        }

  private void BeforeInstallEventHandler(object sender, InstallEventArgs e)
  {

   // Add steps to perform any actions before the install process.

   Console.WriteLine("BeforeInstallEventHandler Called");

   Console.WriteLine("Enter the name you would like this service installed as:");

   serviceInstaller.ServiceName = Console.ReadLine();

   PersistServiceName(serviceInstaller.ServiceName);

   Console.WriteLine("Attempting to install service as: " + serviceInstaller.ServiceName);

  }

  private void BeforeUninstallEventHandler(object sender, InstallEventArgs e)
        {

            Console.WriteLine("BeforeUninstallEventHandler Called");

            serviceInstaller.ServiceName = RetrieveServiceName();

            // Add steps to perform any actions before the Uninstall process.

            Console.WriteLine("Code for BeforeUninstallEventHandler");

        }

  /// 
  /// Storing name of the service to the 
  /// config file, for using duing uninstall
  /// process
  /// 
        private void PersistServiceName(string serviceName)
        {

            TextWriter tw = new StreamWriter("Service.config", false);      

            tw.WriteLine(serviceName);

            tw.Close();

        }

  /// 
  /// Getting stored service name from
  /// the file during uninstall process
  /// 
  /// 
        private string RetrieveServiceName()
        {

            string serviceName;


            TextReader tr = new StreamReader("Service.config");

            serviceName = tr.ReadLine();

            tr.Close();

            Console.WriteLine("ServiceName" + serviceName);

            return serviceName;      
        }
    }

Project installer has few events. The code above is using two of them - BeforeInstallEventHandler and BeforeUninstallEventHandler. First one is using to ask the user the service name and store it to the config file.
The second one is using during uninstallation process to retrieve service name from the file and pass it to uninstall process.
Thus, don't forget to open ProjectInstaller.cs designer and assign its BeforeInstall and BeforeUninstall events with appropriate handlers (BeforeInstallEventHandler and BeforeUninstallEventHandler)

No comments:

Post a Comment