我将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