Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/312.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# AppFabric CreateRoutingClient错误_C#_Asp.net_Appfabric - Fatal编程技术网

C# AppFabric CreateRoutingClient错误

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

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.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网站
这些服务器是负载平衡的。已在两个应用程序上启用AppFabric本地缓存。我们的测试和生产服务器设置相同。我们知道最好在专用服务器上安装AppFabric,但不认为这会导致/解决此问题

我们被这个问题难住了,因为我们在网上的其他地方没有发现任何关于它的提及,因为堆栈跟踪似乎表明这是AppFabric本身的问题。异常提到插入一些东西,但发生这种情况时,我们所做的只是尝试从DataCacheFactory获取默认缓存,以便从中检索项。那么,这个错误意味着什么?我们应该如何解决它

更新

下面是我用来创建
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}
块。在这一点上,这有点像在抓救命稻草,但这是一件值得尝试的事情。