5

Web アプリケーションの場合、セッションを処理する良い方法は、Begin/EndRequest<property name="current_session_context_class">managed_web</property>の呼び出し設定を使用することです。CurrentSessionContext.Bind/Unbindその後sessionFactory.GetCurrentSession()、リポジトリ クラスで使用できます。

これは、すべてのページ要求に対して正常に機能します。しかし、私はバックグラウンド ワーカーが作業を行っており、同じリポジトリ クラスを使用して作業を行っています。これらは Web リクエスト内では実行されないため、セッション処理は機能しません。

これを解決する方法について何か提案はありますか?

4

3 に答える 3

15

独自のセッション コンテキスト クラスを作成して解決しました。

public class HybridWebSessionContext : CurrentSessionContext
{
    private const string _itemsKey = "HybridWebSessionContext";
    [ThreadStatic] private static ISession _threadSession;

    // This constructor should be kept, otherwise NHibernate will fail to create an instance of this class.
    public HybridWebSessionContext(ISessionFactoryImplementor factory)
    {
    }

    protected override ISession Session
    {
        get
        {
            var currentContext = ReflectiveHttpContext.HttpContextCurrentGetter();
            if (currentContext != null)
            {
                var items = ReflectiveHttpContext.HttpContextItemsGetter(currentContext);
                var session = items[_itemsKey] as ISession;
                if (session != null)
                {
                    return session;
                }
            }

            return _threadSession;
        }
        set
        {
            var currentContext = ReflectiveHttpContext.HttpContextCurrentGetter();
            if (currentContext != null)
            {
                var items = ReflectiveHttpContext.HttpContextItemsGetter(currentContext);
                items[_itemsKey] = value;
                return;
            }

            _threadSession = value;
        }
    }
}
于 2011-05-03T14:32:47.437 に答える
2

このシナリオでは、DI ライブラリと「ハイブリッド」スコープ (StructureMap では、これは InstanceScope.Hybrid として定義されています) を使用して自分でセッション作成を処理するのが最も簡単であることがわかりました。これにより、ASP.net アプリ ドメインでは HttpContext によって、通常のアプリ ドメインでは ThreadStatic によってインスタンスのスコープが設定され、両方で同じアプローチを使用できるようになります。

他の DI ライブラリも同様の機能を提供していると思います。

于 2011-05-03T11:42:37.993 に答える
0

私のプロジェクトでは、CurrentSessionContextの周りに小さなラッパークラスを作成しました。
おそらく、ニーズに合わせて拡張することができます。との実装を微調整する必要があると
思います: BindSessionToRequestGetCurrentSession

public static class SessionManager
    {
        private static ISessionFactory _sessionFactory = null;
        private static ISessionFactory SessionFactory
        {
            get
            {
                if (_sessionFactory == null)
                {
                    //check whether we're in web context or win context, and create the session factory accordingly.
                    if (System.Web.HttpContext.Current != null)
                    {
                        if (_sessionFactory == null)
                        {
                            _sessionFactory = DAOBase.GetSessionFactory();
                        }
                    }
                    else
                    {
                        _sessionFactory = DAOBase.GetSessionFactoryForWin();
                    }
                }
                return _sessionFactory;
            }
        }

        public static void BindSessionToRequest()
        {
            ISession session = SessionManager.SessionFactory.OpenSession();
            NHibernate.Context.CurrentSessionContext.Bind(session);
        }

        public static bool CurrentSessionExists()
        {
            return NHibernate.Context.CurrentSessionContext.HasBind(SessionFactory);
        }

        public static void UnbindSession()
        {
            ISession session = NHibernate.Context.CurrentSessionContext.Unbind(SessionManager.SessionFactory);
            if (session != null && session.IsOpen)
            {
                session.Close();
            }
        }

        public static ISession GetCurrentSession()
        {
            return SessionFactory.GetCurrentSession();
        }
    }
于 2011-05-02T14:19:56.747 に答える