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

System.TimeoutException when connecting mongodb using .net core driver with docker

    • Type: Icon: Bug Bug
    • Resolution: Done
    • Priority: Icon: Major - P3 Major - P3
    • None
    • Affects Version/s: 2.9.0, 2.10.4
    • Component/s: Connectivity
    • Labels:
      None
    • Environment:
      linux docker

      Hello, I have a .net core program run in linux docker container. When I run the program in docker , the program fail to connect mongodb with replica set due to serverselectiontimeout. and it report below errors. We set a long time out 3-4 minutes, it could be connected. But too slow to connect mongodb.  If I run in linux using dotnet run, the program is normal without connection issue.  The issue born us a long time. Any solutions? how to resolve the issue?

      /////////////////////////////////////////////////////////////////////////////////

      Environment info:

      Mongodb driver version 2.10.4, actually, we try from 2.9.0 to 2.10.4, neither one works

      Mongodb version 4.2

      .Net core version 3.1

      Linux version: centos-release-7-7.1908.0.el7.centos.x86_64

      Docker version: we try on 18.09.6, build 481bc77156 and 19.03.5, build 633a0ea

      /////////////////////////////////////////////////////////////////////////////////

      Error message: 

      System.TimeoutException: A timeout occured after 30000ms selecting a server using CompositeServerSelector{ Selectors = MongoDB.Driver.MongoClient+AreSessionsSupportedServerSelector, LatencyLimitingServerSelector

      { AllowedLatencyRange = 00:00:00.0150000 }

      }. Client view of cluster state is { ClusterId : "1", ConnectionMode : "ReplicaSet", Type : "ReplicaSet", State : "Disconnected", Servers : [{ ServerId: "

      { ClusterId : 1, EndPoint : "Unspecified/ serverFQN:29031" }

      ", EndPoint: "Unspecified/serverFQN:29031", State: "Disconnected", Type: "Unknown", LastUpdateTimestamp: "2020-05-06T08:50:17.7201620Z" }, { ServerId: "

      { ClusterId : 1, EndPoint : "Unspecified/serverFQN:29032" }

      ", EndPoint: "Unspecified/serverFQN:29032", State: "Disconnected", Type: "Unknown", LastUpdateTimestamp: "2020-05-06T08:50:17.7260395Z" }, { ServerId: "

      { ClusterId : 1, EndPoint : "Unspecified/serverFQN:29033" }

      ", EndPoint: "Unspecified/ serverFQN:29033", State: "Disconnected", Type: "Unknown", LastUpdateTimestamp: "2020-05-06T08:50:17.7265232Z" }] }.
      at MongoDB.Driver.Core.Clusters.Cluster.ThrowTimeoutException(IServerSelector selector, ClusterDescription description)
      at MongoDB.Driver.Core.Clusters.Cluster.WaitForDescriptionChangedHelper.HandleCompletedTask(Task completedTask)
      at MongoDB.Driver.Core.Clusters.Cluster.WaitForDescriptionChanged(IServerSelector selector, ClusterDescription description, Task descriptionChangedTask, TimeSpan timeout, CancellationToken cancellationToken)
      at MongoDB.Driver.Core.Clusters.Cluster.SelectServer(IServerSelector selector, CancellationToken cancellationToken)
      at MongoDB.Driver.MongoClient.AreSessionsSupportedAfterServerSelection(CancellationToken cancellationToken)
      at MongoDB.Driver.MongoClient.AreSessionsSupported(CancellationToken cancellationToken)
      at MongoDB.Driver.MongoClient.StartImplicitSession(CancellationToken cancellationToken)
      at MongoDB.Driver.OperationExecutor.StartImplicitSession(CancellationToken cancellationToken)
      at MongoDB.Driver.MongoCollectionImpl`1.UsingImplicitSession[TResult](Func`2 func, CancellationToken cancellationToken)
      at MongoDB.Driver.MongoCollectionImpl`1.FindSync[TProjection](FilterDefinition`1 filter, FindOptions`2 options, CancellationToken cancellationToken)
      at MongoDB.Driver.FindFluent`2.ToCursor(CancellationToken cancellationToken)
      at MongoDB.Driver.IAsyncCursorSourceExtensions.ToList[TDocument](IAsyncCursorSource`1 source, CancellationToken cancellationToken)
      at PwC.Ms.Console.Program.Main(String[] args) in /src/console/Program.cs:line 81

      ///////////////////////////////////////////////////////////////////////////////////

      Sample code we used when connect mongodb:

       var clientSettings = MongoClientSettings.FromConnectionString(url);
      if (requireSsl)
      {
      clientSettings.SslSettings = new SslSettings

      { ServerCertificateValidationCallback = (sender, certificate, chain, errors) => true, CheckCertificateRevocation = false}

      ;
      clientSettings.VerifySslCertificate = false;
      clientSettings.UseSsl = true;
      clientSettings.AllowInsecureTls = true;
      }
      try

      { clientSettings.ServerSelectionTimeout = TimeSpan.FromMinutes(5); var client = new MongoClient(clientSettings); var database = client.GetDatabase(dbName); var fileFilter = Builders<BsonDocument>.Filter.Exists("_id"); var file1 = database.GetCollection<BsonDocument>("fs.files").Find(Builders<BsonDocument>.Filter.Empty).ToList(); System.Console.WriteLine(file1.Count); var file2 = database.GetCollection<BsonDocument>("fs.files").Find(Builders<BsonDocument>.Filter.Empty).ToList(); System.Console.WriteLine(file2.Count); var file3 = database.GetCollection<BsonDocument>("fs.files").Find(Builders<BsonDocument>.Filter.Empty).ToList(); System.Console.WriteLine(file3.Count); }

      catch (Exception e)

      { System.Console.WriteLine(e); }

       

      ///////////////////////////////////////////////////////////////////////////////////

      docker file content:

      FROM mcr.microsoft.com/dotnet/core/runtime:3.1-buster-slim AS base
      WORKDIR /app

      FROM mcr.microsoft.com/dotnet/core/sdk:3.1-buster AS build
      WORKDIR /src
      COPY ["console/Ms.Console.csproj", "console/"]
      RUN dotnet restore "console/Ms.Console.csproj"
      COPY . .
      WORKDIR "/src/console"
      RUN dotnet build "Ms.Console.csproj" -c Release -o /app/build

      FROM build AS publish
      RUN dotnet publish "Ms.Console.csproj" -c Release -o /app/publish

      FROM base AS final
      WORKDIR /app
      COPY --from=publish /app/publish .
      ENTRYPOINT ["dotnet", "Ms.Console.dll"]

       

       

            Assignee:
            Unassigned Unassigned
            Reporter:
            baichun.mu@cn.pwc.com baichun mu
            Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

              Created:
              Updated:
              Resolved: