我创建了如下简单的 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>