[英]Mongo DB BSON: Unable to determine the serialization information for interface (C#)
I am a newbie to mongo DB and have objects of different classes which implement the same interface. 我是mongo DB的新手,并且具有实现同一接口的不同类的对象。 I store these objects in a mongo DB which works fine. 我将这些对象存储在正常工作的mongo DB中。 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. 对于筛选,我需要检查e.Event.Account。 Here occurs the error 这里发生错误
Unable to determine the serialization information for e => e.Event.Account 无法确定e => e.Event.Account的序列化信息
It seems that the deserialisation of e.Event does not work: 似乎e.Event的反序列化不起作用:
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 (eg BaseEvent). 序列化工作,但我必须选择类(例如BaseEvent)。 This has to work automatically. 这必须自动工作。
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. 所以我将EventLog.Event的类型从IEvent更改为BaseEvent,现在一切正常。
Hint: Furthermore it was important, that all Properties where read- and writable. 提示:此外,重要的是,所有属性都应是可读写的。 In addition to that you have to use the annotation "BsonKnownTypes". 除此之外,您还必须使用注释“ BsonKnownTypes”。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.