title: how to catch exit signal in asp.net core on linux?
body: <p>I am writing a c# console app base on net core 3.1 linux</p>
  <p>It was expected to</p>
  <ul>
  <li>run job async</li>
  <li>await job end</li>
  <li>catch the kill signal and do some clean job</li>
  </ul>
  <p>here is my demo code:</p>
  <pre class="lang-cs prettyprint-override"><code>
  namespace DeveloperHelper
  {
      public class Program
      {
          public static async Task Main(string[] args)
          {
              var http = new SimpleHttpServer();
              var t = http.RunAsync();
              Console.WriteLine(&quot;Now after http.RunAsync();&quot;);
              AppDomain.CurrentDomain.UnhandledException += (s, e) =&gt; {
                  var ex = (Exception)e.ExceptionObject;
                  Console.WriteLine(ex.ToString());
                  Environment.Exit(System.Runtime.InteropServices.Marshal.GetHRForException(ex));
              };
              AppDomain.CurrentDomain.ProcessExit +=  async (s, e) =&gt;
              {
                  Console.WriteLine(&quot;ProcessExit!&quot;);
                  await Task.Delay(new TimeSpan(0,0,1));
                  Console.WriteLine(&quot;ProcessExit! finished&quot;);
              };
              await Task.WhenAll(t);
          }
      }
      public class SimpleHttpServer
      {
          private readonly HttpListener _httpListener;
          public SimpleHttpServer()
          {
              _httpListener = new HttpListener();
              _httpListener.Prefixes.Add(&quot;http://127.0.0.1:5100/&quot;);
          }
          public async Task RunAsync()
          {
              _httpListener.Start();
              while (true)
              {
                  Console.WriteLine(&quot;Now in  while (true)&quot;);
                  var context = await _httpListener.GetContextAsync();
                  var response = context.Response;
  
                  const string rc = &quot;{\&quot;statusCode\&quot;:200, \&quot;data\&quot;: true}&quot;;
                  var rbs = Encoding.UTF8.GetBytes(rc);
                  var st = response.OutputStream;
  
                  response.ContentType = &quot;application/json&quot;;
                  response.StatusCode = 200;
  
                  await st.WriteAsync(rbs, 0, rbs.Length);
                  context.Response.Close();
              }
          }
      }
  }
  
  </code></pre>
  <p>expect it will print</p>
  <pre><code>Now in  while (true)
  Now after http.RunAsync();
  ProcessExit!
  ProcessExit! finished
  </code></pre>
  <p>but it only output</p>
  <pre class="lang-sh prettyprint-override"><code>$ dotnet run
  Now in  while (true)
  Now after http.RunAsync();
  ^C%
  </code></pre>
  <p>does the async/await block the kill signal to be watched by eventHandler?</p>
  <p>the unexpected exception eventHandler do not have any output too.</p>
  <p>is there any <code>signal.signal(signal.SIGTERM, func)</code> in asp.net core?</p>