Having a problem deserializing iDictionary object with C# driver.

104 views
Skip to first unread message

Darrin Olson

unread,
Jan 19, 2015, 3:41:14 PM1/19/15
to mongod...@googlegroups.com
I'm running into a problem deserializing an object that has a property which inherits from iDictionary using the standard C# driver for MongoDB. The error message I'm getting is: An error occurred while deserializing the cxnProps property of class MyClass: DictionarySerializer<String, String> can't be used with type String.

I set the SetSerializationOptions of the cxnProps object to DictionarySerializationOptions.ArrayOfDocuments (Actually I tried them all) and it seems to serialize into the database without a problem, but I get the error when I'm trying to read it back into the MyClass class using:

var results = myMongoCollection.AsQueryable<MyClass>().Where(e => e.MyId== myclass.MyId).ToArray();

In the database the cxnProps property looks like this:

"CxnProps" : [ { "k" : "thisID", : "123456" } ] }  --when using the ArrayOfDocuments representation.

The CxnProps property is an object that looks like this:

public interface CxnCollection: IDictionary<string, string>  
    {
        string UserName { get; set; }
        string RedirectURL { get; set; }
    }

Why would I be getting this type of error when deserializing?

Darrin Olson

unread,
Jan 20, 2015, 12:02:21 PM1/20/15
to mongod...@googlegroups.com
After looking into this more, I believe this issue was because I have an object that has a property which is an interface. The C# driver for MongoDB is unable to instantiate interfaces to deserialize them.

Robert Stam

unread,
Jan 20, 2015, 12:33:37 PM1/20/15
to mongod...@googlegroups.com
Which version of the C# driver are you using?

Can you provide a complete class definition for the class that is failing. I would like to reproduce this.

The particular error message you are getting might be caused if you have existing documents in the database that don't conform to the required representation. This sometimes happens when you change a class definition during development and you have already saved some documents using the old class definition.

--
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/97e89dee-80e9-40ff-b0dd-32a6fce1f1fe%40googlegroups.com.

For more options, visit https://groups.google.com/d/optout.

Darrin Olson

unread,
Jan 20, 2015, 12:45:06 PM1/20/15
to mongod...@googlegroups.com
I'm using version 1.9.2.235. I'm trying to read the object back directly after I write it, so the object should be the same. 

Here is what the object looks like that I am writing. It writes without any problem, looks correct in MongoDB and I can query it through MongoDB without trouble, but I cannot read it back through the C# driver.

 public class SSOConnectionContext : ISSOConnectionContext
    {
        private Guid ssoconnectionID;
        private DateTimeOffset? creationDate;
        private bool expired;
        private IConnectionBagCollection connectionProps;
       

        public Guid SSOConnectionID
        { 
            get
            {
                return ssoconnectionID;
            }
            set
            {
                ssoconnectionID = value; 
            }
        }

        public DateTimeOffset? CreationDate
        {
            get
            {
                return creationDate;
            }
            set
            {
                creationDate = value; 
            }
        }

        public bool Expired
        {
            get
            {
                return expired;
            }
            set
            {
                expired = value;
            }
        }

        public string Token { get; set; } 
        public string ReturnUrl { get; set; }

        public IConnectionBagCollection ConnectionProps
        {
            get
            {
                return connectionProps;
            }
            set
            {
                connectionProps = value;
            }
        }
    }

And the IConnectionBagCollection looks like this:

public interface IConnectionBagCollection : IDictionary<string, string>  
    {
        string UserName { get; set; }
        string RedirectURL { get; set; }
    }

If I change the IConnectionBagCollection to a class, like a Dictionary, and not an interface then it writes to the database exactly the same and will read back without error.

Robert Stam

unread,
Jan 22, 2015, 10:02:12 AM1/22/15
to mongod...@googlegroups.com
I'm having trouble trying to reproduce this. There are still some missing pieces of information (mainly about the implementation of IConnectionBagCollection... for example, do the UserName and RedirectURL have their own backing fields or are they just alternate ways to access keyed values in the dictionary?).

If you could distill this problem down to a small self-contained code sample that I could compile and run that would be extremely helpful.

Thanks.

--
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.
Reply all
Reply to author
Forward
0 new messages