Service stack: Difference between GetSession and S

2019-07-21 14:24发布

问题:

Are there any significant differences between Service.GetSession() and Service.SessionAs<T>() and how they resolve the sessions?

I'm maintaining this code that makes use of one in some requests and uses the later in others. Are there interchangeable or are their some other considerations?

回答1:

Exactly this difference

public virtual IAuthSession GetSession(bool reload = false)
{
    var req = this.Request;
    if (req.GetSessionId() == null)
        req.Response.CreateSessionIds(req);
    return req.GetSession(reload);
}

protected virtual TUserSession SessionAs<TUserSession>()
{
    var ret = TryResolve<TUserSession>();
    return !Equals(ret, default(TUserSession))
        ? ret 
        : Cache.SessionAs<TUserSession>(Request, Response);
}

I have never used any of them, but it seems that they should not be randomly interchanged. You can browse this source code here and figure out how significantly different they are. It's hard to tell at the first glance due to the lack of documentation