如何使WriteAsync
错误更快?从连接的设备中拔出网络插头以将其注册为问题后,以下代码需要20秒!
try {
Util.Logger.Info("sending space");
await ctx.Response.OutputStream.WriteAsync(Consts.SingleSpaceBytes, 0, 1);
// await ctx.Response.OutputStream.FlushAsync();
Util.Logger.Info("space sent");
await Task.Delay(1000);
}
catch (Exception ex) {
Util.Logger.Error(ex, $"Error sending space!");
break;
}
输出:
2019-07-09 15:45:49.0156 sending space // disconnect cable here
2019-07-09 15:45:49.0156 space sent
...
2019-07-09 15:46:09.1709 sending space
2019-07-09 15:46:09.2219 Error sending space! // some sort of timeout here
System.Net.HttpListenerException (1229): An operation was attempted on a nonexistent network connection
理想情况下,我希望它立即失败,但是少于20秒的任何改进都是可以的。