0

I am using C# StackExchange.Redis cache, and am attempting to cache an object that has a collection of interface objects. Cache works fine, but deserialization throws the error:

Could not create an instance of type IMyInterface. Type is an interface or abstract class and cannot be instantiated.

If I were handling the deserialization myself, I understand I could create custom deserialization rules via this SO post:

var obj = JsonConvert.DeserializeObject <MyParentClass>(json, new JsonSerializerSettings
{
    TypeNameHandling = TypeNameHandling.Auto,
    Binder = binder // write some custom deserialization rules
});

However, I don't have access to the deserialization process because it handled by Redis / Newtonsoft.Json. Btw, I am using StackExchange.Redis 1.1.0.0 and Newtonsoft.Json 9.0.0.0

I also found this SO post, about specifying a serialVersionUID, however this is explicitly for Java as outlined in this SO post.

So what am I missing or what are my options? Thanks in advance!

Here is sample code to work with:

public interface IMyInterface
{
    string Name { get; set; }
}

public class MyChildClassA : IMyInterface
{
    public string Name { get; set; }
}

public class MyChildClassB : IMyInterface
{
    public string Name { get; set; }
}

public class MyParentClass
{
    public IList<IMyInterface> Children { get; set; }
    // Assume proper init of list in constructor
}

public void DoStuff()
{
    var parentValue = new MyParentClass();
    // Assume parentValue.Children contains objects of both MyChildClassA and MyChildClassB

    // Works!
    MyCacheImplementation.Add<MyParentClass>(someKey, parentValue);
    // Does NOT work!
    MyCacheImplementation.Get<MyParentClass>(someKey);
}
Community
  • 1
  • 1
Soturi
  • 1,486
  • 1
  • 14
  • 30
  • 1
    SE Redis does not handle serialization. You are probably using some other nuget which does that for you. How about you care about serialization yourself? Or, if the library you use cares about serialization, it should allow you to set the Newstonsoft Json options – MichaC Jan 17 '17 at 09:13
  • Was not aware that SE Redis didn't do the serialization itself, I will have to dig to find where it's happening then in my code base. Thanks! – Soturi Jan 17 '17 at 15:23

0 Answers0