7

I'm using Stackexchange.Redis for Redis cache on Azure with Glass mapper and Sitecore 8.1 - For Glass mapper implementation I'm using interfaces as models and maps configuration to get the data.

Now the issue is that for Redis provider to work we should pass the object as serializable which can only happen in classes but I'm using interfaces as model with glass mapper. If I use classes as model and serialize it my redis implementation works just fine but then I'll be limited to have multiple inheritence in my template.

Can anyone please suggest how to use Glass Mapper interfaces to be pushed into Redis cache or any work around for this?

Below is the error I'm getting while serializing the Interface: Type

'System.Linq.Enumerable+WhereSelectEnumerableIterator`2[[Sitecore.Data.Templates.Template, Sitecore.Kernel, Version=8.1.0.0, Culture=neutral, PublicKeyToken=null],[System.Guid, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089]]' in Assembly 'System.Core, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089' is not marked as serializable.

at System.Runtime.Serialization.FormatterServices.InternalGetSerializableMembers(RuntimeType type) at System.Runtime.Serialization.FormatterServices.GetSerializableMembers(Type type, StreamingContext context) at System.Runtime.Serialization.Formatters.Binary.WriteObjectInfo.InitMemberInfo() at System.Runtime.Serialization.Formatters.Binary.WriteObjectInfo.InitSerialize(Object obj, ISurrogateSelector surrogateSelector, StreamingContext context, SerObjectInfoInit serObjectInfoInit, IFormatterConverter converter, ObjectWriter objectWriter, SerializationBinder binder) at System.Runtime.Serialization.Formatters.Binary.ObjectWriter.Write(WriteObjectInfo objectInfo, NameInfo memberNameInfo, NameInfo typeNameInfo) at System.Runtime.Serialization.Formatters.Binary.ObjectWriter.WriteArrayMember(WriteObjectInfo objectInfo, NameInfo arrayElemTypeNameInfo, Object data) at System.Runtime.Serialization.Formatters.Binary.ObjectWriter.WriteArray(WriteObjectInfo objectInfo, NameInfo memberNameInfo, WriteObjectInfo memberObjectInfo) at System.Runtime.Serialization.Formatters.Binary.ObjectWriter.Write(WriteObjectInfo objectInfo, NameInfo memberNameInfo, NameInfo typeNameInfo) at System.Runtime.Serialization.Formatters.Binary.ObjectWriter.Serialize(Object graph, Header[] inHeaders, __BinaryWriter serWriter, Boolean fCheck)
at System.Runtime.Serialization.Formatters.Binary.BinaryFormatter.Serialize(Stream serializationStream, Object graph, Header[] headers, Boolean fCheck)
at System.Runtime.Serialization.Formatters.Binary.BinaryFormatter.Serialize(Stream serializationStream, Object graph) at

 public class SampleItemMap : SitecoreGlassMap<ISampleItem>
{

    public override void Configure()
    {
        Map(x =>
        {
            ImportMap<IGlassBase>();
            x.TemplateId("{21AB47FD-F264-48B4-ABF4-1FF5B10FB4A4}");
            x.Cachable();
            x.AutoMap();
        });
    }
}

And the Interface for Handling the Default SampleItem template is as below:

 public interface ISampleItem : IGlassBase
 {
    string Title { get; set; }

    string Text { get; set; }
 }
0

2 Answers 2

2

UPDATE:

Based on the updated stack trace, this actually looks like an issue with serializing an IEnumerable property exposed on one of your glass models (whether directly on your primary model, or indirectly as a child item's properties).

One thing you can try to do is to replace all of your IEnumerable<T> definitions with a concrete collection type, like List<T>.


Caveat Emptor: I have not tried this myself, but...

Caching interface-based proxies should in theory work (see this reference)

The interface-based models are backed by a dynamic proxy class, and Castle (the parent project of DynamicProxy) has ensured that proxies are serializable.

However, if this is not working for you currently, it's entirely possible that this is a bug in Glass Mapper. I say this because even though the primary interceptor used in Glass Mapper (source here) is marked as [Serializable], the ProxyHook is not.

If this seems like the problem to you, I would recommend opening an issue in GitHub to let Mike know.

8
  • Thanks for your input. I'm using fluent configuration maps for my glass mapper interface models to be mapped to the templates and then in my controller fetching the data for the first time and add it to Redis cache. Do I need to add the map class in cache instead of interface? Any idea? Commented Nov 21, 2016 at 17:01
  • I don't think you need to change what you're caching, since you really just want to cache the glass model itself. If you're able, it might help to see the actual exception message & stack trace that you're getting - you can go ahead and add that to your question. Commented Nov 21, 2016 at 17:27
  • I've added the stack trace to the question. Please let me know if this is a bug in Glass Mapper or am I doing something wrong. Just to clarify I've tried using JSON Serializer and BinarySerializer both above error is when I use Binary Serializer. Almost same error I get for JSON Serializer as well. Commented Nov 30, 2016 at 16:28
  • Would you also be able to add the definition of your glass interface? I'll update the answer with some initial thoughts Commented Nov 30, 2016 at 17:19
  • Added the interface definition and also the fluent map class. I'm using SimpleInjector for creating instance of all my maps. Commented Dec 1, 2016 at 5:56
0

Didn't see this two years ago, however a couple of notes when serialising:

  1. Turn off lazy loading, this should disable the proxy objects that Glass uses
  2. Don't use interface templates, you will need to use classes for serialisation, again this is to avoid proxy object.

The Glass project contains some unit tests for serialisation which you might want to review as a reference: Redis Cache with Glass mapper interfaces in Sitecore 8.1 and above

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.