【问题标题】:Windows service, can't access app.config from within my Installer's constructorWindows 服务,无法从我的安装程序的构造函数中访问 app.config
【发布时间】:2010-09-27 14:45:15
【问题描述】:

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

所以在我的安装程序中,我试图从 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();

    }
}

【问题讨论】:

    标签: c# .net security configuration windows-services


    【解决方案1】:

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

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

    Assembly Path 可以通过以下几种方式获得: 内部安装程序类实现:

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

    或有时带有反射:

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

    【讨论】:

      【解决方案2】:

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

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

      1. 在安装程序中(或在命令行中)提示用户输入用户名和密码。
      2. 将此信息传递给您的安装程序类(谷歌搜索)
      3. 在您的安装程序类中,有一个变量告诉您​​安装路径
      4. 在安装程序的相应事件中,使用 System.IO 函数打开 app.config 文件并插入用户输入的信息

      【讨论】:

        【解决方案3】:

        我在使用服务安装程序时遇到了同样的问题。您必须调用配置文件“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);
        }
        

        如果您不想遵循“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);
        }
        

        【讨论】:

        • 我无法访问安装程序类中的 Configuration 或 ConfigurationManager...我应该添加一些参考吗?
        • 很久以前写的。这是我的使用列表:using System; using System.Collections; using System.Collections.Generic; using System.ComponentModel; using System.Configuration.Install; using System.Linq; using System.Configuration; using System.ServiceProcess; using System.Reflection; using System.IO;
        【解决方案4】:

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

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

        【讨论】:

          猜你喜欢
          • 2011-03-29
          • 2011-11-29
          • 2019-05-24
          • 1970-01-01
          • 1970-01-01
          • 2013-10-07
          • 2016-02-07
          • 2020-12-16
          • 2017-02-03
          相关资源
          最近更新 更多