【问题标题】:Topshelf and .net core under linuxlinux下的topshelf和.net core
【发布时间】:2019-11-07 04:10:42
【问题描述】:

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

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

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

Topshelf.Runtime.Windows.WindowsHostEnvironment 错误:0:无法获取父进程(忽略),System.DllNotFoundException:无法加载共享库“kernel32.dll”或其依赖项之一。为了帮助诊断加载问题,请考虑设置 LD_DEBUG 环境变量:libkernel32.dll: cannot open shared object file: No such file or directory

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

或者也许还有一些其他的.net core 服务提升框架?

【问题讨论】:

  • WojciechSzabowicz 你愿意接受@Paul-Sebastian Manole 的回答吗?看起来这是你问题的正确答案,他花时间解释了

标签: c# topshelf


【解决方案1】:

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>();
});

【讨论】:

    【解决方案2】:

    假设您安装了 this 版本的 Topshelf - 您会在依赖项下注意到它不支持 .NET Core,因此它不会在 Linux 环境下运行。

    它只能在您在帖子中提到的 Windows 环境下运行。 kernel32.dll 是一个无法找到的 Windows 依赖项,因此无法运行。

    【讨论】:

    • 是的,它是 4.2.0,但是通过主机工厂的 API,我遇到了 UseEnvironmentBuilder(这个方法在官方文档中没有描述,就像它不存在一样)在描述中它说默认值是 WindowsHostEnvironmentBuilder,当我将其更改为 DotNetCoreEnvironmentBuilder 时,它可以在 Linux 上运行。
    猜你喜欢
    • 2020-06-29
    • 1970-01-01
    • 1970-01-01
    • 2019-04-19
    • 1970-01-01
    • 1970-01-01
    • 2019-10-05
    • 2017-11-04
    • 1970-01-01
    相关资源
    最近更新 更多