我已经使用顶部架子创建了一个.net核心控制台应用程序。但是使用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服务运行
答案 0 :(得分:3)
要与Topshelf一起使用,您将需要在Windows操作系统上运行。 Topshelf的开发人员经常在Windows 7和Windows Server 2008RC2上进行测试。只要安装了.Net 3.5 sp1,它仍然可以在Windows Server 2003上运行。
好消息是writing Linux daemons比Windows Services更容易-实际上,它们只需要一个控制台应用程序即可在其中控制主循环。
如果我的问题陈述正确无误,那么您希望能够在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服务运行来解决。 在这种情况下,您有一些不涉及编程而是配置写入的选项:
.exe
答案 1 :(得分:1)
因此,您的要求是将dotnet核心(哪个版本?)应用程序作为Windows服务运行。
TopShelf可能不是正确的工具,因为它支持.NET Framework 4.0或Mono,而不是dotnet核心。
由于您要运行Windows服务,因此将您的应用程序发布为Linux Docker映像没有任何意义!使用sc create
和sc start
来注册和启动发布的可执行文件。
答案 2 :(得分:0)
Topshelf不是.NET Core的理想选择,因为.Net Core具有用于构建Windows Service的强大功能。此外,TopShelf仅支持Windows。 查看示例:
https://medium.com/@tocalai/create-windows-service-using-net-core-console-application-dc2f278bbe42
https://codeburst.io/create-a-windows-service-app-in-net-core-3-0-5ecb29fb5ad0