2017-10-21 76 views
2

我正在測試使用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 

並且沒有收到任何消息。

+0

這是否正常工作之外的碼頭工人? 'EhConnectionString'的價值是什麼? –

+0

也許這會有幫助嗎? https://stackoverflow.com/questions/39246610/keep-a-self-hosted-servicestack-service-open-as-a-docker-swarm-service-without-u/39247585#39247585 – Matt

+0

可能的重複[Keep a自我託管的服務棧服務作爲docker swarm服務打開,而不使用控制檯readline或readkey](https://stackoverflow.com/questions/39246610/keep-a-self-hosted-servicestack-service-open-as-a-docker-一窩蜂的服務,而無需-U) – Matt

回答

6

謝謝大家的建議。

我跟着那些文章,但最終還是結束了這一點,這特別適用於.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(); 
    } 
} 
相關問題