c# - Windows.Web.Http.HttpClient 抛出 COMException c#

标签 c# windows parallel-processing uwp comexception

我用 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/

相关文章:

mysql windows服务卡在启动状态

c - 网络的机器代码是什么样的?

java - 如何并行调用独立函数并在使用 CompletableFuture 失败时抛出异常

c++ - OpenMP 的理想数据结构

concurrency - X10 并行处理共享变量

c# - 使用 Raspberry pi 调用 rest API windows 10 IoT

c# - OnClosing 和 Form_Closing 或 OnLoad 和 Form_Load 之间的区别

相当于 "sort -u"的 Windows CLI

c# - 在 C# 下,try、throw 和 catch block 对性能的影响有多大

c# - 打印 Winform/可视元素