简体   繁体   English

linux下的Topshelf和.net core

[英]Topshelf and .net core under linux

I have a simple application that starts as a service using topshelf and it looks simple:我有一个简单的应用程序,它使用 topshelf 作为服务启动,它看起来很简单:

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

Well it works, but under windows.好吧,它有效,但在 windows 下。 When I tried this under Linux I am getting:当我在 Linux 下尝试这个时,我得到:

Topshelf.Runtime.Windows.WindowsHostEnvironment Error: 0 : Unable to get parent process (ignored), System.DllNotFoundException: Unable to load shared library 'kernel32.dll' or one of its dependencies. Topshelf.Runtime.Windows.WindowsHostEnvironment 错误:0:无法获取父进程(被忽略),System.DllNotFoundException:无法加载共享库“kernel32.dll”或其依赖项之一。 In order to help diagnose loading problems, consider setting the LD_DEBUG environment variable: libkernel32.dll: cannot open shared object file: No such file or directory为了帮助诊断加载问题,请考虑设置 LD_DEBUG 环境变量:libkernel32.dll: cannot open shared object file: No such file or directory

Has someone came across this problem?有人遇到过这个问题吗? I tried to google it but someone said it works other that it is tool only for windows.我试图用谷歌搜索它,但有人说它可以工作,因为它是仅适用于 Windows 的工具。

Or maybe there is some other service hoisting framework for .net core?或者也许还有其他一些适用于 .net core 的服务提升框架?

Assuming that you installed this version of Topshelf - you would notice under dependencies that it doesn't support .NET Core and therefore it will not run under a Linux environment.假设您安装了版本的 Topshelf - 您会在依赖项下注意到它不支持 .NET Core,因此它不会在 Linux 环境下运行。

It will only run under a Windows environment as you mentioned in your post.正如您在帖子中提到的,它只能在 Windows 环境下运行。 kernel32.dll is a Windows dependency that it cannot find, therefore it cannot run. kernel32.dll是它无法找到的 Windows 依赖项,因此无法运行。

Topshelf is not cross-platform and so it does not support .Net Core on non-Windows environments, even if it can run in them (at least at the time of writing). Topshelf 不是跨平台的,因此它不支持非 Windows 环境中的 .Net Core,即使它可以在它们中运行(至少在撰写本文时)。

The solution is to change the environment builder.解决方案是更改环境构建器。 Here is an example from my project, when creating the service:这是我的项目中的一个示例,在创建服务时:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM