Reputation: 2845
I'm getting this issue using the c# driver for mongodb (v1.5)
I've had similar issues to this when serializing objects, but have always been able to resolve the in the past by registering the entity with mongodb during application startup. The document it's having a problem deserializing is nested two levels deep (ie a document embedded within a document embedded within a document).
The classes look like this:
[BsonIgnoreExtraElements]
public class FooItem : IFooItem
{
[BsonId]
public ObjectId Id { get; set; }
public IFooAccessRestrictions AccessRestrictions { get; set; }
}
public class FooAccessRestrictions : IFooAccessRestrictions
{
[BsonId]
public ObjectId Id { get; set; }
public IAccessPermission[] AccessList { get; set; }
}
public class AccessPermission : IAccessPermission
{
[BsonId]
public ObjectId Id { get; set; }
public DateTimeOffset CreatedOn { get; set; }
public ObjectId CreatedBy { get; set; }
public AccessPermissionType Type { get; set; }
public string PermittedIdentity { get; set; }
public AccessPermission()
{
}
public AccessPermission(ObjectId createdBy, AccessPermissionType type, string permittedIdentity)
{
CreatedOn = DateTime.Now;
CreatedBy = createdBy;
Type = type;
PermittedIdentity = permittedIdentity;
}
}
It's the AccessPermission class that it has the 'No serializer found for type' problem with. I've tried registering the entities with mongodb as follows on application start:
BsonClassMap.RegisterClassMap<FooAccessRestrictions>();
BsonClassMap.RegisterClassMap<AccessPermission>();
I'm guessing I must be breaking some mongodb rule here that I'm not aware of. I hope I don't need to create a custom serializer for this ... As far as I can tell I'm not doing anything I haven't done before, apart from that the document is nested two levels deep. And it is creating the document no problem at all, it's just when I try to get it back out that I have the problem.
Any help would be much appreciated.
Upvotes: 2
Views: 954
Reputation: 2845
Okay, I've managed to fix this now.
After another look at the mongodb documentation, I found out that you can register properties of a class whilst registering the class.
so now my registration looks like this:
BsonClassMap.RegisterClassMap<FooAccessRestrictions>(cm =>
{
cm.MapProperty<List<IAccessPermission>>(c => (List<IAccessPermission>)c.AccessList);
});
(Note that my property has changed from an array to a List, but this shouldn;t have any impact on the issue I'm talking about here)
MongoDB is now able to deserialize these objects as well as serialize them.
Upvotes: 1