コード例 #1
0
        public async Task <ConnectionDescription> InitializeConnectionAsync(IConnection connection, CancellationToken cancellationToken)
        {
            Ensure.IsNotNull(connection, nameof(connection));
            var isMasterCommand  = CreateInitialIsMasterCommand(connection.Settings.Authenticators);
            var isMasterProtocol = IsMasterHelper.CreateProtocol(isMasterCommand);
            var isMasterResult   = await IsMasterHelper.GetResultAsync(connection, isMasterProtocol, cancellationToken).ConfigureAwait(false);

            var buildInfoProtocol = CreateBuildInfoProtocol();
            var buildInfoResult   = new BuildInfoResult(await buildInfoProtocol.ExecuteAsync(connection, cancellationToken).ConfigureAwait(false));

            var description = new ConnectionDescription(connection.ConnectionId, isMasterResult, buildInfoResult);

            await AuthenticationHelper.AuthenticateAsync(connection, description, cancellationToken).ConfigureAwait(false);

            try
            {
                var getLastErrorProtocol = CreateGetLastErrorProtocol();
                var getLastErrorResult   = await getLastErrorProtocol.ExecuteAsync(connection, cancellationToken).ConfigureAwait(false);

                description = UpdateConnectionIdWithServerValue(description, getLastErrorResult);
            }
            catch
            {
                // if we couldn't get the server's connection id, so be it.
            }

            return(description);
        }
コード例 #2
0
        private BsonDocument CreateInitialIsMasterCommand(IReadOnlyList <IAuthenticator> authenticators)
        {
            var command = IsMasterHelper.CreateCommand();

            IsMasterHelper.AddClientDocumentToCommand(command, _clientDocument);
            return(IsMasterHelper.CustomizeCommand(command, authenticators));
        }
コード例 #3
0
        public void AddClientDocumentToCommand_with_ConnectionInitializer_client_document_should_return_expected_result()
        {
            var command = IsMasterHelper.CreateCommand();
            var connectionInitializer = new ConnectionInitializer("test", new CompressorConfiguration[0], serverApi: null);
            var subjectClientDocument = (BsonDocument)Reflector.GetFieldValue(connectionInitializer, "_clientDocument");
            var result = IsMasterHelper.AddClientDocumentToCommand(command, subjectClientDocument);

            var names = result.Names.ToList();

            names.Count.Should().Be(2);
            names[0].Should().Be("isMaster");
            names[1].Should().Be("client");
            result[0].Should().Be(1);
            var clientDocument      = result[1].AsBsonDocument;
            var clientDocumentNames = clientDocument.Names.ToList();

            clientDocumentNames.Count.Should().Be(4);
            clientDocumentNames[0].Should().Be("application");
            clientDocumentNames[1].Should().Be("driver");
            clientDocumentNames[2].Should().Be("os");
            clientDocumentNames[3].Should().Be("platform");
            clientDocument["application"]["name"].AsString.Should().Be("test");
            clientDocument["driver"]["name"].AsString.Should().Be("mongo-csharp-driver");
            clientDocument["driver"]["version"].BsonType.Should().Be(BsonType.String);
        }
コード例 #4
0
        public void AddClientDocumentToCommand_with_custom_document_should_return_expected_result(
            [Values("{ client : { driver : 'dotnet', version : '2.4.0' }, os : { type : 'Windows' } }")]
            string clientDocumentString)
        {
            var clientDocument = BsonDocument.Parse(clientDocumentString);
            var command        = IsMasterHelper.CreateCommand();
            var result         = IsMasterHelper.AddClientDocumentToCommand(command, clientDocument);

            result.Should().Be($"{{ isMaster : 1, client : {clientDocumentString} }}");
        }
コード例 #5
0
        public void AddCompressorsToCommand_with_compressors_should_return_expected_result(
            [Values(
                 new CompressorType[] { },
                 new [] { CompressorType.Zlib },
                 new [] { CompressorType.Snappy },
                 new [] { CompressorType.Zlib, CompressorType.Snappy })]
            CompressorType[] compressorsParameters)
        {
            var command     = IsMasterHelper.CreateCommand();
            var compressors =
                compressorsParameters
                .Select(c => new CompressorConfiguration(c))
                .ToArray();
            var result = IsMasterHelper.AddCompressorsToCommand(command, compressors);

            var expectedCompressions = string.Join(",", compressorsParameters.Select(c => $"'{c.ToString().ToLowerInvariant()}'"));

            result.Should().Be(BsonDocument.Parse($"{{ isMaster : 1, compression: [{expectedCompressions}] }}"));
        }
コード例 #6
0
        public ConnectionDescription InitializeConnection(IConnection connection, CancellationToken cancellationToken)
        {
            Ensure.IsNotNull(connection, nameof(connection));
            var authenticators   = connection.Settings.AuthenticatorFactories.Select(f => f.Create()).ToList();
            var isMasterCommand  = CreateInitialIsMasterCommand(authenticators);
            var isMasterProtocol = IsMasterHelper.CreateProtocol(isMasterCommand, _serverApi);
            var isMasterResult   = IsMasterHelper.GetResult(connection, isMasterProtocol, cancellationToken);

            var buildInfoProtocol = CreateBuildInfoProtocol(_serverApi);
            var buildInfoResult   = new BuildInfoResult(buildInfoProtocol.Execute(connection, cancellationToken));

            var description = new ConnectionDescription(connection.ConnectionId, isMasterResult, buildInfoResult);

            AuthenticationHelper.Authenticate(connection, description, authenticators, cancellationToken);

            var connectionIdServerValue = isMasterResult.ConnectionIdServerValue;

            if (connectionIdServerValue.HasValue)
            {
                description = UpdateConnectionIdWithServerValue(description, connectionIdServerValue.Value);
            }
            else
            {
                try
                {
                    var getLastErrorProtocol = CreateGetLastErrorProtocol(_serverApi);
                    var getLastErrorResult   = getLastErrorProtocol.Execute(connection, cancellationToken);

                    description = UpdateConnectionIdWithServerValue(description, getLastErrorResult);
                }
                catch
                {
                    // if we couldn't get the server's connection id, so be it.
                }
            }

            return(description);
        }