C# AppFabric CreateRoutingClient错误
AppFabric存在问题,导致出现以下错误:C# AppFabric CreateRoutingClient错误,c#,asp.net,appfabric,C#,Asp.net,Appfabric,AppFabric存在问题,导致出现以下错误: Exception type: ArgumentException Exception message: An item with the same key has already been added. at System.Collections.Generic.Dictionary`2.Insert(TKey key, TValue value, Boolean add) at Microsoft.ApplicationSer
Exception type: ArgumentException
Exception message: An item with the same key has already been added.
at System.Collections.Generic.Dictionary`2.Insert(TKey key, TValue value, Boolean add)
at Microsoft.ApplicationServer.Caching.DataCacheFactory.CreateRoutingClient(String cacheName, NamedCacheConfiguration config)
at Microsoft.ApplicationServer.Caching.DataCacheFactory.CreateNewCacheClient(DataCacheDeploymentMode mode, String cacheName, NamedCacheConfiguration config, IClientChannel channel)
at Microsoft.ApplicationServer.Caching.DataCacheFactory.GetCache(String cacheName)
at Microsoft.ApplicationServer.Caching.DataCacheFactory.GetDefaultCache()
... our code where we are attempting to retrieve a cache item from the default cache by its key
这个错误在我们的测试环境中很少发生(我们还没有找到一个按需重现问题的场景),但似乎总是在每次部署之后的生产环境中发生。我们的部署是自动化的,我们已经验证了部署到各种环境的步骤是相同的
给定环境的服务器设置如下所示:
- Server1-托管我们的.net网站,是我们的AppFabric 1.1服务器
- 服务器2-托管我们的.net网站
DataCacheFactory
并从缓存中提取数据的代码:
private static readonly Lazy<DataCacheFactory> _DATA_CACHE_FACTORY = new Lazy<DataCacheFactory>(() => new DataCacheFactory());
private static readonly Lazy<DataCache> _CACHE = new Lazy<DataCache>(() => _DATA_CACHE_FACTORY.Value.GetDefaultCache());
public object Get(string key)
{
return _CACHE.Value.Get(key);
}
private static readonly Lazy\u DATA\u CACHE\u FACTORY=new Lazy(()=>new DataCacheFactory());
private static readonly Lazy_CACHE=new Lazy(()=>_DATA_CACHE_FACTORY.Value.GetDefaultCache());
公共对象获取(字符串键)
{
返回_CACHE.Value.Get(key);
}
我100%确定重复密钥错误是由于对DataCacheFactory
的私有\u myCache
属性的错误访问而产生的。该属性是一个哈希表。重复调用Hashtable.Add(“mykey”、“myvalue”)代码>将生成与您看到的相同的期望
我运行了多个测试,背对背调用GetCache(“default”)
和GetDefaultCache()
不会产生错误。AppFabric试图填充这一功能的方式肯定有些奇怪。这是我的代码,它从未产生过这个错误。我想发布一篇文章以供参考,以防您看到一些明显不同于您的代码所做的事情
if (cache == null)
{
if(factory == null)
factory = new DataCacheFactory();
if(string.IsNullOrWhiteSpace(cacheName))
cacheName = ConfigurationManager.AppSettings["APP_FABRIC_CACHE_NAME"];
cache = factory.GetCache(cacheName);
return cache;
}
在上面的示例中,缓存
和工厂
是各自类型的私有静态
版本,位于名为缓存
的静态类中
希望这能有所帮助我们在工厂创建时使用了锁,从来没有出现过这个问题。我们还在集群中使用AppFab 1.1。我建议您这样做:
lock (cacheLock)
{
if(cacheFactory == null)
{
DataCacheFactoryConfiguration config = new DataCacheFactoryConfiguration(...);
cacheFactory = new DataCacheFactory(config);
}
return cacheFactory;
}
您可以通过DataCacheFactory发布用于获取默认缓存的代码吗?还有使用缓存的代码示例?感谢您对此进行研究。对不起,回复太慢了。我在等待机会部署我的最新代码,看看它是否解决了这个问题。我已经更新了问题以包含我的代码。您是否查看了传递附加线程安全类型参数的
Lazy
重载?似乎ExecutionAndPublication
可能是正确的选择这对于错误消息来说是有意义的,尽管AppFabric以非线程安全的方式修改哈希表似乎有些奇怪。我的代码和你的非常相似。区别在于线程安全,我没有提供备用缓存名称的工具。考虑到我仍然存在这个问题,是否还有其他因素可能导致这个错误?我不知道。您可能需要在缓存结构周围添加一些锁(mutexObject){//yourcodehere}
块。在这一点上,这有点像在抓救命稻草,但这是一件值得尝试的事情。