MVC4和ServiceStack会议的Redis [英] MVC4 and ServiceStack session in Redis

查看:182
本文介绍了MVC4和ServiceStack会议的Redis的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有我已经(从MyGET版本4.0.12)安装ServiceStack MVC入门包一个全新的MVC4项目来引导服务栈会议的使用。

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.

在我的 APPHOST 我的自定义会话配置为:

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; }
}   

以及 ICacheClient 注册为Redis的客户端:

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);      

我已经然后创建了一个 ControllerBase 为了简化负载和为每个请求保存自定义会话:

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));
    }
}

我再修改您好属性在我的行动之一,阅读世界,我可以清楚地在 SaveSession断点见法的价值已经正常。然而,在重新加载页面和检查加载的会议上,没有什么集。另外,看在Redis的数据库,下面的BLOB保存:

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?

===更新===

如果我更改任何属性的从基 AuthUserSession 更改这些属性的被持久的 - 所以它似乎是SS某种程度上决定从我的具体类型忽略的属性。

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.

推荐答案

由于AuthUserSession是一个DataContract和属性现在继承了v4的,你还需要标记与 [数据成员] ,例如:

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; }
}   

这篇关于MVC4和ServiceStack会议的Redis的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆