简体   繁体   English

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

[英]Cannot await Task.Delay with .Net Core under Linux

This very simple code does not work the same under Linux as it does on my Windows machine:这段非常简单的代码在 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");
        }
    }

This should produce an output like这应该产生类似的输出

Hello World

0

1

.. 

4

Bye bye

It works well in development and if published to Windows.它在开发中运行良好,如果发布到 Windows。 If published for Linux it hangs after putting out 'Hello World'.如果为 Linux 发布,它会在发布“Hello World”后挂起。 The await Task.Delay() never returns. await Task.Delay() 永远不会返回。

I've tried with a fresh installation of Ubuntu 18 and CentOS 7. I've tried with ASP.Net Core 3.0 runtime and 3.1 (also both fresh installations).我尝试过全新安装 Ubuntu 18 和 CentOS 7。我尝试过 ASP.Net Core 3.0 运行时和 3.1(也是全新安装)。

This code is only for demonstration purposes.此代码仅用于演示目的。 Originally, I started to have problems when using a System.Threading.Timer in an ASP.Net BackgroundService (registered via services.AddHostedService()).最初,我在 ASP.Net BackgroundService(通过 services.AddHostedService() 注册)中使用System.Threading.Timer时开始遇到问题。 The timer's callback there also didn't got called.那里的计时器回调也没有被调用。 That also happened only under Linux, whereas Windows works well.这也只发生在 Linux 下,而 Windows 运行良好。

Does anyone have an idea how to approach this problem?有谁知道如何解决这个问题?

Update After the comment from Herohtar I've tested on a Windows subsystem Ubuntu.更新Herohtar 发表评论后,我在 Windows 子系统 Ubuntu 上进行了测试。 It is working there as well.它也在那里工作。 Now I suspect the Linux image(s) of my hoster (virtual server at Strato.de) is somehow crippled.现在我怀疑我的主机(Strato.de 上的虚拟服务器)的 Linux 映像不知何故损坏了。

Can someone think of a limitation in Linux that could cause such issues?有人能想到 Linux 中可能导致此类问题的限制吗?

After getting response from Strato I can post the answer to my own question here: Strato is using Virtuozzo for their virtualization.在得到 Strato 的回复后,我可以在这里发布我自己问题的答案:Strato 正在使用 Virtuozzo 进行虚拟化。 For security reasons they have also disabled some features so that .Net core ist not running there.出于安全原因,他们还禁用了一些功能,因此 .Net 核心不在那里运行。

For everyone else having the same problem: You can't use .Net core on Strato Linux V-Servers at all.对于有同样问题的其他人:您根本无法在 Strato Linux V-Server 上使用 .Net 核心。

Thaks to everyone trying to help!感谢所有试图提供帮助的人!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM