Cannot save object due to "Unsupported type" error

1,427 views
Skip to first unread message

tker

unread,
Jul 24, 2015, 1:08:14 AM7/24/15
to RavenDB - 2nd generation document database
Hi,

I am having trouble saving instances of an object using RavenDB.Client version 3.0.3690 when I have not had this problem previously.

The exception message I am getting is:

Unsupported type: IS.Argus.Domain.Shared.CommandModel.ValueObjects.EquipmentAlarmType. Use the JsonSerializer class to get the object's JSON representation. Path 'AlarmTypes'.

'AlarmTypes' is an IEnumerable of a type called EquipmentAlarmType. I followed the advice for this exception given here: https://groups.google.com/forum/#!msg/ravendb/Z-3-_b_7s60/zZm6sgfaJIMJ; however, it did not work for me.

My IDocumentStore/IDocumentSessions are created like this using StructureMap 3:

    public class RavenDbRegistry : Registry
    {
        public RavenDbRegistry()
        {
            var documentStore = new DocumentStore
            {
                ConnectionStringName = "RavenDB",
                Conventions =
                {
                    IdentityPartsSeparator = "-",
                    JsonContractResolver = new PrivatePropertySetterResolver(),
                    CustomizeJsonSerializer =
                        serializer =>
                        {
                            serializer.TypeNameHandling = TypeNameHandling.Auto;
                            serializer.ObjectCreationHandling = ObjectCreationHandling.Replace;
                        },
                    DefaultQueryingConsistency = ConsistencyOptions.AlwaysWaitForNonStaleResultsAsOfLastWrite,
                }
            };
            documentStore.Initialize();

            For<IDocumentStore>().Singleton().Use(documentStore);

            For<IDocumentSession>().Use(ctx => ctx.GetInstance<IDocumentStore>().OpenSession());
        }
    }

I am having trouble saving instances of an object using RavenDB.Client version 3.0.3690 when I have not had this problem previously. The class definition of the type I am trying to save is as follows:

    public class DedPersistentModel : PersistentModel
    {
        public EquipmentName Name { get; private set; }
        public AggregateId CommsEquipmentId { get; private set; }
        public IEnumerable<EquipmentAlarmType> AlarmTypes { get; private set; }
        
        public DedPersistentModel()
        {
            Name = new EquipmentName("Default Ded");
            AggregateId = new AggregateId(new Guid());
            CommsEquipmentId = new AggregateId(new Guid());
            AlarmTypes = new List<EquipmentAlarmType>();

        }

        ... Unrelated methods
    }

The PrivatePropertySetterResolver is defined as such:

public class PrivatePropertySetterResolver : DefaultContractResolver
{
    protected override JsonProperty CreateProperty(MemberInfo member, MemberSerialization memberSerialization)
    {
        var prop = base.CreateProperty(member, memberSerialization);
        if (!prop.Writable)
        {
            var property = member as PropertyInfo;
            if (property != null)
            {
                var hasPrivateSetter = property.GetSetMethod(true) != null;
                prop.Writable = hasPrivateSetter;
            }
        }
 
        return prop;
    }
}

The EquipmentAlarmType looks like this:

    public class EquipmentAlarmType : ValueObject<EquipmentAlarmType>
    {
        public EquipmentAlarmType()
        {
            AlarmText = new EquipmentAlarmText("Alarm raised");
            RestoreText = new EquipmentAlarmText("Alarm restored");
            AlarmCategory = new EquipmentAlarmCategory(3);
            RestoreCategory = new EquipmentAlarmCategory(3);
        }

        public EquipmentAlarmType(string alarmText, string restoreText, int alarmCategory, int restoreCategory, string alarmName)
        {
            try
            {
                AlarmText = new EquipmentAlarmText(alarmText); 
                RestoreText = new EquipmentAlarmText(restoreText);
                AlarmCategory = new EquipmentAlarmCategory(alarmCategory);
                RestoreCategory = new EquipmentAlarmCategory(restoreCategory);
                AlarmName = new EquipmentAlarmName(alarmName);

            }
            catch (ArgumentException)
            {                
                throw;
            }  
            
        }



        public EquipmentAlarmText AlarmText { get; private set; }
        public EquipmentAlarmText RestoreText { get; private set; }

        public EquipmentAlarmCategory AlarmCategory { get; private set; }
        public EquipmentAlarmCategory RestoreCategory { get; private set; }

        public EquipmentAlarmName AlarmName { get; protected set; }

    }

What do I need to do to resolve this issue?

Thanks.

Oren Eini (Ayende Rahien)

unread,
Jul 26, 2015, 1:26:59 AM7/26/15
to ravendb
You have a converter somewhere here, right?

Basically, eventually you are calling JsonWriter.WriteValue( object ) - and that isn't usually supported.

Hibernating Rhinos Ltd  

Oren Eini l CEO Mobile: + 972-52-548-6969

Office: +972-4-622-7811 l Fax: +972-153-4-622-7811

 


--
You received this message because you are subscribed to the Google Groups "RavenDB - 2nd generation document database" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ravendb+u...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Toby Russell

unread,
Jul 26, 2015, 2:08:25 AM7/26/15
to rav...@googlegroups.com
Hi Oren, 

Thank you for responding so quickly.

I haven't actually implemented a converter, I just followed your suggested resolution which simply suggested setting the  serializer.ObjectCreationHandler property to the ObjectCreationHandling.Replace enemy value as such (in the DocumentStore constructor initialization):

serializer.ObjectCreationHandling = ObjectCreationHandling.Replace;

So how do I get around this? Specifically what should I reimplement or remove to enable RavenDB to successfully serialize and thus persist my entity?

Thanks again,

Toby Russell

On 26 Jul 2015, at 1:26 pm, Oren Eini (Ayende Rahien) <aye...@ayende.com> wrote:

You have a converter somewhere here, right?

Basically, eventually you are calling JsonWriter.WriteValue( object ) - and that isn't usually supported.

Hibernating Rhinos Ltd  

Oren Eini l CEO Mobile: + 972-52-548-6969

Office: +972-4-622-7811 l to Fax: +972-153-4-622-7811

 

--
You received this message because you are subscribed to a topic in the Google Groups "RavenDB - 2nd generation document database" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/ravendb/UgkGEm_F9AY/unsubscribe.
To unsubscribe from this group and all its topics, send an email to ravendb+u...@googlegroups.com.

Oren Eini (Ayende Rahien)

unread,
Jul 26, 2015, 3:27:54 AM7/26/15
to ravendb
serializer.ObjectCreationHandling = ObjectCreationHandling.Replace;

Means create new instances. By default in 3.0, we try to reuse the same instance, to allow you to do things like:
new HashSet<string>(InvariantIgnorecase);

Hibernating Rhinos Ltd  

Oren Eini l CEO Mobile: + 972-52-548-6969

Toby Russell

unread,
Jul 26, 2015, 3:52:05 AM7/26/15
to rav...@googlegroups.com
It wouldn't work before I added that, so what is the correct setting? Or do I need to implement a TypeConverter of some kind?

Toby Russell

Oren Eini (Ayende Rahien)

unread,
Jul 26, 2015, 3:54:13 AM7/26/15
to ravendb
I'm not sure. Please try create a _full_ unit test that we can look at.

Oren Eini (Ayende Rahien)

unread,
Jul 26, 2015, 3:54:31 AM7/26/15
to ravendb
Note that using Replace is fine, I just don't know the exact issue in question given that you made a lot of modifications
Reply all
Reply to author
Forward
0 new messages