gpt4 book ai didi

docker - 使用 TopShelf 的 .net core 控制台应用程序

转载 作者:行者123 更新时间:2023-12-02 09:42:10 39 4
gpt4 key购买 nike

我使用 TopShelf 创建了一个 .net core 控制台应用程序。但是我在使用 docker (alpine-linux) 运行应用程序时遇到错误。

Configuration Result:
[Success] Name MyApp
[Success] DisplayName MyApp
[Success] Description My Application
[Success] ServiceName MyApp
Topshelf v4.1.0.177, .NET Framework v4.0.30319.42000
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: Error loading shared library libkernel32.dll: No such file or directory
at Topshelf.Runtime.Windows.Kernel32.CreateToolhelp32Snapshot(UInt32 dwFlags, UInt32 th32ProcessID)
at Topshelf.Runtime.Windows.WindowsHostEnvironment.GetParent(Process child)
Topshelf.HostFactory Error: 0 : The service terminated abnormally, System.PlatformNotSupportedException: ServiceController enables manipulating and accessing Windows services and it is not applicable for other operating systems.
at System.ServiceProcess.ServiceController.GetServices()
at Topshelf.Runtime.Windows.WindowsHostEnvironment.IsServiceListed(String serviceName)
at Topshelf.Hosts.ConsoleRunHost.Run()
at Topshelf.HostFactory.Run(Action`1 configureCallback)

如何解决这个问题?我需要将控制台应用程序作为 Windows 服务运行

最佳答案

Topshelf documentation非常具体:

To work with Topshelf you will need to be running on a Windows operating system. The developers of Topshelf regulary test on Windows 7 and Windows Server 2008RC2. Though it should still work on Windows Server 2003, as long as .Net 3.5 sp1 is installed.

好消息是writing Linux daemons比 Windows 服务更容易 - 它们基本上只是一个控制台应用程序,您可以在其中控制主循环。

如果我正确理解了您的问题陈述,您希望能够在 Windows 和 Docker 中运行一项服务。在这种情况下,最简单的方法似乎是在启动时检查您的操作系统环境,例如 System.Runtime.InteropServices.RuntimeInformation.IsOSPlatform()要么将您的主要工作推迟到 Topshelf,要么以 Linux 方式运行。对于下面的示例,我安装了 Microsoft.Extensions.Hosting 包并选择实现 IHostedService (Topshelf 可以方便地重复使用)

public class YourHostedService : IHostedService, IDisposable
{
private int executionCount = 0;
private Timer _timer;

public YourHostedService()
{
}

public Task StartAsync(CancellationToken stoppingToken)
{
_timer = new Timer(DoWork, null, TimeSpan.Zero,
TimeSpan.FromSeconds(5));

return Task.CompletedTask;
}

private void DoWork(object state)
{
executionCount++;// this gets called every 5 seconds
}

public Task StopAsync(CancellationToken stoppingToken)
{
_timer?.Change(Timeout.Infinite, 0);
return Task.CompletedTask;
}

public void Dispose() => _timer?.Dispose();
}

public class Program
{
public static async Task Main(string[] args)
{
if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
{
var rc = HostFactory.Run(x =>
{
var token = CancellationToken.None;
x.Service<YourHostedService>(s =>
{
s.ConstructUsing(name => new YourHostedService());
s.WhenStarted(tc => tc.StartAsync(token));
s.WhenStopped(tc => tc.StopAsync(token));
});
x.RunAsLocalSystem();

x.SetDescription("TopShelf Host");
x.SetDisplayName("YourHostedService");
x.SetServiceName("YourHostedService");
});
}
else
{
await Host.CreateDefaultBuilder(args)
.ConfigureServices(builder =>
{
builder.AddHostedService<YourHostedService>();
})
.RunConsoleAsync();
}
}
}

可以汲取更多灵感from here .

UPD 所以看来您的特殊情况也可以通过将任意(好吧,在本例中是您的)程序作为 Windows 服务运行来解决。在这种情况下,您有一些不涉及编程但涉及配置编写的选项:

  1. Microsoft's own tool SrvAny这是 NT 资源工具包的一部分:您基本上将其安装为虚拟服务并编辑注册表设置以指向您的 .exe
  2. 一个3rd party tool SrvStart :这个也比较容易上手,配置与上面类似

关于docker - 使用 TopShelf 的 .net core 控制台应用程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56037600/

39 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com