C# Windows 服务,无法从我的安装程序的构造函数中访问 app.config

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/379276/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-04 01:15:30  来源:igfitidea点击:

Windows service, can't access app.config from within my Installer's constructor

c#.netsecurityconfigurationwindows-services

提问by Blankman

I want to store the username/password information of my windows service 'logon as' user in the app.config.

我想在 app.config 中存储我的 Windows 服务“登录为”用户的用户名/密码信息。

So in my Installer, I am trying to grab the username/password from app.config and set the property but I am getting an error when trying to install the service.

所以在我的安装程序中,我试图从 app.config 获取用户名/密码并设置属性,但在尝试安装服务时出现错误。

It works fine if I hard code the username/password, and fails when I try and access the app.config

如果我对用户名/密码进行硬编码,它工作正常,并且在我尝试访问 app.config 时失败

public class Blah : Installer
{

    public Blah()
    {

        ServiceProcessInstaller oServiceProcessInstaller = new ServiceProcessInstaller();
                ServiceInstaller oServiceInstaller = new ServiceInstaller();            

                oServiceProcessInstaller.Account = ServiceAccount.User;

        oServiceProcessInstaller.Username =             ConfigurationManager.AppSettings["ServiceProcessUsername"].ToString();

    }
}

采纳答案by Robert Wagner

The problem is that when your installer runs, you are still in installation phase and your application hasn't been fully installed. The app.config will only be available when the actual application is run.

问题是当您的安装程序运行时,您仍处于安装阶段并且您的应用程序尚未完全安装。app.config 仅在实际应用程序运行时可用。

You can however do the following:

但是,您可以执行以下操作:

  1. Prompt the user for the username and password within the installer (or on the command line).
  2. Pass this information to your installer class (google it)
  3. Within your installer class, there is a variable that tells you the installation path
  4. Within the appropriate event in the installer, use System.IO functions to open the app.config file and insert the user entered information
  1. 在安装程序中(或在命令行中)提示用户输入用户名和密码。
  2. 将此信息传递给您的安装程序类(谷歌它)
  3. 在您的安装程序类中,有一个变量会告诉您安装路径
  4. 在安装程序的相应事件中,使用 System.IO 函数打开 app.config 文件并插入用户输入的信息

回答by justin.m.chase

You really shouldn't store a password in an app.config file, that is very bad. You need to either use the service account, the current user or prompt them. Also a user can right click an .exe (which presumably is what is triggering your install) and select "run as" to change their credentials before installation (in which case current user would be a good selection).

你真的不应该在 app.config 文件中存储密码,这很糟糕。您需要使用服务帐户、当前用户或提示他们。此外,用户可以右键单击 .exe(这可能是触发安装的原因)并选择“运行方式”以在安装前更改其凭据(在这种情况下,当前用户将是一个不错的选择)。

Additionally in the services manager a user can change which user the service is supposed to run as after the installation is over. But you definitely don't want to store passwords in plain text files.

此外,在服务管理器中,用户可以在安装结束后更改服务应该运行的用户。但是您绝对不想将密码存储在纯文本文件中。

回答by justin.m.chase

Just some ideas on accessing config files inside an installer.

只是一些关于在安装程序中访问配置文件的想法。

Configuration config = ConfigurationManager.OpenExeConfiguration(assemblyPath);
ConnectionStringsSection csSection = config.ConnectionStrings;

Assembly Path can be gotten several ways: Inside Installer class implementation with:

可以通过多种方式获取程序集路径: 在安装程序类实现中:

this.Context.Parameters["assemblypath"].ToString();

or sometimes with reflection:

或有时与反思:

Assembly service = Assembly.GetAssembly(typeof(MyInstaller));
string assemblyPath = service.Location;

回答by BenC

I had the same problem with a service installer. You have to call your config file "myService.exe.config" and use the OpenExeConfiguration method with the assembly path to look for the right config file (as it is explained in the first answer, when your installers run, the base directory is the directory of the installUtil and not your installer)

我在使用服务安装程序时遇到了同样的问题。您必须调用您的配置文件“myService.exe.config”并使用带有程序集路径的 OpenExeConfiguration 方法来查找正确的配置文件(如第一个答案中所述,当您的安装程序运行时,基目录是installUtil 的目录而不是您的安装程序)

{
Assembly __ServiceAssembly = Assembly.GetAssembly(typeof(MyServiceInstaller));
Configuration config = ConfigurationManager.OpenExeConfiguration(__ServiceAssembly.Location);
KeyValueConfigurationCollection  svcSettings = config.AppSettings.Settings;
info("Service name : " + svcSettings["ServiceName"].Value);
}

If you don't want to follow the "myService.exe.config" format, use an exeConfigurationFileMap:

如果您不想遵循“myService.exe.config”格式,请使用 exeConfigurationFileMap:

{
Assembly __ServiceAssembly = Assembly.GetAssembly(typeof(SyslogServiceInstaller));
ExeConfigurationFileMap configFileMap = new ExeConfigurationFileMap();
configFileMap.ExeConfigFilename = 
     Path.Combine(Directory.GetParent(__ServiceAssembly.Location).ToString(),
     "App.config");

Configuration config = ConfigurationManager.OpenMappedExeConfiguration(
     configFileMap, ConfigurationUserLevel.None);
KeyValueConfigurationCollection mySettings = config.AppSettings.Settings;

Console.Out.WriteLine(mySettings["ServiceName"].Value);
}