I have added two ServiceInstallers to my ServiceProcessInstaller. After that I haved changed my Main() as below:
static void Main()
{
ServiceBase[] ServicesToRun;
ServicesToRun = new ServiceBase[]
{
new Service1(),
new Service2()
};
ServiceBase.Run(ServicesToRun);
}
I have also set Service2 as dependent service on Service1 as below:
private void InitializeComponent()
{
this.Service1ProcessInstaller = new System.ServiceProcess.ServiceProcessInstaller();
this.Service1Installer = new System.ServiceProcess.ServiceInstaller();
this.Service2Installer = new System.ServiceProcess.ServiceInstaller();
//
// Service1ProcessInstaller
//
this.Service1ProcessInstaller.Account = System.ServiceProcess.ServiceAccount.LocalSystem;
this.Service1ProcessInstaller.Password = null;
this.Service1ProcessInstaller.Username = null;
//
// Service1Installer
//
this.Service1Installer.ServiceName = "Service1";
this.Service1Installer.ServicesDependedOn = new string[] {"Service2"};
//
// Service2Installer
//
this.Service2Installer.ServiceName = "Service2";
//
// ProjectInstaller
//
this.Installers.AddRange(new System.Configuration.Install.Installer[] {
this.Service1ProcessInstaller,
this.Service1Installer,
this.Service2Installer});
}
still it is only running my Service1.
Service2 is never calling.
And if I change sequence in Main() then Service2 is only calling.
It is always calling first service.
How to call my both services?
I found the solution. The problem was not with dependent service it was with my uninstallation.I have uninstalled my service and then installed it again and then I found both services in my Services.msc.
We need dependent service when they are actually dependent on each other so I have removed dependent service code too.
Now I can start them manually. and both of them are running. Below is my code which ran successfully.