Curtis
Curtis

Reputation: 85

ServiceStack.Redis typed client not saving values

I am trying to use the typed client for ServiceStack.Redis; but whenever I use a class type, it does not save the values and just returns the default of each of its properties. But if I just use a simple data type, it works.

I am using ServiceStack.Redis version 3.9.71, MS Visual Studio 2013 Ultimate, and MSOpenTechRedis for Windows 64-bit version 2.6.12 running on localhost.

Here is the code to reproduce this problem:

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

Upvotes: 0

Views: 477

Answers (1)

mythz
mythz

Reputation: 143399

ServiceStack's JSON Serializer by default only serializes public properties not fields. You can either change your fields to properties, e.g:

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

Or configure the JSON Serializer to serialize public fields with:

JsConfig.IncludePublicFields = true;

Upvotes: 3

Related Questions