NHibernate KeyNotFoundException

时间:2017-06-14 14:47:52

标签: c# asp.net nhibernate fluent-nhibernate

我的ASP.NET应用程序在VS 2012的本地实例中运行良好,但在VS 2015中没有运行。它正在抛出

  

在mscorlib.dll中发生了Systems.Collections.Generic.KeyNotFoundException但未在用户代码中处理

应用程序在服务器上部署时运行正常,但问题仅发生在本地计算机上 - NHibernate会话密钥为空并在以下行中断。

var sessionInitializer = map[sessionFactory]

我的代码粘贴在下面,我遵循类似的实现,如下面的链接(我有所有内容,直到链接的IWindsorInstaller部分) -

http://nhibernate.info/blog/2011/03/02/effective-nhibernate-session-management-for-web-apps.html

public class LazyNHWebSessionContext : ICurrentSessionContext
{
    private readonly ISessionFactoryImplementor factory;
    private const string CurrentSessionContextKey = 
                                    "NHibernateCurrentSession";

    public LazyNHWebSessionContext(ISessionFactoryImplementor factory)
    {
        this.factory = factory;
    }

    /// <summary>
    /// Retrieve the current session for the session factory.
    /// </summary>
    /// <returns></returns>
    public ISession CurrentSession()
    {
        Lazy<ISession> initializer;
        var currentSessionFactoryMap = GetCurrentFactoryMap();
        if (currentSessionFactoryMap == null ||
            !currentSessionFactoryMap.TryGetValue(factory, out initializer))
        {
            return null;
        }

        return initializer.Value;
    }

    /// <summary>
    /// Bind a new sessionInitializer to the context of the sessionFactory.
    /// </summary>
    /// <param name="sessionInitializer"></param>
    /// <param name="sessionFactory"></param>
    public static void Bind(Lazy<ISession> sessionInitializer, ISessionFactory sessionFactory)
    {
        var map = GetCurrentFactoryMap();
        map[sessionFactory] = sessionInitializer;
    }

    /// <summary>
    /// Unbind the current session of the session factory.
    /// </summary>
    /// <param name="sessionFactory"></param>
    /// <returns></returns>
    public static ISession UnBind(ISessionFactory sessionFactory)
    {
        var map = GetCurrentFactoryMap();
        var sessionInitializer = map[sessionFactory];
        map[sessionFactory] = null;
        if (sessionInitializer == null || !sessionInitializer.IsValueCreated) return null;
        return sessionInitializer.Value;
    }

    /// <summary>
    /// Provides the CurrentMap of SessionFactories.
    /// If there is no map create/store and return a new one.
    /// </summary>
    /// <returns></returns>
    private static IDictionary<ISessionFactory, Lazy<ISession>> GetCurrentFactoryMap()
    {
        //var contextItem = HttpContext.Current.Items[CurrentSessionContextKey];
        if (HttpContext.Current == null) return null;


        var currentFactoryMap = (IDictionary<ISessionFactory, Lazy<ISession>>)
                                HttpContext.Current.Items[CurrentSessionContextKey];

        if (currentFactoryMap == null)
        {
            currentFactoryMap = new Dictionary<ISessionFactory, Lazy<ISession>>();
            HttpContext.Current.Items[CurrentSessionContextKey] = currentFactoryMap;
        }

        return currentFactoryMap;

    }
}

0 个答案:

没有答案