Pergunta

I'm in the process of migrating my app over to ServiceStack, and have registered up the existing MVC3 Controllers using ServiceStackController, as outlined by How can I use a standard ASP.NET session object within ServiceStack service implementation

I don't see where to find the PageBase class, referenced in the article, outside from the test project. Is this something i need to copy/paste over into my solution?

I also have a few custom IHttpHandler (.ashx) pages, that I'm trying to get on the same ISession, but I don't see a base class for that called out in the referenced post. Is this possible, or do I need to move these IHttpHandler's to ServiceStack services to be able to take advantage of ISession?

Foi útil?

Solução

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)));
    }
Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top