MartenDB serializer issue

278 Views Asked by At

Recently moved to latest version of MartenDB 6.4.0 and have started seeing the below error when attempting to query the postgres database for a list of stream events

Code await _querySession.Events.AggregateStreamAsync<TAggregate>(id, token: cancellationToken)

System.InvalidCastException: Reading as 'System.Collections.Generic.Dictionary`2[[System.String, System.Private.CoreLib, Version=6.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e],[System.Object, System.Private.CoreLib, Version=6.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e]]' is not supported for fields having DataTypeName 'jsonb'
 ---> System.NotSupportedException: Type 'Dictionary`2' required dynamic JSON serialization, which requires an explicit opt-in; call 'EnableDynamicJson' on 'NpgsqlDataSourceBuilder' or NpgsqlConnection.GlobalTypeMapper (see https://www.npgsql.org/doc/types/json.html and the 8.0 release notes for more details). Alternatively, if you meant to use Newtonsoft JSON.NET instead of System.Text.Json, call UseJsonNet() instead.

Example mt_events table payload

seq_id id stream_id data
10. 018d1b4e-e410-4071-a201-f8a796523c01 69a2d0c6-64ff-4b71-b850-c23014a5bc7f {"Id":123")

Martin config

options.Events.MetadataConfig.HeadersEnabled = true;
                options.Events.MetadataConfig.CausationIdEnabled = true;
                options.Events.MetadataConfig.CorrelationIdEnabled = true;
                options.UseDefaultSerialization(EnumStorage.AsString,
                    nonPublicMembersStorage: NonPublicMembersStorage.All);

Have tried altering the serializer and also downgrading Martin version with no luck

1

There are 1 best solutions below

0
RogerW On

As per the error message you are getting JSON serialization, which requires an explicit opt-in; call 'EnableDynamicJson' , you now need to explicitly opt into Dynamic Json.

This worked for me by adding this line.

NpgsqlConnection.GlobalTypeMapper.EnableDynamicJson();

See official release notes documentation on NPGSQL:

JSON POCO and other dynamic features now require an explicit opt-in