Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

issues(4885) #4886

Merged
merged 1 commit into from
Feb 5, 2024
Merged

issues(4885) #4886

merged 1 commit into from
Feb 5, 2024

Conversation

bbenameur
Copy link
Contributor

@bbenameur bbenameur commented Feb 5, 2024

Hello,
See issue:
#4885

Based on the context provided, it appears that we are encountering a serialization registration issue when running integration tests with Elsa and MongoDB. The error message indicates that a serializer for the Object type is being registered multiple times, which is not allowed by the MongoDB driver.

The code snippet provided suggests a solution to use BsonSerializer.TryRegisterSerializer instead of BsonSerializer.RegisterSerializer.

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());
}

@bbenameur
Copy link
Contributor Author

bbenameur commented Feb 5, 2024

@sfmskywalker can you please see and approve this PR ?

@bbenameur
Copy link
Contributor Author

@dotnet-policy-service agree

@sfmskywalker sfmskywalker merged commit f71b8c3 into elsa-workflows:main Feb 5, 2024
2 checks passed
Copy link

@bbenameur the command you issued was incorrect. Please try again.

Examples are:

@dotnet-policy-service agree

and

@dotnet-policy-service agree company="your company"

@bbenameur
Copy link
Contributor Author

@dotnet-policy-service agree

@hsnsalhi
Copy link

hsnsalhi commented Feb 7, 2024

this doesn't resolve the problem since TryRegisterSerializer throws an exception in addition to returning false if it couldn't register the serializer.
I suggest to put the code in a try catch block in a separate method to ensure the registration of every serializer even if one of them couldn't be registered.

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


        private static void TryRegisterSerializerWithoutException(Type type, IBsonSerializer serializer)
        {
            try
            {
                BsonSerializer.TryRegisterSerializer(type, serializer);
            }
            catch (BsonSerializationException ex)
            {
                // should we log the exception ?
            }
        }

@hsnsalhi
Copy link

hsnsalhi commented Feb 7, 2024

BTW, you will face the same problem with RegisterClassMap. I suggest you to replace it by TryRegisterClassMap this method does not throw an exception if it cannot register the class map. so no need to put a try catch block for it.

        private static void RegisterClassMaps()
        {
            BsonClassMap.TryRegisterClassMap<StoredBookmark>(cm =>
            {
                cm.AutoMap(); // Automatically map other properties
                cm
                    .MapIdProperty(b => b.BookmarkId)
                    .SetSerializer(new StringSerializer(BsonType.String));
            });
        }

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
elsa 3 This issue is specific to Elsa 3
Projects
Status: No status
Development

Successfully merging this pull request may close these issues.

None yet

3 participants