UWP中Windows.Web.Http.HttpClient在部分设备上调用无响应问题
为何Windows.Web.Http.HttpClient实例在部分电脑正常运行,另一台却不行?
我曾查阅多篇讨论Windows.Web.Http.HttpClient.GetAsync无法返回控制权的帖子,多数指出死锁问题并建议用ConfigureAwait(false)解决,但这对我的情况无效。
棘手的是,应用在开发机及其他多台设备上运行正常,唯独在新部署的某台设备出问题。应用通过IDE的「发布」功能部署,再在目标设备运行安装程序。我没有目标设备的管理员权限,且安装时未被提示授予应用特定网络权限——这可能是问题根源?
以下是相关代码:
return Task.Factory.StartNew(async () => { if(first_ping_attempt) { Serilog.Log.Information("First ping attempt..."); } HttpClient http = new HttpClient(); var cancellation = new CancellationTokenSource(10000); HttpStringContent content = new HttpStringContent(FormPingPacket(device_ID, deviceName)); content.Headers.ContentType = JSONMediaType; try { var response = await http.PostAsync(PingPostURI, content).AsTask(cancellation.Token).ConfigureAwait(false); if(!response.IsSuccessStatusCode) { Serilog.Log.Error(String.Format("Ping POST response status code {0}\n URL: {1}\n Data:\n{2}", response.StatusCode, PingPostURI, content.ToString())); } if(first_ping_attempt) { Serilog.Log.Information($"POST status: {response.StatusCode}"); } } catch(TaskCanceledException) { Serilog.Log.Error("Ping POST Timeout"); } catch(Exception e) { Serilog.Log.Logger.Error(e, $"SendPing: Exception from http.PostAsync: {e.GetType()}: {e.Message}\n URL: {PingPostURI}"); } content = new HttpStringContent(FormVersionPutPacket()); content.Headers.ContentType = JSONMediaType; cancellation = new CancellationTokenSource(10000); try { var response = await http.PutAsync(DevicePutURI(device_ID), content).AsTask(cancellation.Token).ConfigureAwait(false); // We don't actually care about the response code other than for debugging if(!response.IsSuccessStatusCode) { Serilog.Log.Error(String.Format("Ping PUT response status code {0}\n URL: {1}", response.StatusCode, DevicePutURI(device_ID))); } if(first_ping_attempt) { Serilog.Log.Information($"POST status: {response.StatusCode}"); } } catch(TaskCanceledException) { Serilog.Log.Error("Ping PUT Timeout"); } catch(Exception e) { Serilog.Log.Logger.Error(e, $"SendPing: Exception from http.PutAsync: {e.GetType()}: {e.Message}\n URL: {DevicePutURI(device_ID)}"); } if(first_ping_attempt) { Serilog.Log.Information("First manager ping complete."); first_ping_attempt = false; } }); }
一个UWP计时器每秒调用一次该方法,用_ =语法丢弃返回的Task。Serilog将日志记录到中央日志接收器,日志内容如下:
172.18.0.141 6/12/2025 3:16:07 PM First ping attempt... 172.18.0.141 6/12/2025 3:17:07 PM First ping attempt... 172.18.0.141 6/12/2025 3:18:07 PM First ping attempt... 172.18.0.141 6/12/2025 3:19:07 PM First ping attempt... 172.18.0.141 6/12/2025 3:20:07 PM First ping attempt... 172.18.0.141 6/12/2025 3:21:07 PM First ping attempt... 172.18.0.141 6/12/2025 3:22:07 PM First ping attempt... 172.18.0.141 6/12/2025 3:23:07 PM First ping attempt... 172.18.0.141 6/12/2025 3:24:07 PM First ping attempt... 172.18.0.141 6/12/2025 3:25:07 PM First ping attempt... 172.18.0.141 6/12/2025 3:26:07 PM First ping attempt... 172.18.0.141 6/12/2025 3:27:07 PM First ping attempt...
显然await从未返回,即使设置了10秒后触发的取消令牌。
内容的提问来源于stack exchange,提问作者Brian A. Henning
相关产品推荐
相关产品推荐

