使用 msi 安装后无法启动 windows 服务

unable to start windows service after installing using msi

我创建了一个 windows 服务项目。在 post 构建事件中,我将输出与另一个项目一起复制到一个公共目录之外。我正在使用 heat 任务生成 MSI 以复制 MSI 中的两个目录。我正在尝试在安装后启动服务。

两个项目都使用 serilog.configuration 加载 json 具有记录器配置的文件。

如果我在installutil的帮助下安装服务,则服务安装成功并完美启动。但是当我尝试使用 MSI 安装时,出现以下错误并且安装永远不会完成。

Service cannot be started. System.IO.FileLoadException: Could not load file or assembly 'Microsoft.Extensions.Configuration.Abstractions, Version=3.1.0.0, Culture=neutral, PublicKeyToken=adb9793829ddae60' or one of its dependencies. The located assembly's manifest definition does not match the assembly reference. (Exception from HRESULT: 0x80131040)
File name: 'Microsoft.Extensions.Configuration.Abstractions, Version=3.1.0.0, Culture=neutral, PublicKeyToken=adb9793829ddae60'
   at Hive.WindowsAgent.Service.HiveAgentService.OnStart(String[] args)
   at System.ServiceProcess.ServiceBase.ServiceQueuedMainCallback(Object state)

WRN: Assembly binding logging is turned OFF.
To enable assembly bind failure logging, set the registry value [HKLM\Software\Microsoft\Fusion!EnableLog] (DWORD) to 1.
Note: There is some performance penalty associated with assembly bind failure logging.
To turn this feature off, remove the registry value [HKLM\Software\Microsoft\Fusion!EnableLog].

我检查了 nuget 包,windows 服务包含 Microsoft.Extensions.Configuration.Abstraction v3.1.5。而且它仅在 MSI 上失败。

我的 Wix 文件:

<?xml version="1.0" encoding="utf-8"?>
<Wix xmlns="http://schemas.microsoft.com/wix/2006/wi">
  <Product Id="*" Name="$(var.ProductName)" Language="1033" Version="$(var.ProductVersion)" Manufacturer="$(var.ProductManufacturer)" UpgradeCode="$(var.ProductUpgradeId)">
    <Package InstallerVersion="200" Compressed="yes" InstallScope="perMachine" />
    <MajorUpgrade DowngradeErrorMessage="A newer version of [ProductName] is already installed." />
    <MediaTemplate EmbedCab="yes" />
    <Feature Id="ProductFeature" Title="HarvestSetup" Level="1">
      <ComponentGroupRef Id="AutogeneratedComponents" />
      <ComponentGroupRef Id="ProductComponents"/>
    </Feature>
    <UI />
  </Product>
  <Fragment>
    <Directory Id="TARGETDIR" Name="SourceDir">
      <Directory Id="ProgramFiles64Folder">
        <Directory Id="INSTALLFOLDER" Name="$(var.ProductManufacturer)" ComponentGuidGenerationSeed="4BDDE809-FF2B-4DF9-B1D6-2DBA45AB122F">
        </Directory>
      </Directory>
    </Directory>
  </Fragment>
  <Fragment>
    <DirectoryRef Id="INSTALLFOLDER">
      <Directory Id="HiveServiceDirectory" Name="HiveService" />
    </DirectoryRef>
    <ComponentGroup Id="ProductComponents"  Directory="HiveServiceDirectory">
      <Component Id="ProductServiceInstaller" Guid="4F512FED-176D-4FBE-AAC2-8333E4B4231F">
        <File Id="$(var.ServiceName)" Name="$(var.ServiceName)" Source="$(var.ProductPath)" KeyPath="yes" />
        <ServiceInstall Id="ServiceInstaller"
                        Type="ownProcess"
                        Name="$(var.ServiceName)"
                        DisplayName="$(var.ServiceName)"
                        Description="Service for Windows agent"
                        Start="auto"
                        Account="LocalSystem"
                        ErrorControl="normal" />
        <ServiceControl Id="StartStopService"
                        Start="install"
                        Stop="both"
                        Remove="uninstall"
                        Name="$(var.ServiceName)"
                        Wait="yes" />
      </Component>
    </ComponentGroup>
  </Fragment>
</Wix>

我错过了什么?

P.S。让我知道是否需要更多信息

在您的 csproj 文件中添加:

   <PropertyGroup>
     <AutoGenerateBindingRedirects>true</AutoGenerateBindingRedirects>
     <GenerateBindingRedirectsOutputType>true</GenerateBindingRedirectsOutputType>
   </PropertyGroup>

自动生成的绑定重定向存在问题。

这应该会强制 MSBuild 创建/更新包含必要绑定重定向的 YourProject.dll.config 文件。

这是我能想到的最愚蠢的事情。幸运的是,我在 .net slack group 上得到了 Kevin and Alex 的很多帮助。

我在 <File Id="$(var.ServiceName)" Name="$(var.ServiceName)" Source="$(var.ProductPath)" KeyPath="yes" />

中缺少 exe 扩展

最终该行应该是

<File Id="$(var.ServiceName)" Name="$(var.ServiceName).exe" Source="$(var.ProductPath)" KeyPath="yes" />