c# - ServiceStack.Redis 类型的客户端不保存值

标签 c# redis servicestack

我正在尝试为 ServiceStack.Redis 使用类型化的客户端;但是每当我使用类类型时,它都不会保存值,只会返回其每个属性的默认值。但如果我只使用一个简单的数据类型,它就可以工作。

我正在使用 ServiceStack.Redis 版本 3.9.71、MS Visual Studio 2013 Ultimate 和 MSOpenTechRedis for Windows 64 位版本 2.6.12 在本地主机上运行。

这里是重现这个问题的代码:

using ServiceStack.Redis;

class Program
{
    static void Main(string[] args)
    {
        using (IRedisClient client = new RedisClient())
        {
            var typedClient = client.As<TwoInts>();
            TwoInts twoIntsSave = new TwoInts(3, 5);
            typedClient.SetEntry("twoIntsTest", twoIntsSave);
            TwoInts twoIntsLoad = typedClient.GetValue("twoIntsTest");
            //twoIntsLoad is incorrectly (0, 0)
        }

        using (IRedisClient client = new RedisClient())
        {
            var typedClient = client.As<int>();
            int intSave = 4;
            typedClient.SetEntry("intTest", intSave);
            int intLoad = typedClient.GetValue("intTest");
            //intLoad is correctly 4
        }
    }
}


class TwoInts
{
    public int Int1;
    public int Int2;

    public TwoInts(int int1, int int2)
    {
        Int1 = int1;
        Int2 = int2;
    }
}

最佳答案

默认情况下,ServiceStack 的 JSON 序列化程序仅序列化公共(public)属性,而非字段。您可以将字段更改为属性,例如:

class TwoInts
{
    public int Int1 { get; set; }
    public int Int2 { get; set; }
}

或者配置 JSON 序列化程序以序列化公共(public)字段:

JsConfig.IncludePublicFields = true;

关于c# - ServiceStack.Redis 类型的客户端不保存值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23710785/

相关文章:

c# - 如果列存在,则从数据库中选择列

c# - 如何创建 For_Each_With_Condition_With_Index 扩展方法 (EM)

重启机器后Redis服务器被删除

c# - 在 Windows/C# 上加速 Redis

c# - 无法加载文件或程序集 ServiceStack.Text 系统找不到指定的文件

c# - 抛出 System.ExecutionEngineException

c# - FromQueryAttribute 是如何实际工作的,我为什么需要它?

angularjs - 显示来自 Redis 的编码数据

redis - 在 ServiceStack.Redis 的情况下,RedisPoolSize、RedisPoolTimeoutSeconds 的默认值是什么

redis - 服务栈.Redis : Unable to Connect: sPort: 0