Windows服务,无法从我的安装程序的构造函数中访问app.config [英] Windows service, can't access app.config from within my Installer's constructor

查看:811
本文介绍了Windows服务,无法从我的安装程序的构造函数中访问app.config的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



因此,在我的安装程序中,我正在尝试使用我的Windows服务的用户名/密码信息。从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();

}
}


解决方案

问题是,当您的安装程序运行时,您仍处于安装阶段,并且您的应用程序尚未完全安装。



但您可以执行以下操作:


  1. 在安装程序(或命令行)中提示用户输入用户名和密码。

  2. 将此信息传递给安装程序类

  3. 在安装程序类中,有一个变量用于指示安装路径

  4. 在安装程序的相应事件中,使用System.IO函数以打开app.config文件并插入用户输入的信息


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

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.

It works fine if I hard code the username/password, and fails when I try and access the 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();

    }
}

解决方案

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.

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

这篇关于Windows服务,无法从我的安装程序的构造函数中访问app.config的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆