【问题标题】:Random Exeception error. System.ArgumentException: An item with the same key has already been added随机异常错误。 System.ArgumentException:已添加具有相同键的项目
【发布时间】:2016-01-26 20:28:52
【问题描述】:

我正在开发一个 C# 网站,上周末我刚刚开始遇到一些随机错误。该错误是从网站上的随机页面生成的,当然,几乎没有任何信息可以继续。这是错误:

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.Collections.Generic.Dictionary`2.Add(TKey key, TValue value)
  at System.Collections.ObjectModel.KeyedCollection`2.AddKey(TKey key, TItem item)
  at System.Collections.ObjectModel.KeyedCollection`2.InsertItem(Int32 index, TItem item)
  at System.Collections.ObjectModel.Collection`1.Add(T item)
  at CommerceBuilder.Catalog.DefaultUrlRewriter.InitializeCache()
  at CommerceBuilder.Catalog.DefaultUrlRewriter.a(String A_0)
  at CommerceBuilder.Catalog.DefaultUrlRewriter.RewriteUrl(String sourceUrl)
  at CommerceBuilder.Services.b.c(Object A_0, EventArgs A_1)
  at System.Web.HttpApplication.SyncEventExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute()
  at System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously)

我已经阅读了很多关于类似错误的文章,但我没有添加任何我知道的新字典项目,所以我不知道从哪里开始。谁能给我一些建议?

【问题讨论】:

  • 什么是CommerceBuilder?导致此错误的操作是什么?
  • 某事正试图向Dictionary 添加一个密钥,而该密钥已经存在。我先看看CommerceBuilder.Catalog.DefaultUrlRewriter.InitializeCache()
  • CommerceBuilder 是 AbleCommerce 电子商务平台
  • 尝试使用ConcurrentDictionaryTryAdd
  • 我会检查 CommerceBuilder 类并查看“初始化”方法。如果您看不到它(它被写入第三方程序集),那么我会联系该第三方提供商,或者我会尝试使用工具对其进行反编译。

标签: c# asp.net


【解决方案1】:

当两个线程同时尝试向非线程安全的字典中添加条目时,可能会发生这种情况。

如果您有能力修改引发错误的代码,我建议您使用 System.Collections.Concurrent.ConcurrentDictionary 而不是 System.Collections.Dictionary。

【讨论】:

  • 我没有办法修改这部分代码的工作方式。
  • 您可能需要对您的网站进行游戏,以便在初始化期间只运行一个线程。例如。如果你有一个测试客户端,让它在下班时间抓取网站,只有一个客户端线程——并确保你的应用程序池设置为不会自动重置。或许可以联系您的供应商以获取更多建议。
猜你喜欢
  • 1970-01-01
  • 2017-04-15
  • 1970-01-01
  • 1970-01-01
  • 2011-06-18
  • 1970-01-01
  • 2014-12-18
  • 1970-01-01
相关资源
最近更新 更多