异步任务导致异常:System.ArgumentException:已添加具有相同键的项

时间:2017-01-24 19:37:19

标签: c# task-parallel-library

异步执行REST调用和进程响应。

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()

代码:

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

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

return resultTask;

我的理解是我们得到了这个例外,因为由于某种原因,标题被创建了两次。这可能是异步任务的问题。谁能指出正确的文件?

base.WrapCallWithLogging()方法:

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

1 个答案:

答案 0 :(得分:0)

异常堆栈跟踪将您带到此代码行:

var result = r.Result;

r这是第一个片段中的task

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

所以,就你没有提供它的初始化而言,很难说它有什么问题,但你应该调查它的代码。你说错误信息是

  

异步任务导致异常:System.ArgumentException:已添加具有相同键的项

可以在此处找到与解决方案类似的错误:

An item with the same key has already been added

  

最有可能的是,您的模型包含两次相同的属性。也许您正在使用new来隐藏基本属性   解决方案是覆盖属性或使用其他名称   如果您分享您的模型,我们将能够详细说明。