在 Linux 下无法使用 .Net Core 等待 Task.Delay

oze*_*ter 6 c# linux async-await .net-core

这段非常简单的代码在 Linux 下的工作方式与在我的 Windows 机器上的工作方式不同:

    class Program
    {
        async static Task Main(string[] args)
        {
            Console.WriteLine("Hello World!");

            for (int i = 0; i < 5; i++)
            {
                await Task.Delay(TimeSpan.FromSeconds(1));
                Console.WriteLine("" + i);
            }

            Console.WriteLine("Bye bye");
        }
    }
Run Code Online (Sandbox Code Playgroud)

这应该产生类似的输出

Hello World

0

1

.. 

4

Bye bye
Run Code Online (Sandbox Code Playgroud)

它在开发中运行良好,如果发布到 Windows。如果为 Linux 发布,它会在发布“Hello World”后挂起。await Task.Delay() 永远不会返回。

我尝试过全新安装 Ubuntu 18 和 CentOS 7。我尝试过 ASP.Net Core 3.0 运行时和 3.1(也是全新安装)。

此代码仅用于演示目的。最初,我System.Threading.Timer在 ASP.Net BackgroundService(通过 services.AddHostedService() 注册)中使用 a 时开始遇到问题。那里的计时器回调也没有被调用。这也只发生在 Linux 下,而 Windows 运行良好。

有谁知道如何解决这个问题?

更新 Herohtar 发表评论后,我在 Windows 子系统 Ubuntu 上进行了测试。它也在那里工作。现在我怀疑我的主机(Strato.de 上的虚拟服务器)的 Linux 映像不知何故损坏了。

有人能想到 Linux 中可能导致此类问题的限制吗?

oze*_*ter 1

得到 Strato 的回复后,我可以在这里发布我自己问题的答案:Strato 正在使用 Virtuozzo 进行虚拟化。出于安全原因,他们还禁用了一些功能,以便 .Net core 不会在那里运行。

对于遇到同样问题的其他人:您根本无法在 Strato Linux V-Server 上使用 .Net core。

感谢每个试图提供帮助的人!