当 PublishSingleFile 设置为 True 时,Service Fabric 可靠服务中出现未处理的异常

Unhandled exception in Service Fabric reliable service when PublishSingleFile is set to True

这是一个让我难过的奇怪的问题。我有一个 .NET Core 应用程序作为可靠服务部署到 Service Fabric 集群。效果很好,直到我将最后两行添加到 CSPROJ 文件中:

    <OutputType>Exe</OutputType>
    <TargetFramework>netcoreapp3.1</TargetFramework>
    <RuntimeIdentifier>win-x64</RuntimeIdentifier>
    <PublishSingleFile>true</PublishSingleFile>  <-- New
    <PublishTrimmed>true</PublishTrimmed>  <-- New

这将我的程序捆绑到一个 EXE 中。但是,当我部署它时,SF 告诉我应用程序已停止,退出代码为 3762504530,这基本上意味着一些未处理的异常。但是,我可以进入节点并转到 D:\SvcFab_App\ 并直接从命令行查看 EXE 和 运行,它启动正常。

然后我仔细研究了 Windows 事件日志,发现出现了这个错误:

Application: DeviceSync.exe
CoreCLR Version: 4.700.20.26901
.NET Core Version: 3.1.6
Description: The process was terminated due to an unhandled exception.
Exception Info: System.IO.FileNotFoundException: Could not load file or assembly 'netstandard, Version=2.0.0.0, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51'. The system cannot find the file specified.
File name: 'netstandard, Version=2.0.0.0, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51'
   at System.Reflection.RuntimeAssembly.GetType(QCallAssembly assembly, String name, Boolean throwOnError, Boolean ignoreCase, ObjectHandleOnStack type, ObjectHandleOnStack keepAlive, ObjectHandleOnStack assemblyLoadContext)
   at System.Reflection.RuntimeAssembly.GetType(String name, Boolean throwOnError, Boolean ignoreCase)
   at System.Reflection.Assembly.GetType(String name, Boolean throwOnError)
   at System.StartupHookProvider.CallStartupHook(StartupHookNameOrPath startupHook)
   at System.StartupHookProvider.ProcessStartupHooks()

我恢复了对 CSPROJ 文件的更改并再次发布,现在它再次运行良好。

我的问题:当我使用 PublishSingleFile 时,为什么我的程序可以从命令行 运行 正常运行,但 Service Fabric 在 运行 运行相同的应用程序时抛出异常在同一个虚拟机上?

删除 <PublishTrimmed>true</PublishTrimmed>。这仍然是实验性的,可能 trim 来自独立包的一些有效依赖。当您 运行 通过登录节点时它起作用的原因是它在 SF 运行 时间之外并且可能能够从全局计算出来。 只保留 <PublishSingleFile>true</PublishSingleFile>.

如果还是不行,尝试在你的主机.csproj中添加netstandard的nuget包引用<PackageReference Include="NETStandard.Library" Version="2.0.3" />