我將OWIN服務器作爲控制檯應用程序的一部分。你可以在這裏看到的主要方法:在停止自託管的owin服務器時完成當前請求
class Program
{
public static ManualResetEventSlim StopSwitch = new ManualResetEventSlim();
static void Main(string[] args)
{
Console.CancelKeyPress += (s, a) =>
{
a.Cancel = true;
StopSwitch.Set();
};
using (WebApp.Start<Startup>("http://+:8080/"))
{
Console.WriteLine("Server is running...");
Console.WriteLine("Press CTRL+C to stop it.");
StopSwitch.Wait();
Console.WriteLine("Server is stopping...");
}
Console.ReadKey();
Console.WriteLine("Server stopped. Press any key to close app...");
}
}
當請求的處理是長一點,並在同一時間用戶按下CTRL + C停止請求處理立即停止,並且不發送響應應用程序。有沒有可能改變這種行爲?我想拒絕所有新請求,但要等到當前正在處理的請求完成並在此之後停止服務器。
我最初的想法是創建OWIN中間件,它將跟蹤當前正在處理的請求並推遲停止操作,直到完成所有操作。中間件也會在停止階段將所有請求短路。但是這個解決方案對我來說聽起來不太好。
聽起來對我來說是個不錯的主意。我認爲你需要_something_來短路新的請求,並且將一些東西放在一邊似乎是正確的做法。對於跟蹤請求,您可以使用基本['Interlocked.Increment'](http://msdn.microsoft.com/en-us/library/system.threading.interlocked.increment(v = vs.110).aspx) /請求開始和結束時的'遞減'計數器,以便知道上次請求何時完成。 – Rhumborl 2014-09-29 08:28:37