【问题标题】:Create long running .NET Core service targeting Linux Docker container创建面向 Linux Docker 容器的长时间运行的 .NET Core 服务
【发布时间】:2017-06-14 14:49:00
【问题描述】:

似乎有两种实质性的 .NET Core 项目:ASP.NET Core Web App 和 Console App。我想在 Docker 环境(Linux 容器)中构建类似于 Windows 服务的东西,进程在其中启动、无限期运行,并且仅在被告知时才停止。两种项目类型似乎都不合适。我错过了什么吗?

【问题讨论】:

  • Windows 服务基本上是由服务管理器启动/停止的控制台应用程序。

标签: c# docker .net-core


【解决方案1】:

两种类型的应用程序都有意义,这取决于您计划如何与此服务通信。

如果您想在某个 TCP 端口上通过标准 HTTP 与它通信,那么使用 ASP.Net Core Web 应用程序会让事情变得更容易。

如果您想通过更“异国情调”的东西(例如 RabbitMQ、Kafka、原始 TCP 套接字或其他东西)进行通信,那么控制台应用程序就是您想要的。正如 Gareth Luckett 的回答所指出的那样,诀窍就是确保您的 main 功能块。一个正在运行的 Docker 容器期望主线程在容器应该运行时阻塞。

【讨论】:

    【解决方案2】:

    “控制台”一词在这里可能有点误导。 Microsoft 使用它来将其与“GUI”应用程序(如 WinForms、WPF、UWP、Xamarin 等)或通过 IIS 带来的 Web 应用程序区分开来。 ASP.NET Core 应用程序只是带有用于托管 Web 服务器的库的控制台应用程序。

    因此,对于您的应用,“控制台”是您想要的项目类型。正如@mason 所提到的,即使是 Windows 服务也只是“控制台”应用程序——一个不是 GUI 应用程序的 .exe 文件。

    【讨论】:

      【解决方案3】:

      不幸的是,控制台应用程序在运行时需要标准输入,通过 docker 它将立即退出。您可以使用 asp.net 来“托管”它。

      public class Program
      {
          public static ManualResetEventSlim Done = new ManualResetEventSlim(false);
          public static void Main(string[] args)
          {
              //This is unbelievably complex because .NET Core Console.ReadLine() does not block in a docker container...!
              var host = new WebHostBuilder().UseStartup(typeof(Startup)).Build();
      
              using (CancellationTokenSource cts = new CancellationTokenSource())
              {
                  Action shutdown = () =>
                  {
                      if (!cts.IsCancellationRequested)
                      {
                          Console.WriteLine("Application is shutting down...");
                          cts.Cancel();
                      }
      
                      Done.Wait();
                  };
      
                  Console.CancelKeyPress += (sender, eventArgs) =>
                  {
                      shutdown();
                      // Don't terminate the process immediately, wait for the Main thread to exit gracefully.
                      eventArgs.Cancel = true;
                  };
      
                  host.Run(cts.Token);
                  Done.Set();
              }
          }      
      }
      

      启动类:

      public class Startup
      {
          public void ConfigureServices(IServiceCollection services)
          {
              services.AddSingleton<IServer, ConsoleAppRunner>();
          }
      
      
          public void Configure(IApplicationBuilder app, IHostingEnvironment env)
          {
          }
      }
      

      ConsoleAppRunner 类

      public class ConsoleAppRunner : IServer
      {
      
          /// <summary>A collection of HTTP features of the server.</summary>
          public IFeatureCollection Features { get; }
      
          public ConsoleAppRunner(ILoggerFactory loggerFactory)
          {
              Features = new FeatureCollection();
          }
      
          /// <summary>Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources.</summary>
          public void Dispose()
          {
      
          }
      
          /// <summary>Start the server with an application.</summary>
          /// <param name="application">An instance of <see cref="T:Microsoft.AspNetCore.Hosting.Server.IHttpApplication`1" />.</param>
          /// <typeparam name="TContext">The context associated with the application.</typeparam>
          public void Start<TContext>(IHttpApplication<TContext> application)
          {
              //Actual program code starts here...
              Console.WriteLine("Demo app running...");
      
              Program.Done.Wait();        // <-- Keeps the program running - The Done property is a ManualResetEventSlim instance which gets set if someone terminates the program.
      
          }
      }
      

      来源:https://stackoverflow.com/a/40549512/2238275

      【讨论】:

        猜你喜欢
        • 2020-11-20
        • 2020-01-22
        • 1970-01-01
        • 1970-01-01
        • 2018-07-01
        • 2023-03-25
        • 2021-10-26
        • 1970-01-01
        • 2018-05-06
        相关资源
        最近更新 更多