NEventStore / NEventStore.Persistence.MongoDB

Mongo Persistence Engine for NEventStore
MIT License
22 stars 26 forks source link

Casting exception at line 70, ExtensionMethods.cs. BsonString -> BsonBinaryData #51

Closed tuyen-vuduc closed 2 years ago

tuyen-vuduc commented 7 years ago

Hi,

I tried to use and create custom IDeserializer using JSON.NET but it keeps saying a casting exception at line 70, ExtensionMethods.cs. BsonString -> BsonBinaryData

   at NEventStore.Persistence.MongoDB.ExtensionMethods.<>c__DisplayClass6.<ToCommit>b__5(BsonValue e) in c:\agents\nes\work\623237454e7de2b5\src\NEventStore.Persistence.MongoDB\ExtensionMethods.cs:line 70
   at System.Linq.Enumerable.WhereSelectEnumerableIterator`2.MoveNext()
   at System.Collections.Generic.List`1..ctor(IEnumerable`1 collection)
   at System.Linq.Enumerable.ToList[TSource](IEnumerable`1 source)
   at NEventStore.Persistence.MongoDB.ExtensionMethods.ToCommit(BsonDocument doc, IDocumentSerializer serializer) in c:\agents\nes\work\623237454e7de2b5\src\NEventStore.Persistence.MongoDB\ExtensionMethods.cs:line 68
   at System.Linq.Enumerable.WhereSelectEnumerableIterator`2.MoveNext()
   at NEventStore.Persistence.PipelineHooksAwarePersistanceDecorator.<ExecuteHooks>d__3.MoveNext() in c:\TeamCity\buildAgent\work\38b1777f2112a252\src\NEventStore\Persistence\PipelineHooksAwarePersistanceDecorator.cs:line 137
   at NEventStore.OptimisticEventStream.PopulateStream(Int32 minRevision, Int32 maxRevision, IEnumerable`1 commits) in c:\TeamCity\buildAgent\work\38b1777f2112a252\src\NEventStore\OptimisticEventStream.cs:line 122
   at NEventStore.OptimisticEventStream..ctor(String bucketId, String streamId, ICommitEvents persistence, Int32 minRevision, Int32 maxRevision) in c:\TeamCity\buildAgent\work\38b1777f2112a252\src\NEventStore\OptimisticEventStream.cs:line 35
   at NEventStore.OptimisticEventStore.OpenStream(String bucketId, String streamId, Int32 minRevision, Int32 maxRevision) in c:\TeamCity\buildAgent\work\38b1777f2112a252\src\NEventStore\OptimisticEventStore.cs:line 75
   at NEventStore.StoreEventsExtensions.OpenStream(IStoreEvents storeEvents, Guid streamId, Int32 minRevision, Int32 maxRevision) in c:\TeamCity\buildAgent\work\38b1777f2112a252\src\NEventStore\StoreEventsExtensions.cs:line 58
AGiorgetti commented 7 years ago

Hi, yes the library is still in development (check the 'develop' branch),

for an example of a custom json serializer check the project: https://github.com/NEventStore/NEventStore.CustomJsonSerializerSample

tuyen-vuduc commented 7 years ago

Thank you @AGiorgetti.

However, it doesn't really relate to my issue. My issue is to provide a custom IDocumentSerializer to UsingMongoPersistence.

What should I do?

tuyen-vuduc commented 7 years ago

Actually, my custom IDocumentSerializer not even reached. I create a new EventMessage, then commit following the example.

AGiorgetti commented 7 years ago

the problem lies in this code line: "serializer.Deserialize(e.AsBsonDocument[MongoCommitFields.Payload].AsByteArray)))"

it works only if the custom IDocumentSerializer produces array of bytes, that part need some love.

there should be a ByteStreamDocumentSerializer somewhere in NEventStore (if i remind well).

I'm honestly not sure if we really would want to keep this document serializer, I think in the original design was a way to convert one object to another, but I have to review the code to look for some hints.

Why do you need to customize the serialization ? can't you do that using the standard MongoDB mapping?

tuyen-vuduc commented 7 years ago

That is a not optional parameter of this method UsingMongoPersistence.

As I debugged, my custom deserializer isn't reached yet. I think that is an internal issue.

AGiorgetti commented 7 years ago

The the '.deserialize()' methos is not called because the payload of the commit is a bson document,

the whole code fragment of interest is (line 70 on the master branch, have to check on develop, but there will be something similar there too):

.Select(e => e.AsBsonDocument[MongoCommitFields.Payload].IsBsonDocument ? BsonSerializer.Deserialize<EventMessage>(e.AsBsonDocument[MongoCommitFields.Payload].ToBsonDocument()) : serializer.Deserialize<EventMessage>(e.AsBsonDocument[MongoCommitFields.Payload].AsByteArray))

if the payload is a bson document the standard MongoDB serialization will be used, any other option will be handled as a byte[]. That's why I said we will probably get rid of the DocumentSerializer sooner or later, I bet no one really used it with the Mongo Persistence

andreabalducci commented 7 years ago

@kanvuduc have you tried the develop branch? the mongo mapping can be customized using a BsonSerializer (check line 139).

It's something I've to checkout, we may need to to work with custom json serializer for Events to avoid TypeSystem as Schema and handle gracefully versioning.

Let me know if a custom serializer helps (no time to check for the next 2 weeks).

AGiorgetti commented 2 years ago

closing due to inactivity