/// <summary> /// A method to create the cosmos client /// </summary> /// <remarks> /// Setting this property after sending any request won't have any effect. /// </remarks> internal virtual CosmosClient Build(DocumentClient documentClient) { CosmosClientConfiguration copyOfConfig = this.cosmosClientConfiguration.Clone(); DefaultTrace.TraceInformation($"CosmosClientBuilder.Build(DocumentClient) with configuration: {copyOfConfig.GetSerializedConfiguration()}"); return(new CosmosClient(copyOfConfig, documentClient)); }
/// <summary> /// A method to create the cosmos client /// </summary> /// <remarks> /// Setting this property after sending any request won't have any effect. /// </remarks> public virtual CosmosClient Build() { CosmosClientConfiguration copyOfConfig = this.cosmosClientConfiguration.Clone(); DefaultTrace.TraceInformation($"CosmosClientBuilder.Build with configuration: {copyOfConfig.GetSerializedConfiguration()}"); return(new CosmosClient(copyOfConfig)); }
internal void Init( CosmosClientConfiguration configuration, DocumentClient documentClient) { this.Configuration = configuration; this.DocumentClient = documentClient; this.CosmosJsonSerializer = new CosmosJsonSerializerWrapper(this.Configuration.CosmosJsonSerializer); //Request pipeline ClientPipelineBuilder clientPipelineBuilder = new ClientPipelineBuilder( this, this.DocumentClient.ResetSessionTokenRetryPolicy, this.Configuration.CustomHandlers ); // DocumentClient is not initialized with any consistency overrides so default is backend consistency this.AccountConsistencyLevel = (ConsistencyLevel)this.DocumentClient.ConsistencyLevel; this.RequestHandler = clientPipelineBuilder.Build(); CosmosClientContext clientContext = new CosmosClientContextCore( this, this.Configuration, this.CosmosJsonSerializer, this.ResponseFactory, this.RequestHandler, this.DocumentClient, new DocumentQueryClient(this.DocumentClient)); this.Databases = new CosmosDatabasesCore(clientContext); this.offerSet = new Lazy <CosmosOffers>(() => new CosmosOffers(this.DocumentClient), LazyThreadSafetyMode.PublicationOnly); }
/// <summary> /// Used for unit testing only. /// </summary> internal CosmosClient( CosmosClientConfiguration cosmosClientConfiguration, DocumentClient documentClient) { if (cosmosClientConfiguration == null) { throw new ArgumentNullException(nameof(cosmosClientConfiguration)); } if (documentClient == null) { throw new ArgumentNullException(nameof(documentClient)); } Init(cosmosClientConfiguration, documentClient); }
/// <summary> /// Extracts the account endpoint and key from the connection string. /// </summary> /// <example>"AccountEndpoint=https://mytestcosmosaccount.documents.azure.com:443/;AccountKey={SecretAccountKey};"</example> /// <param name="connectionString">The connection string must contain AccountEndpoint and AccountKey.</param> internal CosmosClientConfiguration(string connectionString) { if (string.IsNullOrWhiteSpace(connectionString)) { throw new ArgumentNullException(nameof(connectionString)); } DbConnectionStringBuilder builder = new DbConnectionStringBuilder { ConnectionString = connectionString }; this.AccountEndPoint = new Uri(CosmosClientConfiguration.GetValueFromSqlConnectionString(builder, CosmosClientConfiguration.ConnectionStringAccountEndpoint)); this.AccountKey = CosmosClientConfiguration.GetValueFromSqlConnectionString(builder, CosmosClientConfiguration.ConnectionStringAccountKey); Initialize(); }
internal CosmosClientContextCore( CosmosClient client, CosmosClientConfiguration clientConfiguration, CosmosJsonSerializer cosmosJsonSerializer, CosmosResponseFactory cosmosResponseFactory, CosmosRequestHandler requestHandler, DocumentClient documentClient, IDocumentQueryClient documentQueryClient) { this.Client = client; this.ClientConfiguration = clientConfiguration; this.JsonSerializer = cosmosJsonSerializer; this.ResponseFactory = cosmosResponseFactory; this.RequestHandler = requestHandler; this.DocumentClient = documentClient; this.DocumentQueryClient = documentQueryClient; }
/// <summary> /// Create a new CosmosClient with the cosmosClientConfiguration /// </summary> /// <param name="cosmosClientConfiguration">The <see cref="CosmosClientConfiguration"/> used to initialize the cosmos client.</param> /// <example> /// This example creates a CosmosClient /// <code language="c#"> /// <![CDATA[ /// CosmosClientBuilder cosmosClientBuilder = new CosmosClientBuilder("accountEndpoint", "accountkey"); /// using (CosmosClient cosmosClient = cosmosClientBuilder.Build()) /// { /// // Create a database and other CosmosClient operations /// } ///]]> /// </code> /// </example> internal CosmosClient(CosmosClientConfiguration cosmosClientConfiguration) { if (cosmosClientConfiguration == null) { throw new ArgumentNullException(nameof(cosmosClientConfiguration)); } DocumentClient documentClient = new DocumentClient( cosmosClientConfiguration.AccountEndPoint, cosmosClientConfiguration.AccountKey, apitype: cosmosClientConfiguration.ApiType, sendingRequestEventArgs: cosmosClientConfiguration.SendingRequestEventArgs, transportClientHandlerFactory: cosmosClientConfiguration.TransportClientHandlerFactory, connectionPolicy: cosmosClientConfiguration.GetConnectionPolicy(), enableCpuMonitor: cosmosClientConfiguration.EnableCpuMonitor, storeClientFactory: cosmosClientConfiguration.StoreClientFactory); Init( cosmosClientConfiguration, documentClient); }
/// <summary> /// Extracts the account endpoint and key from the connection string. /// </summary> /// <example>"AccountEndpoint=https://mytestcosmosaccount.documents.azure.com:443/;AccountKey={SecretAccountKey};"</example> /// <param name="connectionString">The connection string must contain AccountEndpoint and AccountKey.</param> public CosmosClientBuilder(string connectionString) { this.cosmosClientConfiguration = new CosmosClientConfiguration(connectionString); }
/// <summary> /// Initialize a new CosmosConfiguration class that holds all the properties the CosmosClient requires. /// </summary> /// <param name="accountEndPoint">The Uri to the Cosmos Account. Example: https://{Cosmos Account Name}.documents.azure.com:443/ </param> /// <param name="accountKey">The key to the account.</param> /// <example> /// The example below creates a new <see cref="CosmosClientBuilder"/> /// <code language="c#"> /// <![CDATA[ /// CosmosClientBuilder cosmosClientBuilder = new CosmosClientBuilder( /// accountEndPoint: "https://testcosmos.documents.azure.com:443/", /// accountKey: "SuperSecretKey"); /// CosmosClient client = cosmosClientBuilder.Build(); ///]]> /// </code> /// </example> /// <example> /// The example below creates a new <see cref="CosmosClientBuilder"/> with a ConsistencyLevel and a list of preferred locations. /// <code language="c#"> /// <![CDATA[ /// CosmosClientBuilder cosmosClientBuilder = new CosmosClientBuilder( /// accountEndPoint: "https://testcosmos.documents.azure.com:443/", /// accountKey: "SuperSecretKey") /// .UseConsistencyLevel(ConsistencyLevel.Strong) /// .UseCurrentRegion(Region.USEast2); /// CosmosClient client = cosmosClientBuilder.Build(); /// ]]> /// </code> /// </example> public CosmosClientBuilder( string accountEndPoint, string accountKey) { this.cosmosClientConfiguration = new CosmosClientConfiguration(accountEndPoint, accountKey); }