我正在尝试为 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/