Skip to content

Commit

Permalink
issues(4893): try register serializer or skip when already registred …
Browse files Browse the repository at this point in the history
…8d23d6

issues(4893): try register serializer or skip when already registred  + clean
  • Loading branch information
BEN AMEUR authored and sfmskywalker committed Feb 7, 2024
1 parent 8d23d69 commit e64a2ea
Showing 1 changed file with 17 additions and 6 deletions.
23 changes: 17 additions & 6 deletions src/modules/Elsa.MongoDb/Features/MongoDbFeature.cs
Original file line number Diff line number Diff line change
Expand Up @@ -48,16 +48,16 @@ public override void Apply()

private static void RegisterSerializers()
{
BsonSerializer.TryRegisterSerializer(typeof(object), new PolymorphicSerializer());
BsonSerializer.TryRegisterSerializer(typeof(Type), new TypeSerializer());
BsonSerializer.TryRegisterSerializer(typeof(Variable), new VariableSerializer());
BsonSerializer.TryRegisterSerializer(typeof(Version), new VersionSerializer());
BsonSerializer.TryRegisterSerializer(typeof(JsonElement), new JsonElementSerializer());
TryRegisterSerializerOrSkipWhenExist(typeof(object), new PolymorphicSerializer());
TryRegisterSerializerOrSkipWhenExist(typeof(Type), new TypeSerializer());
TryRegisterSerializerOrSkipWhenExist(typeof(Variable), new VariableSerializer());
TryRegisterSerializerOrSkipWhenExist(typeof(Version), new VersionSerializer());
TryRegisterSerializerOrSkipWhenExist(typeof(JsonElement), new JsonElementSerializer());
}

private static void RegisterClassMaps()
{
BsonClassMap.RegisterClassMap<StoredBookmark>(cm =>
BsonClassMap.TryRegisterClassMap<StoredBookmark>(cm =>
{
cm.AutoMap(); // Automatically map other properties
cm
Expand All @@ -66,6 +66,17 @@ private static void RegisterClassMaps()
});
}

private static void TryRegisterSerializerOrSkipWhenExist(Type type, IBsonSerializer serializer)
{
try
{
BsonSerializer.TryRegisterSerializer(type, serializer);
}
catch (BsonSerializationException ex)
{
}
}

private static IMongoDatabase CreateDatabase(IServiceProvider sp, string connectionString)
{
var options = sp.GetRequiredService<IOptions<MongoDbOptions>>().Value;
Expand Down

0 comments on commit e64a2ea

Please sign in to comment.