sporadic "Unknown Discriminator" with c# driver and automapping

74 views
Skip to first unread message

danieljsinclair

unread,
Jul 15, 2014, 2:40:11 PM7/15/14
to mongod...@googlegroups.com
I have a nested class (I don't know why, or if it's relevant);

    public partial class Foo: Bar
    {
        public class InnerFoo
        {
...
        }
    }

InnerFoo get's serialized as 'Object' so gets automatically annoted with _t: "InnerFoo". I'm using automatic serialization and all works fine most of the time. However, I recently started seeing failures. After stepping through the driver source I discovered that InnerFoo was indeed missing from BsonSerializer.__discriminators. In fact, for the same program that list seems a different length every time I look at it, it's astounding it works the rest of the time.

It turns out that if I *Serialize* an InnerFoo first, it populates the list and works fine. However, if my code tries to "Deserialize" before it's ever Serialized, which is highly likely with web server process recycling it will fail to have the discriminator.

Q: How can I ensure that my discriminators are *fully* populated. I should probably turn OFF the auto-population since that's a disaster waiting to happen.

Robert Stam

unread,
Jul 15, 2014, 4:41:21 PM7/15/14
to mongod...@googlegroups.com
Auto registration of classes works reliably when either of the following is true:

1. You don't use polymorphism
2. You serialize data before deserializing it (this is not usually true in general)

When you have a propery of type object that also counts as a use of polymorphism.

All you need to do is register all your classes during the initialization phase of your application. Then all the discriminators will be known in advance.

The standard way to register a class is:

BsonClassMap.RegisterClassMap<MyClass>();

If you are using custom serializers the initialization would be somewhat different, but the same principle applies: you must ensure that everything serialization related is correctly configured before you start deserializing data (unless you're not using polymorphism in which case the automatic stuff works just fine).



--
You received this message because you are subscribed to the Google Groups "mongodb-user"
group.
 
For other MongoDB technical support options, see: http://www.mongodb.org/about/support/.
---
You received this message because you are subscribed to the Google Groups "mongodb-user" group.
To unsubscribe from this group and stop receiving emails from it, send an email to mongodb-user...@googlegroups.com.
To post to this group, send email to mongod...@googlegroups.com.
Visit this group at http://groups.google.com/group/mongodb-user.
To view this discussion on the web visit https://groups.google.com/d/msgid/mongodb-user/24993e4c-1a53-4c4c-b9f6-5232979f3c38%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply all
Reply to author
Forward
0 new messages