using the same cache for web site and web services over IIS


my application is composed of several modules: a web site, and a few services that receive requests from client apps.
since all these modules manipulate pretty much the same data, I would like to have the same cache for all of them.
I'm using a static class to reference nHibernate's 2nd level cache (the SessionFactory object), and I've set up the web site and the web services within the same website on IIS.
however, the static SessionFactory variables are different between the web site and web service.
Here is the code for the static SessionManager class:

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,
Jhonny


If 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.

链接地址: http://www.djcxy.com/p/74526.html

上一篇: asp.net构建运行时的菜单与异地链接

下一篇: 在IIS上使用相同的缓存来存放网站和Web服务