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;
});
}