Topshelf and .net core under linux
Asked Answered
T

2

5

I have a simple application that starts as a service using topshelf and it looks simple:

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

Well it works, but under windows. When I tried this under Linux I am getting:

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. 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

Has someone came across this problem? I tried to google it but someone said it works other that it is tool only for windows.

Or maybe there is some other service hoisting framework for .net core?

Ted answered 25/6, 2019 at 10:32 Comment(1)
WojciechSzabowicz would you like to accept @Paul-Sebastian Manole's answer then? Looks like that's the correct answer to you question, which he took the time to explainDecani
B
10

Topshelf is not advertised as cross-platform and so it does not (or did not at the time of writing) official support .Net Core on non-Windows environments, even if it can run in them (at least at the time of writing, see below).

The solution is to change the environment builder when running on non-Windows hosts.

Here is an example from my project. When creating the service, pick the env builder at runtime based on the host OS.

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>();
});
Belemnite answered 11/10, 2020 at 17:4 Comment(3)
I'm not sure I'm following you. This code seems to indicate that a Topshelf service will refuse to start if it's not running on Windows. Are you saying that you've been able to get a Topshelf service to run successfully on Linux?Econah
@InteXX, yes. That is what I showed.Belemnite
This is incredibly good news (for me, at least). I can't begin to tell you how good this news is :-)Econah
T
4

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.

It will only run under a Windows environment as you mentioned in your post. kernel32.dll is a Windows dependency that it cannot find, therefore it cannot run.

Tyika answered 25/6, 2019 at 10:59 Comment(2)
Aye it is 4.2.0, but going throu API of the host factory I came upon UseEnvironmentBuilder (well this method is not described in offical documentation, like it does not exists) in description it says that default value is WindowsHostEnvironmentBuilder, and when i changed that to DotNetCoreEnvironmentBuilder it worked on Linux.Ted
@WojciechSzabowicz — That's very interesting! I imagine the Topshelf install CLI argument doesn't work, though (I found some Windows-only code sprinkled throughout the source—example). Is this what you're finding as well?Econah

© 2022 - 2024 — McMap. All rights reserved.