Dotnetcore应用程序在linux下的IWebHostBuilder.Build()上挂起

fla*_*am3 6 c# linux service self-hosting .net-core

我在 Ubuntu 18.04.3 LTS 下将 dotnetcore 应用程序作为服务运行时遇到困难。\n该应用程序在 Windows 上运行正常,大多数时候在 Ubuntu 上也运行正常。但是 Ubuntu 上的多个主机可能会启动服务,但 Web 主机在几个小时内不会初始化,然后在没有外部操作的情况下突然初始化,请参见下面的示例。Journalctl 在突然启动之前不会显示任何有趣的内容。

\n\n
Apr 23 18:52:01 DSK06511.avp.ru MyNode.Api[12276]: Running MyNode as console app\nApr 24 13:42:04 DSK06511.avp.ru MyNode.Api[12276]: IWebHost is built\n
Run Code Online (Sandbox Code Playgroud)\n\n

服务启动正常,状态为“正在运行”

\n\n
root@DSK06511:/home/monouser# service MyNode status\n\xe2\x97\x8f MyNode.service - MyNode\n   Loaded: loaded (/etc/systemd/system/MyNode.service; enabled; vendor preset: enabled)\n   Active: active (running) since Fri 2020-04-24 18:36:31 MSK; 5min ago\n Main PID: 10131 (MyNode)\n    Tasks: 14 (limit: 4915)\n   CGroup: /system.slice/MyNode.service\n           \xe2\x94\x9c\xe2\x94\x8010131 /home/monouser/.octopus/Applications/OctopusServer/Production/MyNode.Linux/4.0.1.907/MyNode --console\n           \xe2\x94\x9c\xe2\x94\x8010220 /home/monouser/.octopus/Applications/OctopusServer/Production/MyNode.Linux/4.0.1.907/MyNode --console\n           \xe2\x94\x9c\xe2\x94\x8010264 /home/monouser/.octopus/Applications/OctopusServer/Production/MyNode.Linux/4.0.1.907/MyNode --console\n           \xe2\x94\x94\xe2\x94\x8010285 /home/monouser/.octopus/Applications/OctopusServer/Production/MyNode.Linux/4.0.1.907/MyNode --console\n\nApr 24 18:36:31 DSK06511.avp.ru systemd[1]: Started MyNode.\nApr 24 18:36:31 DSK06511.avp.ru MyNode[10131]: Running MmNode as console app\nApr 24 18:36:31 DSK06511.avp.ru MyNode[10131]: Building host...\n
Run Code Online (Sandbox Code Playgroud)\n\n

Program.cs内容如下:

\n\n
public static async Task Main(string[] args)\n{\n    var isService = !Debugger.IsAttached && !args.Contains(ConsoleRunOption);\n    Console.WriteLine("Running as " + (isService ? "service" : "console app"));\n    IWebHostBuilder builder = null;\n    try\n    {\n        builder = CreateWebHostBuilder(args.Where(p => p != ConsoleRunOption).ToArray());\n    }\n    catch (Exception e)\n    {\n        // logging here details...\n        Environment.Exit(-1);\n    }\n\n    if (isService)\n    {\n        builder.UseContentRoot(Path.GetDirectoryName(Process.GetCurrentProcess().MainModule.FileName));\n        var host = builder.Build();\n        using ServiceBase service = new ProgramHostService(host);\n        ServiceBase.Run(service);\n        Console.WriteLine("After ServiceBase.Run");\n    }\n    else\n    {\n        Console.WriteLine("Building host...");   //this is the last line written to console\n        var host = builder.Build();              //hangs here\n        Console.WriteLine("IWebHost is built");  //may finish in hours\n        var initializer = host.Services.GetRequiredService<Initializer>();\n        Console.WriteLine("Running InitializeAsync...");\n        await initializer.InitializeAsync();\n        Console.WriteLine("Running IWebHost...");\n        host.Run();\n    }\n}\n
Run Code Online (Sandbox Code Playgroud)\n\n

我的服务文件运行为.../MyNode --console(请参阅下面的完整内容):

\n\n
root@DSK06511:/home/monouser# cat  /etc/systemd/system/MyNode.service\n[Unit]\nDescription=MyNode\n\n[Service]\nType=simple\n\nUser=root\nGroup=root\n\nExecStart=/home/monouser/.octopus/Applications/OctopusServer/Production/MyNode.Linux/4.0.1.907/MyNode --console\n\n[Install]\nWantedBy=multi-user.target\n
Run Code Online (Sandbox Code Playgroud)\n\n

不幸的是,采取 dotnet-dump 也无法解决该错误Writing dump failed (HRESULT: 0x80004005),但这超出了范围。

\n\n

我猜该服务在某种程度上配置错误,但是什么导致它有时工作有时不工作?..提前致谢。

\n