The ASP.NET PageBase class is a T4 template that gets added when you install the ServiceStack.Host.AspNet NuGet package.
All of ServiceStack's Caching and Session support is completely independent of MVC controllers and ASP.NET base pages just comes from resolving the ICacheClient
and ISessionFactory
from ServiceStack's IOC.
If your MVC Controllers and ASP.NET base pages are auto-wired you can just declare them as public properties and they will get injected by ServiceStack's IOC otherwise you can access ServiceStack's IOC directly with the singleton:
var cache = Endpoint.AppHost.TryResolve<ICacheClient>();
var typedSession = cache.SessionAs<CustomUserSession>( //Uses Ext methods
HttpContext.Current.Request.ToRequest(), //ASP.NET HttpRequest singleton
HttpContext.Current.Request.ToResponse() //ASP.NET HttpResponse singleton
);
Accessing the session is all done the same way, here's the sample code from ServiceStack's Service.cs base class:
private ICacheClient cache;
public virtual ICacheClient Cache
{
get { return cache ?? (cache = TryResolve<ICacheClient>()); }
}
private ISessionFactory sessionFactory;
public virtual ISessionFactory SessionFactory
{
get { return sessionFactory ?? (sessionFactory = TryResolve<ISessionFactory>()) ?? new SessionFactory(Cache); }
}
/// <summary>
/// Dynamic Session Bag
/// </summary>
private ISession session;
public virtual ISession Session
{
get
{
return session ?? (session = SessionFactory.GetOrCreateSession(Request, Response));
}
}
/// <summary>
/// Typed UserSession
/// </summary>
private object userSession;
protected virtual TUserSession SessionAs<TUserSession>()
{
return (TUserSession)(userSession ?? (userSession = Cache.SessionAs<TUserSession>(Request, Response)));
}