Uploaded image for project: 'C# Driver'
  1. C# Driver
  2. CSHARP-2579

BsonSerializerRegistry does not distinguish between cached serializers and explicitly registered ones

    • Type: Icon: Improvement Improvement
    • Resolution: Works as Designed
    • Priority: Icon: Major - P3 Major - P3
    • None
    • Affects Version/s: 2.8.0
    • Component/s: BSON, Serialization
    • Labels:
      None
    • Environment:
      Windows

      BsonSerializerRegistry.RegisterSerializer will just put the specified serializer into its internal cache, alongside other serializers that have been created by (default) providers before. If one tries to register a serializer for a type that has been looked up previously in the AppDomain, this will lead to an exception indicating that a serializer is already registered for the type. This can be especially confusing if there is only one call of the register method in the user's codebase. I would expect the explicit registration to override the default serializer; only throwing an exception if it has been registered explicitly before.

      In my use case, I use a library called Hangfire alongside my own code. Hangfire will always run before my code does, calling the MongoDB driver and filling the cache of the BsonSerializerRegistry with default serializers. Although it does not explicitly register global serializers itself, this will still clash with the serializer I want to register in my code, which I think it should not.

      Disclaimer:
      This is my first ticket and will be my first pull request. I hope I did not get anything fundamentally wrong, feedback will be greatly appreciated. I am especially unsure about Backwards Compatibility - I would guess this to be compatible, but could think of weird edge cases that would be broken by changing the behaviour (but I guess those cases would already be buggy as they are now).

            Assignee:
            robert@mongodb.com Robert Stam
            Reporter:
            dennisjaniak Dennis Janiak
            Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

              Created:
              Updated:
              Resolved: