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.