title: Disable Ctrl+C Shutdown in ASP.NET Core Web API Application
body: <p>I am trying to disable the automatically enabled &quot;Ctrl+C&quot; shutdown for an API Host created in an ASP.NET Core Web Application.  The same question was asked previously <a href="https://stackoverflow.com/questions/66502208/how-to-disable-ctrlc-in-asp-net-core-host">here</a>, but it doesn't work for me using the code below -- the &quot;Press Ctrl+C&quot; message is still shown on startup and, indeed, pressing Ctrl+C stops the application.</p>
  <p>How can I turn off this behavior?</p>
  <p>Adding Console.CancelKeyPress handler to trap the Ctrl-C doesn't work -- setting 'Cancel = True' doesn't stop the ASP.NET host from seeing the Ctrl-C and halting.</p>
  <p>Here's the code:</p>
  <pre><code>public static void Main(string[] args) {
      var tokenSource = new CancellationTokenSource();
      var task = CreateHostBuilder(args).Build().RunAsync(tokenSource.Token);
      while (!task.IsCompleted) {
          // TODO: Other stuff...
          Thread.Sleep(1000);
      };
  }
  
  public static IHostBuilder CreateHostBuilder(string[] args) =&gt;
      Host.CreateDefaultBuilder(args)
          .ConfigureWebHostDefaults(webBuilder =&gt; {
              webBuilder.UseStartup&lt;Startup&gt;();
          });
  </code></pre>
  <p>And here's the output on application start (note that the &quot;Press Ctrl+C...&quot; message is still there):</p>
  <blockquote>
  <pre><code>info: Microsoft.Hosting.Lifetime[0]
        Now listening on: https://localhost:5001
  info: Microsoft.Hosting.Lifetime[0]
        Now listening on: http://localhost:5000
  info: Microsoft.Hosting.Lifetime[0]
        Application started. Press Ctrl+C to shut down.
  info: Microsoft.Hosting.Lifetime[0]
        Hosting environment: Development
  info: Microsoft.Hosting.Lifetime[0]
        Content root path: &lt;path&gt;
  </code></pre>
  </blockquote>