我有一個服務,在安裝時將Myapp.exe.config文件(基於app.config文件生成)放入安裝目錄。在安裝過程中,我需要此文件的詳細信息來配置安裝。特別是我需要調整服務的ServiceProcessInstaller實例的帳戶/用戶名/密碼字段,以便在運行時以特定用戶身份運行。在安裝時設置.NET Windows服務的角色...從app.config設置
然而,在安裝過程中,安裝程序並沒有爲我的註冊表設置,但也不能複製我的Myapp.exe.config到安裝目錄...所以我也沒辦法拉這些值,這意味着我的服務不能安裝爲正確的用戶。我現在可以做的唯一方法是將用戶/傳遞值硬編碼到ProjectInstaller類中,但我不能讓自己這樣做。這只是錯誤的。
是否有關於如何安裝服務作爲特定用戶以及如何在安裝過程中訪問這些憑據的Windows安裝最佳做法。
目前我想是這樣的:
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<appSettings>
<add key="User" value="DOMAIN\bob"/>
<add key="Password" value="secret"/>
</appSettings>
</configuration>
我怎麼能拉期間安裝,而不訴諸這些用戶名/密碼值:
namespace MyService
{
[RunInstaller(true)]
public partial class ProjectInstaller : System.Configuration.Install.Installer
{
public ProjectInstaller()
{
InitializeComponent();
//Set the user credentials.
//NOTE: Eventually this needs to be updated to pull these values out of a
// conf file. The problem now is that the location of the conf file
// is tied to a registry entry for the location of the service which
// may or may not exist when this block is executed.
/* This is the only way I can get it to work, but this is
* too horrible to ever actually do it this way:
serviceProcessInstaller1.Account = System.ServiceProcess.ServiceAccount.User;
serviceProcessInstaller1.Username = "DOMAIN\\user";
serviceProcessInstaller1.Password = "password";
*/
// Try to pull the service's registry values to know where it installed:
RegistryKey keyService = Registry.LocalMachine.OpenSubKey("SYSTEM\\CurrentControlSet\\services\\MyService");
string path = ((string)keyService.GetValue("ImagePath"));
path = path.Substring(1, path.LastIndexOf('\\'));
string user = someValueFromFileIOontheAbovePath1,
pass = someValueFromFileIOontheAbovePath2;
serviceProcessInstaller1.Account = System.ServiceProcess.ServiceAccount.User;
serviceProcessInstaller1.Username = user;
serviceProcessInstaller1.Password = pass;
//Doesn't work because install isn't complete and there aren't reg settings
// yet, and even if I had the path the .config file is not written yet so
// there's no file to parse.
}
}
}
保存這些參數有些config文件將它們硬編碼到二進制文件中?
謝謝。
您是否使用內置於Visual Studio中的MSI設置? –
@ErikPhilips感謝您的評論。無論如何,我在我的解決方案中添加了一個簡單的部署項目(vdproj),該解決方案在構建時會發出設置和.msi文件。雖然真正所有的部署項目都會訪問服務內部的ServiceProcessInstaller對象。 – kmarks2