Unknown discriminator value 'MyEvent'

后端 未结 2 1438
梦毁少年i
梦毁少年i 2020-12-20 17:20

Using the MongoDB persistance engine in joliver/EventStore causing the error Unknown discriminator value \'MyEvent\'. The issue is only caused when I try to loa

2条回答
  •  暗喜
    暗喜 (楼主)
    2020-12-20 17:39

    I just ran into this too. Zsolt's answer was a good starting point, but I ended up solving it slightly different.

    Note that I did not only get this when myEventStore.Advanced.GetFrom(...); myEventStore.OpenStream(...) also fails. This makes sense, because both methods use the same IPersistentStream and serializer.

    I don't run into this problem when when I first persist an event, before retrieving an event of the same type. Apparently MongoDB creates a ClassMap when it's being asked to serialize a type for the first time.

    Anyway, for me the solution was to create a class map for all my event types on application startup. Assuming all type are in the assembly of SimpleCQRS.Event and derive from SimpleCQRS.Event, I do it like this:

    var types = Assembly.GetAssembly(typeof(SimpleCQRS.Event))
                        .GetTypes()
                        .Where(type => type.IsSubclassOf(typeof(SimpleCQRS.Event)));
    foreach (var t in types)
        BsonClassMap.LookupClassMap(t);
    

    For me this works better than using BsonClassMap.RegisterClassMap like Zsolt suggests, because that requires a generic type parameter, meaning you have to manually add each event type.

提交回复
热议问题