7

I have a service with an app.config file that is consumed during install. In the ProjectInstaller class there is code to read the app.config file, pull out credentials and set them. The goal is to not show the user/password prompt.

In the below examples serviceUser and servicePassword are just object wide private strings.

The following block (resides in the ProjectInstaller ctor) works correctly. That is, it sets the logon details for the service and never prompts the user:

public ProjectInstaller()
{
   InitializeComponent();
   LoadServiceSettings();

   //Install the service as a specific user:
   serviceProcessInstaller1.Account = ServiceAccount.User;
   serviceProcessInstaller1.Username = "MYDOMAIN\\myUser";
   serviceProcessInstaller1.Password = servicePassword; //pulled out of app.config by LoadServiceSettings call
}

However, the following does not work:

public ProjectInstaller()
{
   InitializeComponent();
   LoadServiceSettings();

   //Install the service as a specific user:
   serviceProcessInstaller1.Account = ServiceAccount.User;
   serviceProcessInstaller1.Username = serviceUser; //both get pulled out of app.config by LoadServiceSettings() call;
   serviceProcessInstaller1.Password = servicePassword;

   WriteLog("The service user is *" + serviceUser + "*");
   // The above line outputs:
   // The service user is *MYDOMAIN\myUser*

   WriteLog("Are the strings the same? " + (serviceUser == "MYDOMAIN\\myUser").ToString());
   // The above line outputs:
   // Are the strings the same? True

   WriteLog("Values: *" + serviceUser + "*" + servicePassword + "*");
   // The above line outputs:
   // Values: *MYDOMAIN\myUser*myPassword*

   WriteLog("Values: *" + serviceProcessInstaller1.Username + "*" + serviceProcessInstaller1.Password + "*");
   // The above line outputs:
   // Values: *MYDOMAIN\myUser*myPassword*
}

Why does it matter whether the string is hardcoded or not? Also worth noting is that in my app.config I don't escape the slash...because it's not an xml escape character.

Why is this failing?

EDIT: By "failing" I mean the service installer prompts for a password, and whatever the user puts in overrides serviceUser and servicePassword.

4

0 回答 0