using the same cache for web site and web services over IIS
public static class SessionManager
{
private static ISessionFactory _sessionFactory = null;
private static ISessionFactory SessionFactory
{
get
{
if (_sessionFactory == null)
{
if (System.Web.HttpContext.Current != null)
{
log4net.Config.XmlConfigurator.Configure();
log4net.ILog log = log4net.LogManager.GetLogger("General");
log.InfoFormat("creating a new session factory for process: {0} (id = {1}), \n stack trace is: {2}",
System.Diagnostics.Process.GetCurrentProcess().ProcessName, System.Diagnostics.Process.GetCurrentProcess().Id,
new System.Diagnostics.StackTrace().ToString());
_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 void UnbindSession()
{
ISession session = NHibernate.Context.CurrentSessionContext.Unbind(SessionManager.SessionFactory);
if (session != null && session.IsOpen)
{
session.Close();
}
}
public static ISession GetCurrentSession()
{
return SessionFactory.GetCurrentSession();
}
}
and the resulting logs:
for the web site: 2011-01-05 10:54:54 INFO General - creating a new session factory for process: aspnet_wp (id = 7892), stack trace is: at Managers.SessionManager.get_SessionFactory() at Managers.SessionManager.BindSessionToRequest() at Managers.SessionPerRequestHttpModule.Context_BeginRequest(Object sender, EventArgs e) at System.Web.HttpApplication.SyncEventExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute() at System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously) at System.Web.HttpApplication.ApplicationStepManager.ResumeSteps(Exception error) at System.Web.HttpApplication.System.Web.IHttpAsyncHandler.BeginProcessRequest(HttpContext context, AsyncCallback cb, Object extraData) at System.Web.HttpRuntime.ProcessRequestInternal(HttpWorkerRequest wr) at System.Web.HttpRuntime.ProcessRequestNoDemand(HttpWorkerRequest wr) at System.Web.Hosting.ISAPIRuntime.ProcessRequest(IntPtr ecb, Int32 iWRType) and for the web service: 10:55:20 INFO General - creating a new session factory for process: aspnet_wp (id = 7892), stack trace is: at Managers.SessionManager.get_SessionFactory() at Managers.SessionManager.BindSessionToRequest() at Managers.SessionPerRequestHttpModule.Context_BeginRequest(Object sender, EventArgs e) at System.Web.HttpApplication.SyncEventExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute() at System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously) at System.Web.HttpApplication.ApplicationStepManager.ResumeSteps(Exception error) at System.Web.HttpApplication.System.Web.IHttpAsyncHandler.BeginProcessRequest(HttpContext context, AsyncCallback cb, Object extraData) at System.Web.HttpRuntime.ProcessRequestInternal(HttpWorkerRequest wr) at System.Web.HttpRuntime.ProcessRequestNoDemand(HttpWorkerRequest wr) at System.Web.Hosting.ISAPIRuntime.ProcessRequest(IntPtr ecb, Int32 iWRType) I'm using IIS5 (yuck, I know), and i've tried about every configuration option I could think of. I've created the site and service as virtual directories under the same site, i've set their protection level to 'Low (IIS process)' etc., but to no avail. anyone got an idea? thanks in advance, JhonnyIf the web app and the service are running in the same process/assembly, I'd imagine they might both have access to the application's application cache. When the SessionFactory gets initialized, if you happen to be storing the SessionFactory in the runtime cache by name (perhaps a dictionary/hash), you should be able to retrieve it by name from either the web app or the web service. The SharpArchitecture guys have a well built solution to caching the sessionfactory, although their approach was in order to solve multiple database scenarios. The source has inclusions for services as well and I think you'd be able to modify what they've built to suit your needs. If the web app and service have to live separately (different assemblies) you might not be able to accomplish this.
精彩评论