예제 #1
0
        public static async Task Main(string[] args)
        {
            var connectionString        = "AccountEndpoint=https://localhost:8081/;AccountKey=C2y6yDjf5/R+ob0N8A7Cgv30VRDJIWEHLM+4QDU5DE2nQ9nDuVTqobD4b8mGGyPMbIZnqyMsEcaGQy67XIw/Jw==";
            var clientBuilder           = new CosmosClientBuilder(connectionString);
            var clientWithSerialization = clientBuilder.WithConnectionModeGateway().WithSerializerOptions(new CosmosSerializationOptions
            {
                PropertyNamingPolicy = CosmosPropertyNamingPolicy.CamelCase
            });
            var containerProperties = new ContainerProperties
            {
                Id = "testcontainer",
                PartitionKeyPath = $"/partitionKey"
            };

            var client    = clientWithSerialization.Build();
            var database  = (await client.CreateDatabaseIfNotExistsAsync("test")).Database;
            var container = (await database.CreateContainerIfNotExistsAsync(containerProperties)).Container;

            var item = new Item
            {
                Id           = Guid.NewGuid(),
                PartitionKey = "abc"
            };

            // await container.CreateItemAsync(item);

            var batch = container.CreateTransactionalBatch(new PartitionKey(item.PartitionKey));

            batch.CreateItem(item);
            await batch.ExecuteAsync();
        }
        private static CosmosClient CreateCosmosClient()
        {
            var endPoint = ConfigurationManager.AppSettings["EndPointUri"];
            var key      = ConfigurationManager.AppSettings["PrimaryKey"];
            CosmosClientBuilder clientBuilder = new CosmosClientBuilder(endPoint, key);

            return(clientBuilder.WithConnectionModeGateway().Build());
        }
예제 #3
0
        private static CosmosClientBuilder WithConnectionMode(this CosmosClientBuilder builder, string connectionMode)
        {
            if (!Enum.TryParse(connectionMode, true, out ConnectionMode mode))
            {
                mode = ConnectionMode.Direct;
            }

            return(mode == ConnectionMode.Direct
                ? builder.WithConnectionModeDirect()
                : builder.WithConnectionModeGateway());
        }
예제 #4
0
        internal static CosmosClient CreateCosmosClient(
            bool useGateway)
        {
            CosmosClientBuilder cosmosClientBuilder = GetClientBuilder(resourceToken: null);

            if (useGateway)
            {
                cosmosClientBuilder.WithConnectionModeGateway();
            }

            return(cosmosClientBuilder.Build());
        }
예제 #5
0
        private static Lazy <CosmosClient> SetupClient(ICosmosContainerOptions options)
        {
            var csb      = ValidatInput(options.ConnectionString, options.DatabaseId, options.ContainerId);
            var cacheKey = $"{options.DatabaseId}_{options.UseGatewayMode}_{ToSHA256(csb.Key)}";

            return(new Lazy <CosmosClient>(() =>
            {
                CosmosClient client = null;
                bool fromCache = false;
                if (_clientCache.ContainsKey(cacheKey))
                {
                    client = _clientCache[cacheKey];
                    if (client != null)
                    {
                        fromCache = true;
                    }
                }

                if (!fromCache)
                {
                    var builder = new CosmosClientBuilder(options.ConnectionString);
                    builder
                    .WithThrottlingRetryOptions(TimeSpan.FromSeconds(30), 10);

                    if (options.UseGatewayMode)
                    {
                        builder.WithConnectionModeGateway(options.ConcurrentConnections);
                    }

                    foreach (var region in options.AzureRegions ?? new List <string>())
                    {
                        builder.WithApplicationRegion(region);
                    }
                    client = builder.Build();

                    _clientCache[cacheKey] = client;
                }

                CreateDatabaseIfNotExistsAsync(client, options).GetAwaiter().GetResult();
                return client;
            }));
        }
        public void VerifyHttpClientHandlerIsSet()
        {
            string endpoint = AccountEndpoint;
            string key      = "425Mcv8CXQqzRNCgFNjIhT424GK99CKJvASowTnq15Vt8LeahXTcN5wt3342vQ==";

            IWebProxy webProxy = new TestWebProxy();

            CosmosClientBuilder cosmosClientBuilder = new CosmosClientBuilder(
                accountEndpoint: endpoint,
                authKeyOrResourceToken: key);

            cosmosClientBuilder.WithConnectionModeGateway(
                maxConnectionLimit: null,
                webProxy: webProxy);

            CosmosClient      cosmosClient     = cosmosClientBuilder.Build();
            CosmosHttpClient  cosmosHttpClient = cosmosClient.DocumentClient.httpClient;
            HttpClientHandler handler          = (HttpClientHandler)cosmosHttpClient.HttpMessageHandler;

            Assert.IsTrue(object.ReferenceEquals(webProxy, handler.Proxy));
        }