如何在Docker容器中保持.NET Core控制台应用程序活着
问题描述:
我正在测试使用Service Bus SDK从Event Hub中检索消息的.NET Core 2.0应用程序。我设置了一个控制台应用程序来做到这一点,并打算将该应用程序作为Docker容器运行。如何在Docker容器中保持.NET Core控制台应用程序活着
此方法创建事件主机处理器将读取消息:
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);
}
正如EventProcessor
类实现的事件处理器会,我试图阻止控制台应用程序退出一个处理事件当处理器的注册完成时。
但是,我找不到一个可靠的方法来保持应用程序的活着。如果我按原样运行此容器,则我在输出窗口中看到的所有内容为:
Registering EventProcessor...
Retrieving messages
Sleeping
并且没有收到任何消息。
答
谢谢大家的建议。
我跟着那些文章,但最终还是结束了这一点,这特别适用于.NET应用程序的核心:
https://github.com/aspnet/Hosting/issues/870
我测试过它,应用程序可以关闭,当它接收到终止信号正常来自Docker运行时。
UPDATE:这是从上面的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();
}
}
这是否正常工作之外的码头工人? 'EhConnectionString'的价值是什么? –
也许这会有帮助吗? https://*.com/questions/39246610/keep-a-self-hosted-servicestack-service-open-as-a-docker-swarm-service-without-u/39247585#39247585 – Matt
可能的重复[Keep a自我托管的服务栈服务作为docker swarm服务打开,而不使用控制台readline或readkey](https://*.com/questions/39246610/keep-a-self-hosted-servicestack-service-open-as-a-docker-一窝蜂的服务,而无需-U) – Matt