【问题标题】:Mongo DB BSON: Unable to determine the serialization information for interface (C#)Mongo DB BSON:无法确定接口的序列化信息(C#)
【发布时间】:2019-08-06 16:53:47
【问题描述】:

我是 mongo DB 的新手,拥有实现相同接口的不同类的对象。我将这些对象存储在工作正常的 mongo DB 中。问题是这个对象的反序列化。

这是我的界面和类:

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; }

对于过滤,我需要检查 e.Event.Account。这里出现错误

无法确定 e => e.Event.Account 的序列化信息

好像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);
    }
 }

如果我添加注释

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

上面

public IEvent Event { get; set; }

序列化有效,但我必须选择类(例如 BaseEvent)。这必须自动工作。

【问题讨论】:

    标签: c# mongodb bson mongodb.driver


    【解决方案1】:

    我想办法解决这个问题:

    显然在使用接口时无法反序列化。至少我无法让它工作。所以我将 EventLog.Event 的类型从 IEvent 更改为 BaseEvent,现在一切正常。

    提示:此外,重要的是,所有属性都是可读写的。除此之外,您还必须使用注释“BsonKnownTypes”。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-10-10
      • 2014-11-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-01-24
      • 1970-01-01
      相关资源
      最近更新 更多