1

我将 LinqToSql 用于 mvc Web 应用程序。如果很多人大致同时访问网络应用程序,我会看到一个An item with the same key has already been added.错误。此错误的堆栈如下所示:

[ArgumentException: An item with the same key has already been added.]
   System.Collections.Generic.Dictionary`2.Insert(TKey key, TValue value, Boolean add) +12673712
   System.Data.Linq.DataContext.GetTable(MetaTable metaTable) +286
   System.Data.Linq.DataContext.GetTable() +100
   CableSense.Domain.Repository.Concrete.RoleRepository.GetRolesForUser(String userName) in c:\BuildAgent\work\271278ff356daf1\CableSense.Domain\Repository\Concrete\RoleRepository.cs:84

这只发生在我的 RoleRrovider 类中,它是 .net RoleProvider 的自定义实现。在那里,我的 ctor 从 Ninject 获得了一个存储库,如下所示:

    public CustomRoleProvider()
    {
        _roleRepository = NinjectMVC3.Resolve<IRoleRepository>();
    }

出错的方法:

    public override string[] GetRolesForUser(string username)
    {
        return _roleRepository.GetRolesForUser(username);
    }

在我的仓库中,只是一个返回数据的 linq 查询——仓库在内部实例化了一个上下文,没有什么是静态的或共享的。

任何想法为什么会发生这种情况?

4

2 回答 2

1

我不知道 Ninject 是否可以选择执行此操作,但每次调用 resolve 时它都应该返回所需上下文的新实例。

这是因为 EF 上下文不是线程安全的。

例如,我使用 Castle.Windsor 作为我选择的 IoC,它有一个 LifeStyle 选项,将其设置为 Transient 而不是 Singleton(这是默认设置)可以获得所需的行为。

于 2012-05-11T07:49:26.333 回答
0
private object _lockHandle=new object();

 public override string[] GetRolesForUser(string username)
    {
lock(_lockHandle){
        return _roleRepository.GetRolesForUser(username);
}
    }
于 2012-05-11T07:29:51.870 回答