首页 > 解决方案 > linux下的topshelf和.net core

问题描述

我有一个简单的应用程序,它使用 topshelf 作为服务启动,它看起来很简单:

 HostFactory.Run(x =>
 {
    x.Service<RequestService>();
    x.RunAsLocalSystem();
 });

好吧,它可以工作,但是在Windows下。当我在 Linux 下尝试这个时,我得到:

Topshelf.Runtime.Windows.WindowsHostEnvironment 错误:0:无法获取父进程(忽略),System.DllNotFoundException:无法加载共享库“kernel32.dll”或其依赖项之一。为了帮助诊断加载问题,请考虑设置 LD_DEBUG 环境变量: libkernel32.dll:无法打开共享对象文件:没有这样的文件或目录

有人遇到过这个问题吗?我试图用谷歌搜索它,但有人说它可以工作,因为它只是用于 Windows 的工具。

或者,.net 核心可能还有其他一些服务提升框架?

标签: c#topshelf

解决方案


Topshelf 不是跨平台的,因此它不支持非 Windows 环境上的 .Net Core,即使它可以在其中运行(至少在撰写本文时)。

解决方案是更改环境构建器。这是我的项目中创建服务时的示例:

HostFactory.Run(c =>
{
  // Change Topshelf's environment builder on non-Windows hosts:
  if (
    RuntimeInformation.IsOSPlatform(OSPlatform.OSX) ||
    RuntimeInformation.IsOSPlatform(OSPlatform.Linux)
  )
  {
    c.UseEnvironmentBuilder(
      target => new DotNetCoreEnvironmentBuilder(target)
    );
  }

  c.SetServiceName("SelloutReportingService");
  c.SetDisplayName("Sellout Reporting Service");
  c.SetDescription(
    "A reporting service that does something...");
  c.StartAutomatically();
  c.RunAsNetworkService();
  c.EnableServiceRecovery(
    a => a.RestartService(TimeSpan.FromSeconds(60))
  );
  c.StartAutomatically();
  c.Service<SelloutReportingService>();
});

推荐阅读