[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 4489
  • Last Modified:

Setting up dependencies in a Windows Service?

When I create an installer for a Windows Service (with Visual Studio), how can I set up dependencies?  
For instance, dependencies on other services?

Thanks!
0
yizchaknaveh
Asked:
yizchaknaveh
  • 7
  • 2
2 Solutions
 
Tom KnowltonWeb developerCommented:
It's not automatic?

::: listening ::::
0
 
yizchaknavehAuthor Commented:
Maybe in some cases.  

But let's say my service relies on SQL Server running locally (because I have ADO.NET that assumes that).

How do I register that implicit dependency?
0
 
Tom KnowltonWeb developerCommented:
From a book I have on C#:


ServiceInstaller is the class needed for every service; it has the following propeties for each service inside a process:

StartType
DisplayName
ServiceName

and   (   I think this is the one you want )

ServicesDependedOn


ServicesDependedOn -- Specifies an array of services that MUST be started before this service can be started.  When the service is started, all these dependent services are started automatically, and then our service will start.




Does this help you?


Tom
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
Tom KnowltonWeb developerCommented:
I created a new Windows Service from scratch.


It displays the "Design" view for the Windows Service.


If you right-click on this "Design" view....you have an option to "Add Installer" --  go ahead and do this.


Then open up the installer source code and find this region:



#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();
                  //
                  // serviceProcessInstaller1
                  //
                  this.serviceProcessInstaller1.Password = null;
                  this.serviceProcessInstaller1.Username = null;
                  //
                  // serviceInstaller1
                  //
                  this.serviceInstaller1.ServiceName = "Service1";
                  //
                  // ProjectInstaller
                  //
                  this.Installers.AddRange(new System.Configuration.Install.Installer[] {
                                                                                                                                this.serviceProcessInstaller1,
                                                                                                                                this.serviceInstaller1});

            }
            #endregion




Where you see:


this.serviceInstaller1.ServiceName = "Service1";



You will want to add something like:


this.serviceInstaller1.ServiceDependsOn []  servicesDependedUpon = {"ServiceDepenedOnNameOne","ServiceDependedOnTwo"};


I am guessing on how the dependent services are listed or initialized.
0
 
Tom KnowltonWeb developerCommented:
I am getting all of this information from the following book:

Professional C#  3rd Edition  (by Wrox Publishing)
0
 
Tom KnowltonWeb developerCommented:
This all starts on page 1112 in the above-mentioned book.
0
 
Tom KnowltonWeb developerCommented:
Sorry to be so psycho....but this is from the Help documentation for .NET:



.ServicesDependedOn Property  [C#]See Also
ServiceInstaller Class | ServiceInstaller Members | System.ServiceProcess Namespace | StartType | ServiceInstaller Members (Visual J# Syntax) | Managed Extensions for C++ Programming
Requirements
Platforms: Windows NT Server 4.0, Windows NT Workstation 4.0, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 family

.NET Framework Security:  

Full trust for the immediate caller. This member cannot be used by partially trusted code. For more information, see Using Libraries From Partially Trusted Code
Language
C#

C++

JScript

Visual Basic

Show All
Indicates the services that must be running for this service to run.

[Visual Basic]
Public Property ServicesDependedOn As String ()

[C#]
public string[] ServicesDependedOn {get; set;}

[C++]
public: __property String* get_ServicesDependedOn();
public: __property void set_ServicesDependedOn(String* __gc[]);

[JScript]
public function get ServicesDependedOn() : String[];
public function set ServicesDependedOn(String[]);

Property Value
An array of services that must be running before the service associated with this installer can run.

Remarks
A service can require other services to be running before it can start. The information from this property is written to a key in the registry. When the user (or the system, in the case of automatic startup) tries to run the service, the Service Control Manager (SCM) verifies that each of the services in the array has already been started.

If any service in the array is not running then, the SCM tries to start them. This includes services with Manual StartType.

If any service upon which this service depends fails to start, this service will not start. An exception is not thrown if the system is not started because there is no exception handling at the system level to detect this. Decide how to handle service start failures and implement this in your code. Typically, a dialog appears to the user at startup if a service fails to start.

If the service does not start, an entry is written to the Application event log.

The services upon which this service depends do not need to be in the same executable.

Requirements
Platforms: Windows NT Server 4.0, Windows NT Workstation 4.0, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 family

.NET Framework Security:  

Full trust for the immediate caller. This member cannot be used by partially trusted code. For more information, see Using Libraries From Partially Trusted Code
See Also
ServiceInstaller Class | ServiceInstaller Members | System.ServiceProcess Namespace | StartType | ServiceInstaller Members (Visual J# Syntax) | Managed Extensions for C++ Programming


Syntax based on .NET Framework version 1.1.
0
 
heintalusCommented:
You can do it through the IDE in design view you dont have to do it in code.

As knowlton has said you should add an Installer by right clicking on your service in design view & selecting Add Installer.

Now go to your ProjectInstaller in design view & you should see 2 objects on the design service, serviceProcessInstaller1 & serviceInstaller1.

Select the properties of serviceInstaller1, within the properties you should see an entry for ServicesDependedOn.  If you click on the elipse it will open up a window where you can type in the services that your service depends upon, one on each line.

The value you put in there is the name of the service as it appears in the Services.msc

HTH
Andy
0
 
yizchaknavehAuthor Commented:
Thanks!  

I ended up using heintalus' suggestion, but learned a lot from knowlton's in-depth efforts.

Points split.
0
 
Tom KnowltonWeb developerCommented:
Thanks!!!  Have fun?
0

Featured Post

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

  • 7
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now