我用 Windows.Web.Http.HttpClient
在我的 UWP Windows 10 应用程序中。我创建了 HttpClient
的单个实例在我的应用程序中并重新使用所有 HTTP 调用的连接。一切正常,除了并行 HTTP 调用。
每当我发起并行 HTTP 请求时,并行线程第一次抛出以下 COMException。我捕获了这个异常并重新发送 http 调用,它成功了。过了一会儿,如果我再次启动并行线程,它又失败了。
Exception thrown: 'System.Runtime.InteropServices.COMException' in System.Private.CoreLib.dll
WinRT information: A concurrent or interleaved operation changed the state of the object, invalidating this operation.
尝试冲浪,但无法获得解决方案。有没有人遇到过这个问题并得到解决方案?
这是代码示例:
httpClient is a single reusable instance.
var response = await httpClient.SendRequestAsync(reqMsg).AsTask(cts.Token); if (response.IsSuccessStatusCode) { if (isBufferResponseRequired) { return await response.Content.ReadAsBufferAsync(); } else { return await response.Content.ReadAsStringAsync(); } }
Im creating multiple threads using Tasks.Run() and access the httpClient paralelly.
最佳答案
在 here他们说当一个人执行两个时会发生异常 async
同时对同一个对象进行操作。这对我来说听起来很奇怪,但如果是这种情况,锁定应该对你有所帮助。
关于c# - Windows.Web.Http.HttpClient 抛出 COMException c#,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48402512/