如何在Docker容器中保持.NET Core控制台应用程序的活动

Lui*_*ado 8 docker .net-core

我正在测试一个.NET Core 2.0应用程序,该应用程序使用Service Bus SDK从事件中心检索消息.我设置了一个控制台应用程序,并打算将该应用程序作为Docker容器运行.

此方法创建将读取消息的事件主机处理器:

    private static async Task MainAsync(string[] args)
    {
        Console.WriteLine("Registering EventProcessor...");

        var eventProcessorHost = new EventProcessorHost(
            EhEntityPath,
            PartitionReceiver.DefaultConsumerGroupName,
            EhConnectionString,
            StorageConnectionString,
            StorageContainerName);

        // Registers the Event Processor Host and starts receiving messages
        Console.WriteLine("Retrieving messages");
        await eventProcessorHost.RegisterEventProcessorAsync<EventProcessor>();

        Console.WriteLine("Sleeping");
        Thread.Sleep(Timeout.Infinite);
    }
Run Code Online (Sandbox Code Playgroud)

由于在类中实现的事件处理器EventProcessor将是处理事件的事件处理器,我试图阻止控制台应用程序在处理器的注册完成时退出.

但是,我找不到一种可靠的方法来保持应用程序的活力.如果我按原样运行此容器,我在输出窗口中看到的只有:

Registering EventProcessor...
Retrieving messages
Sleeping
Run Code Online (Sandbox Code Playgroud)

并且没有收到任何消息.

Lui*_*ado 11

谢谢大家的建议.

我遵循了这些文章但最终以此结束,这特别适用于.NET Core应用程序:

https://github.com/aspnet/Hosting/issues/870

我测试了它,当它从Docker运行时收到终止信号时,应用程序可以正常关闭.

更新:这是上面GH问题链接的相关样本:

public class Program
{
    public static void Main(string[] args)
    {
        var ended = new ManualResetEventSlim();
        var starting = new ManualResetEventSlim();

        AssemblyLoadContext.Default.Unloading += ctx =>
        {
            System.Console.WriteLine("Unloding fired");
            starting.Set();
            System.Console.WriteLine("Waiting for completion");
            ended.Wait();
        };

        System.Console.WriteLine("Waiting for signals");
        starting.Wait();

        System.Console.WriteLine("Received signal gracefully shutting down");
        Thread.Sleep(5000);
        ended.Set();
    }
}
Run Code Online (Sandbox Code Playgroud)