Created
March 17, 2017 21:58
-
-
Save cphillips83/a08f75caf8e9539573afa44c3a4e1c1b to your computer and use it in GitHub Desktop.
Kestrel not shutting down gracefully
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.IO; | |
using System.Threading; | |
using Microsoft.AspNetCore.Builder; | |
using Microsoft.AspNetCore.Hosting; | |
using Microsoft.AspNetCore.Mvc; | |
namespace demo | |
{ | |
[Route("api/[controller]")] | |
public class ValuesController : Controller | |
{ | |
public static bool stopping = false; | |
// GET api/values/5 | |
[HttpGet("{id}")] | |
public string Get(int id) | |
{ | |
while(!stopping){ | |
Console.WriteLine("Sleeping for 1s"); | |
System.Threading.Thread.Sleep(1000); | |
} | |
System.Threading.Thread.Sleep(1000); | |
Console.WriteLine("Done sleeping"); | |
return "value"; | |
} | |
// DELETE api/values/5 | |
[HttpDelete("{id}")] | |
public void Delete(int id) | |
{ | |
stopping = true; | |
Console.WriteLine("Received shutdown request..."); | |
// var appLifeCycle = HttpContext.RequestServices.GetService(typeof(IApplicationLifetime)) as IApplicationLifetime; | |
// appLifeCycle.StopApplication(); | |
Program.TokenSource.Cancel(false); | |
} | |
} | |
public class Program | |
{ | |
public static CancellationTokenSource TokenSource{get;private set;} | |
public static void Main(string[] args) | |
{ | |
var host = new WebHostBuilder() | |
.UseKestrel(options => { options.ShutdownTimeout = TimeSpan.FromSeconds(5);}) | |
.UseContentRoot(Directory.GetCurrentDirectory()) | |
.UseIISIntegration() | |
.UseStartup<Startup>() | |
.Build(); | |
TokenSource = new CancellationTokenSource(); | |
var token = TokenSource.Token; | |
host.Run(token); | |
Console.WriteLine("quitting"); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment