ASP.NET PageBase 类是在安装ServiceStack.Host.AspNet NuGet 包时添加的 T4 模板。
ServiceStack 的所有缓存和会话支持完全独立于 MVC 控制器,而 ASP.NET 基页仅来自于 ServiceStack 的 IOC 的解析ICacheClient
和处理。ISessionFactory
如果您的 MVC 控制器和 ASP.NET 基本页面是自动连接的,您可以将它们声明为公共属性,它们将由ServiceStack 的 IOC注入,否则您可以直接使用单例访问 ServiceStack 的 IOC:
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
);
访问会话都以相同的方式完成,这是来自 ServiceStack 的Service.cs基类的示例代码:
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)));
}