インストール中に消費される app.config ファイルを含むサービスがあります。ProjectInstaller クラスには、app.config ファイルを読み取り、資格情報を引き出して設定するコードがあります。目標は、ユーザー/パスワードのプロンプトを表示しないことです。
以下の例serviceUser
でservicePassword
は、 と はオブジェクト全体のプライベート文字列です。
次のブロック (ProjectInstaller ctor にあります) は正しく動作します。つまり、サービスのログオンの詳細を設定し、ユーザーにプロンプトを表示しません。
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
}
ただし、次の場合は機能しません。
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*
}
文字列がハードコードされているかどうかが重要なのはなぜですか? また、注目に値するのは、私の app.config ではスラッシュをエスケープしていないことです...これは xml エスケープ文字ではないためです。
なぜこれが失敗するのですか?
編集:「失敗」とは、サービスインストーラーがパスワードの入力を求め、ユーザーが入力したものは何でも上書きすることを意味しserviceUser
ますservicePassword
.