Windows服务无法从App.config获取值

时间:2013-09-05 10:22:00

标签: c# windows-services wix

我创建了简单的Windows服务,如下所示:

   private void TraceService(string content)
        {
            try
            {
                string path = ConfigurationManager.AppSettings["TextLocation"];

                FileStream fs = new FileStream(@path, FileMode.OpenOrCreate, FileAccess.Write);
                StreamWriter sw = new StreamWriter(fs);             
                sw.BaseStream.Seek(0, SeekOrigin.End);                
                sw.WriteLine(content);         
                sw.Flush();             
                sw.Close();
            }
            catch (Exception ex)
            {
                FileStream fs = new FileStream(@"D:\Error.txt", FileMode.OpenOrCreate, FileAccess.Write);            
                StreamWriter sw = new StreamWriter(fs);              
                sw.BaseStream.Seek(0, SeekOrigin.End);               
                sw.WriteLine(ex.Message);
                sw.Flush();               
                sw.Close();
            }
        }

如果我使用 Installutil 在VS命令提示符下运行此服务,则它会从app.config获取值。但我需要使用wixInstaller创建安装程序。当我运行安装程序并启动服务时,它会抛出异常,并且消息保存为“路径不能为空。”这是什么原因?我该如何解决这个问题?

编辑:我的App.config如下:

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <appSettings>
    <add key="TextLocation" value="d:\ScheduledServiceNew.txt"/>
  </appSettings>
</configuration>

我的Wix安装程序文件:

<?xml version="1.0" encoding="utf-8"?>
<Wix xmlns="http://schemas.microsoft.com/wix/2006/wi" xmlns:util="http://schemas.microsoft.com/wix/UtilExtension">
    <Product Id="5AC767F2-4F21-45DF-9DAB-C013C62B2B67" Name="Installer" Language="1033" Version="1.0.0.0" Manufacturer="TRT " UpgradeCode="3fd83540-23f6-45f4-bb1c-e77c12725680">
        <Package InstallerVersion="200" Compressed="yes" />
        <Media Id="1" Cabinet="SampleApp.cab" EmbedCab="yes" />
        <Directory Id="TARGETDIR" Name="SourceDir">
            <Directory Id="ProgramFilesFolder">
                <Directory Id="INSTALLFOLDER" Name="Installer">
                    <Directory Id="TestService" Name="TestService">
                        <Component Id="MainExecutable" Guid="CE30D5D8-3211-4890-A9DC-0C315C15B39E">
                            <File Id="testServiceexe" Name="TestInstallerProject.exe" DiskId="1" KeyPath="yes" Source="..\TestInstallerProject\bin\Debug\TestInstallerProject.exe" />
                            <File Id="App.config" Name="App.config" Source="..\TestInstallerProject\App.config" />
                            <ServiceInstall Name="ScheduledService" Type="ownProcess" Start="auto" ErrorControl="ignore" Id="ServiceInstaller" DisplayName="ScheduledService" Account="[SERVICEACCOUNT]" Password="[SERVICEACCOUNTPASSWORD]">
                                <util:ServiceConfig FirstFailureActionType="restart" SecondFailureActionType="restart" ThirdFailureActionType="restart" ResetPeriodInDays="1" RestartServiceDelayInSeconds="1" />
                            </ServiceInstall>
                            <ServiceControl Id="StartService" Name="ScheduledService" Start="install" Stop="both" Remove="uninstall" Wait="no" />
                            <RemoveFolder Id="INSTALLDIR" On="uninstall" />
                        </Component>
                    </Directory>
                </Directory>
            </Directory>
        </Directory>
        <Feature Id="Complete" Title="Installer" Level="1">
            <ComponentRef Id="MainExecutable" />
        </Feature>
        <UI />
    </Product>
</Wix>

1 个答案:

答案 0 :(得分:5)

我认为问题在于配置文件的名称。 App.config在您的解决方案的上下文中可能是正确的,但生成的exe将期望名称TestInstallerProject.exe.config。我现在还不清楚解决方案,但我会做一些阅读。

解决方案可能就像将Source属性更改为此一样简单:Source="..\TestInstallerProject\bin\Debug\TestInstallerProject.exe.config"