Click here to Skip to main content
15,885,767 members
Articles / Programming Languages / C#
Tip/Trick

Multiple File Watcher Services in One Windows Service Project C#

Rate me:
Please Sign up or sign in to vote.
4.20/5 (2 votes)
15 Apr 2011CPOL1 min read 18.3K   10  

Note: There is a Document sub folder and an InstallerBatch sub folder included in the entire solution containing some good information/comments and the installation/uninstallation batch files.


What I am Trying to Achieve


I'm trying to put multiple services into one service, with one same name. So, When you do "net start TheService", it will start Service1() and MySecondUserService().

What I Have Done


I thought adding another service to my project and changing the services to run would be sufficient.
ServiceBase[] ServicesToRun;
ServicesToRun = new ServiceBase[] {new Service1(), new MySecondUserService()};
ServiceBase.Run(ServicesToRun);

When I install and run this,
only the first service is run "Service1".


The solutions is not to add another project installer, but a service installer, which is a component on the project installer. That newly added service installer has to have second service name configured.


Another
example?



href="http://stackoverflow.com/questions/2820216/multiple-windows-services-in-a-single-project-mystery">http://stackoverflow.com/questions/2820216/multiple-windows-services-in-a-single-project-mystery


Like code below:


Installer1.cs[Design]

Must have 2 serviceInstaller’s and 1 serviceProcessInstaller


In Installer1.Designer.cs code must be like below:


C#
#region Component Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
    this.serviceProcessInstaller1 = new System.ServiceProcess.ServiceProcessInstaller();
    this.serviceInstaller1 = new System.ServiceProcess.ServiceInstaller();
    this.serviceInstaller3 = new System.ServiceProcess.ServiceInstaller();
    //
    // serviceProcessInstaller1
    //
    this.serviceProcessInstaller1.Account = System.ServiceProcess.ServiceAccount.LocalSystem;
    this.serviceProcessInstaller1.Password = null;
    this.serviceProcessInstaller1.Username = null;
    //
    // serviceInstaller1
    //
    this.serviceInstaller1.Description = "TEST WATCHER1";
    this.serviceInstaller1.DisplayName = "Test Watcher1";
    this.serviceInstaller1.ServiceName = "Test Watcher Service #1";
    //
    // serviceInstaller3
    //
    this.serviceInstaller3.Description = "TEST WATCHER3";
    this.serviceInstaller3.DisplayName = "Test Watcher3";
    this.serviceInstaller3.ServiceName = "Test Watcher Service #3";
    //
    // Installer1
    //
    this.Installers.AddRange(new System.Configuration.Install.Installer[] {
  this.serviceProcessInstaller1,
  this.serviceInstaller1,
  this.serviceInstaller3});

}

#endregion

private System.ServiceProcess.ServiceProcessInstaller serviceProcessInstaller1;
private System.ServiceProcess.ServiceInstaller serviceInstaller1;
private System.ServiceProcess.ServiceInstaller serviceInstaller3;

In both Service1.Designer.cs and Service3.Designer.cs files, below must be true:


this.ServiceName MUST match this.serviceInstaller1.ServiceName in Installer1.Designer.cs file
 
this.ServiceName MUST match this.serviceInstaller3.ServiceName in Installer1.Designer.cs file

ADVANTAGE



  • Putting multiple services in one single Windows Service
    .exe file can allow one step installation. One installation will have all the
    services installed in the Service Controller and all can be viewed in the
    Services MMC snap-in.
  • Also may be multiple services can share code.

ISSUE


The only issue found so far is that after all the services started, if one or more (but not all) stopped, the "stopped" service(s) is/are still running somehow at the background. To avoid this type of behavior, all other services that still need to run will have to do a restart in order to have all the stopped services truly stop.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
United States United States
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
-- There are no messages in this forum --