Dotnetcore application hangs on IWebHostBuilder.Build() under linux

0

I have difficulties with running dotnetcore application as a service under Ubuntu 18.04.3 LTS. The application works OK on Windows, and most of the times on Ubuntu too. But several hosts on Ubuntu may start service, but web host won't initialize for several hours and then suddenly initialize without outside action, see example below. Journalctl won't show nothing interesting before sudden startup.

Apr 23 18:52:01 DSK06511.avp.ru MyNode.Api[12276]: Running MyNode as console app
Apr 24 13:42:04 DSK06511.avp.ru MyNode.Api[12276]: IWebHost is built

Service is starting OK, status is "running"

root@DSK06511:/home/monouser# service MyNode status
● MyNode.service - MyNode
   Loaded: loaded (/etc/systemd/system/MyNode.service; enabled; vendor preset: enabled)
   Active: active (running) since Fri 2020-04-24 18:36:31 MSK; 5min ago
 Main PID: 10131 (MyNode)
    Tasks: 14 (limit: 4915)
   CGroup: /system.slice/MyNode.service
           ├─10131 /home/monouser/.octopus/Applications/OctopusServer/Production/MyNode.Linux/4.0.1.907/MyNode --console
           ├─10220 /home/monouser/.octopus/Applications/OctopusServer/Production/MyNode.Linux/4.0.1.907/MyNode --console
           ├─10264 /home/monouser/.octopus/Applications/OctopusServer/Production/MyNode.Linux/4.0.1.907/MyNode --console
           └─10285 /home/monouser/.octopus/Applications/OctopusServer/Production/MyNode.Linux/4.0.1.907/MyNode --console

Apr 24 18:36:31 DSK06511.avp.ru systemd[1]: Started MyNode.
Apr 24 18:36:31 DSK06511.avp.ru MyNode[10131]: Running MmNode as console app
Apr 24 18:36:31 DSK06511.avp.ru MyNode[10131]: Building host...

Program.cs content is below:

public static async Task Main(string[] args)
{
    var isService = !Debugger.IsAttached && !args.Contains(ConsoleRunOption);
    Console.WriteLine("Running as " + (isService ? "service" : "console app"));
    IWebHostBuilder builder = null;
    try
    {
        builder = CreateWebHostBuilder(args.Where(p => p != ConsoleRunOption).ToArray());
    }
    catch (Exception e)
    {
        // logging here details...
        Environment.Exit(-1);
    }

    if (isService)
    {
        builder.UseContentRoot(Path.GetDirectoryName(Process.GetCurrentProcess().MainModule.FileName));
        var host = builder.Build();
        using ServiceBase service = new ProgramHostService(host);
        ServiceBase.Run(service);
        Console.WriteLine("After ServiceBase.Run");
    }
    else
    {
        Console.WriteLine("Building host...");   //this is the last line written to console
        var host = builder.Build();              //hangs here
        Console.WriteLine("IWebHost is built");  //may finish in hours
        var initializer = host.Services.GetRequiredService<Initializer>();
        Console.WriteLine("Running InitializeAsync...");
        await initializer.InitializeAsync();
        Console.WriteLine("Running IWebHost...");
        host.Run();
    }
}

My service file is run as .../MyNode --console (see full below):

root@DSK06511:/home/monouser# cat  /etc/systemd/system/MyNode.service
[Unit]
Description=MyNode

[Service]
Type=simple

User=root
Group=root

ExecStart=/home/monouser/.octopus/Applications/OctopusServer/Production/MyNode.Linux/4.0.1.907/MyNode --console

[Install]
WantedBy=multi-user.target

Unfortunately taking dotnet-dump also won't work with the error Writing dump failed (HRESULT: 0x80004005) but that's out of scope.

I'm guessing the service is configured wrong somehow, but what causes it to work sometimes and sometimes not?.. Thanks in advance.

c#
linux
.net-core
service
self-hosting
asked on Stack Overflow Apr 24, 2020 by flam3 • edited Apr 25, 2020 by flam3

0 Answers

Nobody has answered this question yet.


User contributions licensed under CC BY-SA 3.0