Using SharpArchitecture's NHibernateSession in conjunction with a different thread

若如初见. 提交于 2020-01-13 05:08:52

问题


I'm using SharpArchitecture in an ASP.NET MVC 3 application. Everything works wonderfully.

Using SharpArchitecture's NHibernateInitializer to initialize a new Session per request like so:

    protected void Application_BeginRequest(object sender, EventArgs e)
    {
        NHibernateInitializer.Instance().InitializeNHibernateOnce(InitializeNHibernateSession);
    }

    private void InitializeNHibernateSession(ISessionStorage sessionStorage)
    {
        NHibernateSession.ConfigurationCache = new NHibernateConfigurationFileCache(
            new[] { "App.Core" });
        NHibernateSession.Init(
            sessionStorage,
            new[] { Server.MapPath("~/bin/" + ApplicationSettings.Instance.NHibernateMappingAssembly) },
            new AutoPersistenceModelGenerator().Generate(),
            Server.MapPath("~/NHibernate.config"));

        NHibernateSession.AddConfiguration(ApplicationSettings.NHIBERNATE_OTHER_DB,
                                           new[] { Server.MapPath("~/bin/" + ApplicationSettings.Instance.NHibernateMappingAssembly) },
                                           new AutoPersistenceModelGenerator().Generate(),
                                           Server.MapPath("~/NHibernateForOtherDb.config"), null, null, null);
    }

As you can see, we are also hitting multiple databases. All that's fine.

Here's where I run into an issue.

I need to spin up a separate thread to execute a database polling mechanism. My intention was to do something like this:

    protected void Application_Start()
    {
            ....
            ThreadingManager.Instance.ExecuteAction(() =>
            {
                // initialize another NHibernateSession within SharpArchitecture somehow
                NHibernateInitializer.Instance().InitializeNHibernateOnce(InitializeNHibernateSession);

                var service = container.Resolve<IDatabaseSynchronizationService>();
                service.SynchronizeRepositories();
            });
}

And through my SynchronizationService, some repositories get called. Obviously when they attempt to access their session, an exception is thrown because the Session is null.

Here's my question. How can I leverage SharpArchitecture's NHibernateSession and somehow have it or a copy of it, spun up inside my polling thread? I am hoping this can be done without having to bypass using the built in SessionManagers and SessionFactories used by SharpArchitecture.


回答1:


I assume that sessionStorage is a SharpArch.Web.NHibernate.WebSessionStorage object, right? If so then the issue isn't so much that the NHibernate Session is null, it's probably that on your thread, HttpContext.Current is null, and the WebSessionStorage object relies on HttpContext.Current to do its work (see this code, particularly line 37, where, when called from a thread, context is null, so a null exception occurs).

I think the solution would be to write a different SessionStorage object that checks to see if HttpContext.Current is null, and if it is, use thread local storage to store NHibernate Sessions in instead (this solution gleaned from this other StackOverflow article which talks about the same sort of thing: NullReferenceException when initializing NServiceBus within web application Application_Start method)

EDIT

Something like this perhaps:

public class HybridWebSessionStorage : ISessionStorage
{

    static ThreadLocal<SimpleSessionStorage> threadLocalSessionStorage;

    public HybridWebSessionStorage( HttpApplication app )
    {
        app.EndRequest += Application_EndRequest;
    }

    public ISession GetSessionForKey( string factoryKey )
    {
        SimpleSessionStorage storage = GetSimpleSessionStorage();
        return storage.GetSessionForKey( factoryKey );
    }

    public void SetSessionForKey( string factoryKey, ISession session )
    {
        SimpleSessionStorage storage = GetSimpleSessionStorage();
        storage.SetSessionForKey( factoryKey, session );
    }

    public System.Collections.Generic.IEnumerable<ISession> GetAllSessions()
    {
        SimpleSessionStorage storage = GetSimpleSessionStorage();
        return storage.GetAllSessions();
    }

    private SimpleSessionStorage GetSimpleSessionStorage()
    {
        HttpContext context = HttpContext.Current;
        SimpleSessionStorage storage;
        if ( context != null )
        {
            storage = context.Items[ HttpContextSessionStorageKey ] as SimpleSessionStorage;
            if ( storage == null )
            {
                storage = new SimpleSessionStorage();
                context.Items[ HttpContextSessionStorageKey ] = storage;
            }
        }
        else
        {
            if ( threadLocalSessionStorage == null )
                threadLocalSessionStorage = new ThreadLocal<SimpleSessionStorage>( () => new SimpleSessionStorage() );
            storage = threadLocalSessionStorage.Value;
        }
        return storage;
    }

    private static readonly string HttpContextSessionStorageKey = "HttpContextSessionStorageKey";

    private void Application_EndRequest( object sender, EventArgs e )
    {
        NHibernateSession.CloseAllSessions();

        HttpContext context = HttpContext.Current;
        context.Items.Remove( HttpContextSessionStorageKey );
    }
}

NOTE: You will have to make sure you call NHibernateSession.CloseAllSessions() when you have finished doing your work on the thread though, as Application_EndRequest won't get fired when your thread completes.



来源:https://stackoverflow.com/questions/6527545/using-sharparchitectures-nhibernatesession-in-conjunction-with-a-different-thre

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!