Prompt for Service Account During Windows Service Installation


This article shows how to customize the installation of a windows service in Visual Studio 2005 to prompt for a service account username and password. This process involves combining several separate techniques than can each be used for other purposes (like passing setup screen values to a generic custom action program).

 

Verify you have a project installer class in your project. If not:

 

  1. In Solution Explorer, access Design view for the service for which you want to add an installation component.
  2. Click the background of the designer to select the service itself, rather than any of its contents.
  3. With the designer in focus, right-click, and then click Add Installer.
  4. A new class, ProjectInstaller, and two installation components, ServiceProcessInstaller and ServiceInstaller, are added to your project, and property values for the service are copied to the components.

 

Verify you have a Setup project.

 

Modify the Setup project.

 

  • Use the User Interface Editor to add a Textboxes (A) dialog box.  You will need to move it up above the Installation folder dialog box.

    1.jpg
     
  • Confirm the properties of the Textboxes dialog box. Verify the Edit1Property value is the default EDITA1 and the EditProperty value is the default EDITA2.

    TextboxProperties.jpg
     
  • Use the Custom Actions Editor to add to the Applications folder the Primary Output.

    CustomActions.jpg
     
  • Click on the Primary output item under Install and modify the properties. Set the customActionData property to:/f1=[EDITA1] /f2=[EDITA2]

    Data.jpg

Note: This is the restricted format expected by installer classes, you can use your own format for other custom action programs. 

 

This will pass the value entered into the username textbox as a parameter named "f1" and similarly the password value will be parameter "f2".

 

Modify the ProjectInstaller.cs

 

1. Note in the ProjectInstaller.Designer.cs file, the InitializeComponent method shows adding two installers to the Installers collection:

//

// ProjectInstaller

//

this.Installers.AddRange(new System.Configuration.Install.Installer[]
{this.serviceProcessInstaller1, this.serviceInstaller1});


2. Since we need to add the service account info to the ServiceProcessInstaller we need to find it inside the Installers collection first.  Once we have the instance we can assign the service account properties found in Context.Parameters then finish up by running the base.Install method.


3. Add this to the Project.Installer.cs file:INSTALLOVERRIDEpublicoverridevoidInstal System.Collections.IDictionarystateSaver


{

    System.ServiceProcess.ServiceProcessInstaller

    serviceProcessInstaller1=null;

    foreach (object installer in this.Installers)

    try

    {

        serviceProcessInstaller1 = (System.ServiceProcess.ServiceProcessInstaller)
        installer;

    }

    catch (Exception exc)

    {

    }

    if (serviceProcessInstaller1 != null)

    {

        serviceProcessInstaller1.Account =

        System.ServiceProcess.ServiceAccount.User;

        serviceProcessInstaller1.Username = Context.Parameters["f1"];

        serviceProcessInstaller1.Password = Context.Parameters["f2"];

    }

    base.Install(stateSaver);

}