5

I use Windows.Web.Http.HttpClient in my UWP Windows 10 app. I have created a single instance of the HttpClient in my app and re-using the connection for all the HTTP calls. Everything works fine, except for the parallel HTTP calls.

Whenever I iniate a parallel HTTP request, At first time the paralell threads throws the below COMException. I catch this exception and resend the http calls and it succeeds. After a while, again if i start parallel thread, its failing again.

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.

Tried surfing and couldn't get the solution. Does anyone faced this issue and got solution?

Here is the code sample:

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.

1 Answers1

1

In here they say that the exception occurs when one performs two async operations on the same object simultaneously. This sounds quite bizarre to me, but if this is the case, locking should help you.

Mike
  • 843
  • 7
  • 13