internal static CosmosClientContext Create(
            CosmosClient cosmosClient,
            CosmosClientOptions clientOptions)
        {
            if (cosmosClient == null)
            {
                throw new ArgumentNullException(nameof(cosmosClient));
            }

            clientOptions = ClientContextCore.CreateOrCloneClientOptions(clientOptions);
            HttpMessageHandler httpMessageHandler = CosmosHttpClientCore.CreateHttpClientHandler(
                clientOptions.GatewayModeMaxConnectionLimit,
                clientOptions.WebProxy);

            DocumentClient documentClient = new DocumentClient(
                cosmosClient.Endpoint,
                cosmosClient.AuthorizationTokenProvider,
                apitype: clientOptions.ApiType,
                sendingRequestEventArgs: clientOptions.SendingRequestEventArgs,
                transportClientHandlerFactory: clientOptions.TransportClientHandlerFactory,
                connectionPolicy: clientOptions.GetConnectionPolicy(),
                enableCpuMonitor: clientOptions.EnableCpuMonitor,
                storeClientFactory: clientOptions.StoreClientFactory,
                desiredConsistencyLevel: clientOptions.GetDocumentsConsistencyLevel(),
                handler: httpMessageHandler,
                sessionContainer: clientOptions.SessionContainer);

            return(ClientContextCore.Create(
                       cosmosClient,
                       documentClient,
                       clientOptions));
        }
        internal static CosmosClientContext Create(
            CosmosClient cosmosClient,
            CosmosClientOptions clientOptions)
        {
            if (cosmosClient == null)
            {
                throw new ArgumentNullException(nameof(cosmosClient));
            }

            clientOptions = ClientContextCore.CreateOrCloneClientOptions(clientOptions);

            DocumentClient documentClient = new DocumentClient(
                cosmosClient.Endpoint,
                cosmosClient.AccountKey,
                apitype: clientOptions.ApiType,
                sendingRequestEventArgs: clientOptions.SendingRequestEventArgs,
                transportClientHandlerFactory: clientOptions.TransportClientHandlerFactory,
                connectionPolicy: clientOptions.GetConnectionPolicy(),
                enableCpuMonitor: clientOptions.EnableCpuMonitor,
                storeClientFactory: clientOptions.StoreClientFactory,
                desiredConsistencyLevel: clientOptions.GetDocumentsConsistencyLevel(),
                handler: ClientContextCore.CreateHttpClientHandler(clientOptions),
                sessionContainer: clientOptions.SessionContainer);

            return(ClientContextCore.Create(
                       cosmosClient,
                       documentClient,
                       clientOptions));
        }
        internal static CosmosClientContext Create(
            CosmosClient cosmosClient,
            DocumentClient documentClient,
            CosmosClientOptions clientOptions,
            RequestInvokerHandler requestInvokerHandler = null)
        {
            if (cosmosClient == null)
            {
                throw new ArgumentNullException(nameof(cosmosClient));
            }

            if (documentClient == null)
            {
                throw new ArgumentNullException(nameof(documentClient));
            }

            clientOptions = ClientContextCore.CreateOrCloneClientOptions(clientOptions);

            if (requestInvokerHandler == null)
            {
                //Request pipeline
                ClientPipelineBuilder clientPipelineBuilder = new ClientPipelineBuilder(
                    cosmosClient,
                    clientOptions.ConsistencyLevel,
                    clientOptions.CustomHandlers);

                requestInvokerHandler = clientPipelineBuilder.Build();
            }

            CosmosSerializerCore serializerCore = CosmosSerializerCore.Create(
                clientOptions.Serializer,
                clientOptions.SerializerOptions);

            // This sets the serializer on client options which gives users access to it if a custom one is not configured.
            clientOptions.SetSerializerIfNotConfigured(serializerCore.GetCustomOrDefaultSerializer());

            CosmosResponseFactoryInternal responseFactory = new CosmosResponseFactoryCore(serializerCore);

            return(new ClientContextCore(
                       client: cosmosClient,
                       clientOptions: clientOptions,
                       serializerCore: serializerCore,
                       cosmosResponseFactory: responseFactory,
                       requestHandler: requestInvokerHandler,
                       documentClient: documentClient,
                       userAgent: documentClient.ConnectionPolicy.UserAgentContainer.UserAgent,
                       batchExecutorCache: new BatchAsyncContainerExecutorCache()));
        }
Exemplo n.º 4
0
        internal static CosmosClientContext Create(
            CosmosClient cosmosClient,
            DocumentClient documentClient,
            CosmosClientOptions clientOptions,
            RequestInvokerHandler requestInvokerHandler = null)
        {
            if (cosmosClient == null)
            {
                throw new ArgumentNullException(nameof(cosmosClient));
            }

            if (documentClient == null)
            {
                throw new ArgumentNullException(nameof(documentClient));
            }

            clientOptions = ClientContextCore.CreateOrCloneClientOptions(clientOptions);

            if (requestInvokerHandler == null)
            {
                //Request pipeline
                ClientPipelineBuilder clientPipelineBuilder = new ClientPipelineBuilder(
                    cosmosClient,
                    clientOptions.ConsistencyLevel,
                    clientOptions.CustomHandlers);

                requestInvokerHandler = clientPipelineBuilder.Build();
            }

            CosmosSerializerCore serializerCore = CosmosSerializerCore.Create(
                clientOptions.Serializer,
                clientOptions.SerializerOptions);

            CosmosResponseFactory responseFactory = new CosmosResponseFactory(serializerCore);

            return(new ClientContextCore(
                       client: cosmosClient,
                       clientOptions: clientOptions,
                       serializerCore: serializerCore,
                       cosmosResponseFactory: responseFactory,
                       requestHandler: requestInvokerHandler,
                       documentClient: documentClient,
                       userAgent: documentClient.ConnectionPolicy.UserAgentContainer.UserAgent,
                       encryptionProcessor: new EncryptionProcessor(),
                       dekCache: new DekCache(),
                       batchExecutorCache: new BatchAsyncContainerExecutorCache()));
        }
        internal static CosmosClientContext Create(
            CosmosClient cosmosClient,
            DocumentClient documentClient,
            CosmosClientOptions clientOptions,
            RequestInvokerHandler requestInvokerHandler = null)
        {
            if (cosmosClient == null)
            {
                throw new ArgumentNullException(nameof(cosmosClient));
            }

            if (documentClient == null)
            {
                throw new ArgumentNullException(nameof(documentClient));
            }

            clientOptions = ClientContextCore.CreateOrCloneClientOptions(clientOptions);

            ConnectionPolicy connectionPolicy = clientOptions.GetConnectionPolicy(cosmosClient.ClientId);
            ClientTelemetry  telemetry        = null;

            if (connectionPolicy.EnableClientTelemetry)
            {
                try
                {
                    telemetry = ClientTelemetry.CreateAndStartBackgroundTelemetry(
                        documentClient: documentClient,
                        userAgent: connectionPolicy.UserAgentContainer.UserAgent,
                        connectionMode: connectionPolicy.ConnectionMode,
                        authorizationTokenProvider: cosmosClient.AuthorizationTokenProvider,
                        diagnosticsHelper: DiagnosticsHandlerHelper.Instance,
                        preferredRegions: clientOptions.ApplicationPreferredRegions);
                }
                catch (Exception ex)
                {
                    DefaultTrace.TraceInformation($"Error While starting Telemetry Job : {ex.Message}. Hence disabling Client Telemetry");
                    connectionPolicy.EnableClientTelemetry = false;
                }
            }
            else
            {
                DefaultTrace.TraceInformation("Client Telemetry Disabled.");
            }

            if (requestInvokerHandler == null)
            {
                //Request pipeline
                ClientPipelineBuilder clientPipelineBuilder = new ClientPipelineBuilder(
                    cosmosClient,
                    clientOptions.ConsistencyLevel,
                    clientOptions.CustomHandlers,
                    telemetry: telemetry);

                requestInvokerHandler = clientPipelineBuilder.Build();
            }

            CosmosSerializerCore serializerCore = CosmosSerializerCore.Create(
                clientOptions.Serializer,
                clientOptions.SerializerOptions);

            // This sets the serializer on client options which gives users access to it if a custom one is not configured.
            clientOptions.SetSerializerIfNotConfigured(serializerCore.GetCustomOrDefaultSerializer());

            CosmosResponseFactoryInternal responseFactory = new CosmosResponseFactoryCore(serializerCore);

            return(new ClientContextCore(
                       client: cosmosClient,
                       clientOptions: clientOptions,
                       serializerCore: serializerCore,
                       cosmosResponseFactory: responseFactory,
                       requestHandler: requestInvokerHandler,
                       documentClient: documentClient,
                       userAgent: documentClient.ConnectionPolicy.UserAgentContainer.UserAgent,
                       batchExecutorCache: new BatchAsyncContainerExecutorCache(),
                       telemetry: telemetry));
        }