MVC4 and ServiceStack session in Redis

nover

I have a brand new MVC4 project on which I have installed the ServiceStack MVC starter pack (version 4.0.12 from MyGET) to bootstrap the usage of the service stack sessions.

In my AppHost my custom session is configured as:

Plugins.Add(new AuthFeature(() => new CustomUserSession(),
    new IAuthProvider[] {
        new CredentialsAuthProvider(config)
    }));

The custom session looks like this for testing purposes:

public class CustomUserSession : AuthUserSession
{
    public string Hello { get; set; }
}   

And the ICacheClient is registered as a redis client:

// register the message queue stuff 
var redisClients = config.Get("redis-servers", "redis.local:6379").Split(',');
var redisFactory = new PooledRedisClientManager(redisClients);
var mqHost = new RedisMqServer(redisFactory, retryCount: 2);
container.Register<IRedisClientsManager>(redisFactory); // req. to l
container.Register<IMessageFactory>(mqHost.MessageFactory);

container.Register<ICacheClient>(c =>
                        (ICacheClient)c.Resolve<IRedisClientsManager>()
                        .GetCacheClient())
                        .ReusedWithin(Funq.ReuseScope.None);      

I have then created a ControllerBase which for simplicity loads and saves the custom session for each request:

public abstract class ControllerBase :  ServiceStackController<CustomUserSession>
{
    protected override IAsyncResult BeginExecute (System.Web.Routing.RequestContext requestContext, AsyncCallback callback, object state)
    {
        ViewBag.Session = this.UserSession;
        return base.BeginExecute (requestContext, callback, state);
    }

    protected override void EndExecute(IAsyncResult asyncResult)
    {
        SaveSession(null);

        base.EndExecute(asyncResult);
    }
    public void SaveSession(TimeSpan? expiresIn = null)
    {
        Cache.CacheSet(SessionFeature.GetSessionId(), UserSession, expiresIn ?? new TimeSpan(0, 30, 0));
    }
}

I then modify the Hello property to read "World" in one of my actions, and I can clearly see with a breakpoint on the SaveSession method that the value has been properly. However, upon loading the page again and inspecting the loaded session, there's nothing set. Also, looking in the Redis database, the following blob is saved:

{
   "createdAt": "/Date(-62135596800000-0000)/",
   "lastModified": "/Date(-62135596800000-0000)/",
   "providerOAuthAccess": [],
   "isAuthenticated": true,
   "tag": 0
}

It's not saving my custom property. Can anyone tell me what I'm doing wrong / missing?

=== UPDATE ===

If I change any of the properties from the base AuthUserSession the changes to those properties are persisted - so it would seem that SS somehow decides to disregard the properties from my concrete type.

mythz

Because AuthUserSession is a DataContract and attributes are now inherited in v4, you also need to mark each member with [DataMember], e.g:

public class CustomUserSession : AuthUserSession
{
    [DataMember]
    public string Hello { get; set; }
}   

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

MVC4 and ServiceStack session in Redis

From Dev

Reconnecting to Servicestack session in an asp.net MVC4 application

From Dev

Session without authentication with MemoryCacheClient in servicestack with MVC4

From Dev

MVC4 + ServiceStack +....Glimpse?

From Dev

Storing an ASP.NET Session in Redis and reading it in ServiceStack

From Dev

Aspnet5 - ServiceStack.Redis - custom session provider

From Dev

ASP MVC4 + NHibernate objects in Session

From Dev

MVC4 Session not persisted between requests

From Dev

Error calling ServiceStack service with mvc4 Controller

From Dev

How do I get ServiceStack to work in an MVC4 project?

From Dev

How do I get ServiceStack to work in an MVC4 project?

From Dev

Sliding Session Expiration with ServiceStack Authentication on ASP.NET MVC

From Dev

Sliding Session Expiration with ServiceStack Authentication on ASP.NET MVC

From Dev

secure session cookie in servicestack

From Dev

ServiceStack update session on heartbeat

From Dev

Session based validation in ServiceStack

From Dev

secure session cookie in servicestack

From Dev

ServiceStack 'session' missing?

From Dev

ServiceStack Redis Messaging - IMessage?

From Dev

Alternative to servicestack.redis

From Dev

Using ServiceStack Redis with Twemproxy

From Dev

ServiceStack Redis Serialization Error

From Dev

Alternative to servicestack.redis

From Dev

ServiceStack.Redis SearchKeys

From Dev

ServiceStack - Redis Sessions Accumulating

From Dev

Redirect to specific page after session expires (MVC4)

From Dev

Is it possible to get session values from action to another in MVC4?

From Dev

Best way to share session between MVC4 and a class library

From Dev

Not redirecting to login page after session expires in mvc4

Related Related

  1. 1

    MVC4 and ServiceStack session in Redis

  2. 2

    Reconnecting to Servicestack session in an asp.net MVC4 application

  3. 3

    Session without authentication with MemoryCacheClient in servicestack with MVC4

  4. 4

    MVC4 + ServiceStack +....Glimpse?

  5. 5

    Storing an ASP.NET Session in Redis and reading it in ServiceStack

  6. 6

    Aspnet5 - ServiceStack.Redis - custom session provider

  7. 7

    ASP MVC4 + NHibernate objects in Session

  8. 8

    MVC4 Session not persisted between requests

  9. 9

    Error calling ServiceStack service with mvc4 Controller

  10. 10

    How do I get ServiceStack to work in an MVC4 project?

  11. 11

    How do I get ServiceStack to work in an MVC4 project?

  12. 12

    Sliding Session Expiration with ServiceStack Authentication on ASP.NET MVC

  13. 13

    Sliding Session Expiration with ServiceStack Authentication on ASP.NET MVC

  14. 14

    secure session cookie in servicestack

  15. 15

    ServiceStack update session on heartbeat

  16. 16

    Session based validation in ServiceStack

  17. 17

    secure session cookie in servicestack

  18. 18

    ServiceStack 'session' missing?

  19. 19

    ServiceStack Redis Messaging - IMessage?

  20. 20

    Alternative to servicestack.redis

  21. 21

    Using ServiceStack Redis with Twemproxy

  22. 22

    ServiceStack Redis Serialization Error

  23. 23

    Alternative to servicestack.redis

  24. 24

    ServiceStack.Redis SearchKeys

  25. 25

    ServiceStack - Redis Sessions Accumulating

  26. 26

    Redirect to specific page after session expires (MVC4)

  27. 27

    Is it possible to get session values from action to another in MVC4?

  28. 28

    Best way to share session between MVC4 and a class library

  29. 29

    Not redirecting to login page after session expires in mvc4

HotTag

Archive