From 02d4572fa7c20669043225a591f950d91a491c92 Mon Sep 17 00:00:00 2001 From: bastiantoepfer Date: Wed, 20 Nov 2024 10:07:01 +0100 Subject: [PATCH] CSHARP-5419 Fix Single Node Replica Set Handling in the Driver --- src/MongoDB.Driver/Core/Clusters/ClusterFactory.cs | 2 +- src/MongoDB.Driver/Core/Clusters/SingleServerCluster.cs | 5 +++-- 2 files changed, 4 insertions(+), 3 deletions(-) diff --git a/src/MongoDB.Driver/Core/Clusters/ClusterFactory.cs b/src/MongoDB.Driver/Core/Clusters/ClusterFactory.cs index bb836cae1cb..e16c1ebd642 100644 --- a/src/MongoDB.Driver/Core/Clusters/ClusterFactory.cs +++ b/src/MongoDB.Driver/Core/Clusters/ClusterFactory.cs @@ -52,7 +52,7 @@ public IClusterInternal CreateCluster() { return CreateLoadBalancedCluster(settings); } - else if (settings.DirectConnection) + else if (settings.DirectConnection || settings.EndPoints.Count == 1) { return CreateSingleServerCluster(settings); } diff --git a/src/MongoDB.Driver/Core/Clusters/SingleServerCluster.cs b/src/MongoDB.Driver/Core/Clusters/SingleServerCluster.cs index 0f6651a2b92..242a0c9f643 100644 --- a/src/MongoDB.Driver/Core/Clusters/SingleServerCluster.cs +++ b/src/MongoDB.Driver/Core/Clusters/SingleServerCluster.cs @@ -32,7 +32,7 @@ internal sealed class SingleServerCluster : Cluster public SingleServerCluster(ClusterSettings settings, IClusterableServerFactory serverFactory, IEventSubscriber eventSubscriber, ILoggerFactory loggerFactory) : base(settings, serverFactory, eventSubscriber, loggerFactory) { - Ensure.That(settings.DirectConnection, $"DirectConnection mode is not supported for {nameof(SingleServerCluster)}."); + Ensure.That(!settings.DirectConnection, $"DirectConnection mode is not supported for {nameof(SingleServerCluster)}."); Ensure.That(settings.SrvMaxHosts == 0, "srvMaxHosts cannot be used with a single server cluster."); Ensure.IsEqualTo(settings.EndPoints.Count, 1, nameof(settings.EndPoints.Count)); @@ -121,7 +121,8 @@ private void ServerDescriptionChanged(object sender, ServerDescriptionChangedEve } newClusterDescription = newClusterDescription.WithServerDescription(newServerDescription); - + if(newClusterDescription.Type == ClusterType.Unknown) + newClusterDescription = newClusterDescription.WithType(newServerDescription.Type.ToClusterType()); var shouldClusterDescriptionChangedEventBePublished = !args.OldServerDescription.SdamEquals(args.NewServerDescription); UpdateClusterDescription(newClusterDescription, shouldClusterDescriptionChangedEventBePublished); }