1

I have two applications deployed on different servers.

1) ASP.net MVC 4 web application with sessionState mode="SQLServer" for session management

2) Servicestack service application with sessionState mode="SQLServer" for session management ( yes i need session in service application )

both application share same database and tables for session management

I am using following solution to have asp.net session in service stack application .

Reconnecting to Servicestack session in an asp.net MVC4 application

I am not using the servicestack session so following way is not available to me.

How can I use a standard ASP.NET session object within ServiceStack service implementation

User logs in to the mvc web application, for user data mvc application in turn calls the service methods from service stack application.

using (var client = new JsonServiceClient(servicestackUrl))
                {

                    var response = client.Get<HttpWebResponse>(new GetFile
                    {
                        UserName = UserName,
                        EmployerID = EmployerID,
                        filename = fileName

                    });

                    //work on responce
                }

now i require to have same session variables initialized in mvc application in Servicestack application deployed on another server

session cookie is set in mvc web application not on servicestack application ,but session details are stored in sql database accessible to both application.

I can get the current session id and send it to service as parameter , but how to get the session details from database using session id ?.

Update :

I have found a way to send session id to app server

 var cookie = new Cookie("ASP.NET_SessionId", Session.SessionID);

 cookie.Domain = ".app.localhost";

 client.CookieContainer.Add(cookie);

How to get session in serviceStack application using above from sql server ?

Community
  • 1
  • 1
A_m0
  • 974
  • 2
  • 17
  • 45

1 Answers1

2

ServiceStack doesn't use ASP.NET's Session Provider for storing Sessions, it uses its own Session API which stores Sessions into the registered Caching provider.

E.g. to have Sessions stored in SQL Server you would use OrmLiteCacheClient which can be registered with:

//Register OrmLite Db Factory if not already
container.Register<IDbConnectionFactory>(c => 
    new OrmLiteConnectionFactory(connString, SqlServerDialect.Provider)); 

container.RegisterAs<OrmLiteCacheClient, ICacheClient>();

//Create 'CacheEntry' RDBMS table if it doesn't exist already
container.Resolve<ICacheClient>().InitSchema(); 

To access the Session you'd need to access the Session Id (i.e. ss-id or ss-pid Cookies) which you can get from an IRequest with:

var sessionId = req.GetSessionId();

Then you can use SessionFeature.GetSessionKey() to access the cache key that's used to store the Session, i.e:

var sessionKey = SessionFeature.GetSessionKey(sessionId);

Which you can then access from the ICacheClient with:

var userSession = cache.Get<IAuthSession>(sessionKey);

Note the easiest way to access the Session for the current user is with just:

var userSession = req.GetSession();

Accessing ASP.NET Sessions within ServiceStack

Since ServiceStack is just a normal ASP.NET Web Application you can continue to access the ASP.NET Request Context via the HttpContext.Current singleton, e.g. you can access an ASP.NET Session with:

var value = HttpContext.Current.Session[key];

Also as a ServiceStack Request is just an ASP.NET IHttpHandler underneath, you can also access the underlyng ASP.NET HttpRequest with:

var aspReq = req.OriginalRequest as HttpRequestBase;
var value = aspReq.RequestContext.HttpContext.Session[key];

Note: ServiceStack has its own clean Session and Caching Providers which is completely independent from ASP.NET Session or Caching Provider model which has degraded performance by design.

mythz
  • 141,670
  • 29
  • 246
  • 390
  • Thanks for answer. problem is we have old project that uses asp.net session , now we have moved to Servicestack and still want to use asp.net session ,which we able to do as mentioned in above question. with this set up how to share session varibles in diffrent application deployed on diffrent server? – A_m0 Aug 11 '15 at 05:18
  • @amol ASP.NET Sessions is a completely different library / provider model that doesn't have anything to do with [ServiceStack Sessions](https://github.com/ServiceStack/ServiceStack/wiki/Sessions). You can still access ASP.NET Sessions via the ASP.NET `HttpContext.Current.Session[]` singleton, just understand this is an ASP.NET System.Web API and doesn't have anything to do with ServiceStack Sessions nor does any of ServiceStack libraries make use of ASP.NET Sessions which have [degraded performance by design](http://stackoverflow.com/q/3629709/85785). – mythz Aug 11 '15 at 05:33
  • @amol I've updated my answer with different ways to access ASP.NET underlying Request and HttpContext. – mythz Aug 11 '15 at 05:44
  • thanks , but i am getting value of session key as null as. servicestack app is deployed on another server. mvc web app and servicestack service application share the sql session state. session cookie is set in MVC app and mvc app will request servicestack app. any way to share the session id and so that servicestack will automatically load the session in asp.net session ( which normally happens in session state as sqlserver mode ) – A_m0 Aug 11 '15 at 06:14
  • @amol Can't stress enough that ServiceStack doesn't have anything to do with ASP.NET Sessions, doesn't enable it, doesn't touch it, provides no APIs for it, has nothing to with it, etc. If you want to enable it you'll need to do it outside of ServiceStack, e.g you can try enabling it with a [custom IHttpHandler](http://stackoverflow.com/a/8659726/85785). If that doesn't work you may need to call [ServiceStack Services via MVC](https://github.com/ServiceStack/ServiceStack/wiki/ServiceStack-Integration) – mythz Aug 11 '15 at 06:53
  • i understood it. as mentioned earlier i have already using asp.net session in servicestack application using custom Ihttphandler ( mentioned in question) .either way thanks. – A_m0 Aug 11 '15 at 06:59