Unknown discriminator value 'MyEvent'

心不动则不痛 提交于 2019-11-29 13:51:19

Try to register your objects (itself the event messages as well as the subjects of the EventStore payloads) using the BsonClassMap.RegisterClassMap method. It seems EventStore's mongo extension handles the string payloads well, but not the deserialized objects ... at least registering the classed was the solution in my case.

Marijn

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<TypeToMap> like Zsolt suggests, because that requires a generic type parameter, meaning you have to manually add each event type.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!