Mongo DB BSON: Unable to determine the serialization information for interface (C#)

966 Views Asked by At

I am a newbie to mongo DB and have objects of different classes which implement the same interface. I store these objects in a mongo DB which works fine. The problem is the deserialization of this objects.

Here is my interface and the classes:

public interface IEvent
{ ... }


[Serializable, JsonObject]
[BsonDiscriminator(Required = true)]
[BsonKnownTypes(typeof(CloudBaseEvent))]
public class BaseEvent: IEvent
{...}

[Serializable, JsonObject]
[BsonDiscriminator(Required = true)]
[BsonKnownTypes(typeof(CloudRenameEvent))]
public abstract class CloudBaseEvent : BaseEvent, IEvent
{

[Serializable, JsonObject]
[BsonDiscriminator(Required = true)]
public class CloudRenameEvent : CloudBaseEvent, IEvent
{ ... }

public class EventLog
{

    [BsonId]
    public string EventID { get; set; }

    [JsonProperty(TypeNameHandling = TypeNameHandling.Auto)]
    public IEvent Event { get; set; }

For the filtering I need to check e.Event.Account. Here occurs the error

Unable to determine the serialization information for e => e.Event.Account

It seems that the deserialisation of e.Event does not work:

internal class MyRepository
{

    private readonly IMongoDbRepository<Types.Models.EventLog> _mongoDb;

    /// <summary>
    /// Static constructor, initializes the MongoDB ClassMap.
    /// </summary>
    static MyRepository()
    {
        BsonClassMap.RegisterClassMap<Types.Models.EventLog>(cm =>
        {
            cm.AutoMap();
            cm.SetIgnoreExtraElements(true);
        });
        BsonClassMap.RegisterClassMap<BaseEvent>(cm =>
        {
            cm.AutoMap();
            cm.SetIgnoreExtraElements(true);
        });
        BsonClassMap.RegisterClassMap<CloudRenameEvent>(cm =>
        {
            cm.AutoMap();
            cm.SetIgnoreExtraElements(true);
        });
        MongoDefaults.GuidRepresentation = GuidRepresentation.Standard;
    }


public async Task<EventLog.Types.Models.EventLog> GetEventLogAsync(string eventId)
    {
        var collection = await _mongoDb.GetCollectionAsync();
        var filter = GetCustomerEventLogFilter(eventId);

        var res = await collection.Aggregate()
    //Here occurs the error
            .Match(filter)
            .FirstOrDefaultAsync();
        return res;
    }


private FilterDefinition<Types.Models.EventLog> GetCustomerEventLogFilter(string eventId)
    {
        var filters = new List<FilterDefinition<Types.Models.EventLog>>
        {
            Builders<Types.Models.EventLog>.Filter.Eq(e => e.Event.Account, Account),
        };           


        return Builders<Types.Models.EventLog>.Filter.And(filters);
    }
 }

If I add the annotation

[BsonSerializer(typeof(ImpliedImplementationInterfaceSerializer<IEvent, BaseEvent>))]

above

public IEvent Event { get; set; }

the serialization works but I have to choose the class (e.g. BaseEvent). This has to work automatically.

1

There are 1 best solutions below

0
On

I figured out to resolve this issue:

Apparently it is not possible to deserialize when using an interface. At least I could not get it working. So I changed the type of EventLog.Event from IEvent to BaseEvent and now everything is working fine.

Hint: Furthermore it was important, that all Properties where read- and writable. In addition to that you have to use the annotation "BsonKnownTypes".