1
    [RunInstaller(true)]
    public partial class ProjectInstaller : Installer
    {
        private ServiceInstaller m_serviceInstaller;
        private ServiceProcessInstaller m_processInstaller;


        public ProjectInstaller()
        {            
            string Names= ConfigurationManager.AppSettings["Names"].ToString();
            System.Diagnostics.Debugger.Break();
            m_serviceInstaller = new System.ServiceProcess.ServiceInstaller();
            m_processInstaller = new System.ServiceProcess.ServiceProcessInstaller();
            // 
            // serviceProcessInstaller1
            // 
            m_processInstaller.Account = System.ServiceProcess.ServiceAccount.LocalSystem;
            m_processInstaller.Password = null;
            m_processInstaller.Username = null;
            // 
            // serviceInstaller1
            //       
            m_serviceInstaller.ServiceName = "Testing" + Names;
            m_serviceInstaller.DisplayName = "Testing" + Names;
            m_serviceInstaller.Description = "Testing" + Names;
            // 
            // ProjectInstaller
            // 
            this.Installers.AddRange(new System.Configuration.Install.Installer[] {
            m_processInstaller,
            m_serviceInstaller});
        }
    }

上記の私のプロジェクトインストーラーコードでは、コンパイルエラーは発生しませんが、インストールしようとすると、例外 System.Null referenceException がスローされるため、ロールバックされます。また、問題なく AppConfig を再確認しました。

  <appSettings>       
    <add key="Names" value="BOC" />
  </appSettings>

どこが間違っているのかわかるかもしれません。

4

1 に答える 1

3

同じ問題がありました。サービスのインストール フェーズでは、デフォルトで <frameworkpath>\InstallUtil.exe.Config を読み取っているように見えます (例: C:\Windows\Microsoft.NET\Framework\v4.0.30319\InstallUtil.exe.Config)。

回避策は、正しい構成を手動でロードすることです。

using System.Reflection;
...

Configuration config = ConfigurationManager.OpenExeConfiguration(
    Assembly.GetExecutingAssembly().Location);

KeyValueConfigurationElement element = config.AppSettings.Settings["Names"];

string names = null;
if (element != null)
    names = element.Value;
于 2016-03-21T08:31:40.050 に答える