1

Execute REST call and process response asynchronously.

System.AggregateException: One or more errors occurred. ---> System.AggregateException: One or more errors occurred. ---> System.ArgumentException: An item with the same key has already been added.
   at System.ThrowHelper.ThrowArgumentException(ExceptionResource resource)
   at System.Collections.Generic.Dictionary`2.Insert(TKey key, TValue value, Boolean add)
   at System.Net.Http.Headers.HttpHeaders.AddHeaderToStore(String name, HeaderStoreItemInfo info)
   at System.Net.Http.Headers.HttpHeaders.CreateAndAddHeaderToStore(String name)
   at System.Net.Http.Headers.HttpHeaders.GetOrCreateHeaderInfo(String name, Boolean parseRawValues)
   at System.Net.Http.Headers.HttpHeaders.SetParsedValue(String name, Object value)
   at System.Net.Http.Headers.HttpContentHeaders.get_ContentLength()
   at System.Net.Http.HttpClientHandler.PrepareAndStartContentUpload(RequestState state)
   --- End of inner exception stack trace ---
   at System.Threading.Tasks.Task.ThrowIfExceptional(Boolean includeTaskCanceledExceptions)
   at System.Threading.Tasks.Task`1.GetResultCore(Boolean waitCompletionNotification)
   at System.Threading.Tasks.Task`1.get_Result()
   System.Threading.Tasks.ContinuationResultTaskFromResultTask`2.InnerInvoke()
   at System.Threading.Tasks.Task.Execute()
   --- End of inner exception stack trace ---
   at System.Threading.Tasks.Task.ThrowIfExceptional(Boolean includeTaskCanceledExceptions)
   at System.Threading.Tasks.Task`1.GetResultCore(Boolean waitCompletionNotification)
   at System.Threading.Tasks.Task`1.get_Result()

Code:

var resultTask = base.WrapCallWithLogging(requestUri, task, method, sw, content);

resultTask.ContinueWith(t =>
{
    // Do something 
    GetMessage(); //access the variable content
};

return resultTask;

MY understanding is that we are getting this exception because for some reason header is being created twice. Could this be problem of the asynchronous tasks. can anyone point to the right documentation?

base.WrapCallWithLogging() method:

protected virtual Task<HttpResponseMessage> WrapCallWithLogging(RestUri requestUri, Task<HttpResponseMessage> task, HttpMethod method, Stopwatch sw, HttpContent content = null)
{
    var uriTemplate = requestUri.BuildFullTemplateString();
    Logger.DebugFormat("Making http '{0}' call to '{1}'", method, uriTemplate);
    return task.ContinueWith(r =>
    {
        var result = r.Result;
        sw.Stop();
        Logger.DebugFormat("Ending http '{0}' call to '{1}' with status code {2} in {3} ms",
           method, 
           uriTemplate.ToString(),
           result == null ? HttpStatusCode.InternalServerError : result.StatusCode,
           sw.ElapsedMilliseconds);

        return result;
    });
}
PKO
  • 21
  • 3

1 Answers1

0

The exception stacktrace leafs you to this code line:

var result = r.Result;

r here is the task from the first fragment:

var resultTask = base.WrapCallWithLogging(requestUri, task, method, sw, content);

So, as far as you didn't provide it's initialization, it's hard to say what's wrong with it, but you should investigate it's code. You say that the error message is

Asynchronous Task results in exception: System.ArgumentException: An item with the same key has already been added

Similar error with a solution could be found here:

An item with the same key has already been added

Most likely, you have model which contains the same property twice. Perhaps you are using new to hide the base property.
Solution is to override the property or use another name.
If you share your model, we would be able to elaborate more.

Community
  • 1
  • 1
VMAtm
  • 27,943
  • 17
  • 79
  • 125