Exemple #1
0
 static void builder(CosmosClientBuilder action)
 {
     action.WithSerializerOptions(new CosmosSerializationOptions()
     {
         PropertyNamingPolicy = CosmosPropertyNamingPolicy.CamelCase
     });
 }
Exemple #2
0
        public static CosmosClient CreateMockCosmosClient(
            bool useCustomSerializer = false,
            Action <CosmosClientBuilder> customizeClientBuilder = null)
        {
            MockDocumentClient  documentClient      = new MockDocumentClient();
            CosmosClientBuilder cosmosClientBuilder = new CosmosClientBuilder("http://localhost", Convert.ToBase64String(Guid.NewGuid().ToByteArray()));

            cosmosClientBuilder.WithConnectionModeDirect();
            customizeClientBuilder?.Invoke(cosmosClientBuilder);

            if (useCustomSerializer)
            {
                cosmosClientBuilder.WithSerializerOptions(
                    new CosmosSerializationOptions()
                {
                    IgnoreNullValues = true,
                });
            }

            documentClient.dummyHeaderNames = new string[100];
            for (int i = 0; i < documentClient.dummyHeaderNames.Length; i++)
            {
                documentClient.dummyHeaderNames[i] = Guid.NewGuid().ToString();
            }
            documentClient.dummyHeaderNames[0] = HttpConstants.HttpHeaders.ActivityId;
            documentClient.dummyHeaderNames[1] = HttpConstants.HttpHeaders.SessionToken;
            documentClient.dummyHeaderNames[2] = HttpConstants.HttpHeaders.ConsistencyLevel;
            documentClient.dummyHeaderNames[3] = HttpConstants.HttpHeaders.XDate;

            return(cosmosClientBuilder.Build(documentClient));
        }
        public static CosmosClient CreateCosmosClient(this ClientContextOptions options)
        {
            options.Validate();

            var clientBuilder = new CosmosClientBuilder(
                options.ConnectionString);

            if (options.UseThrottling)
            {
                clientBuilder = clientBuilder
                                .WithThrottlingRetryOptions(
                    options.MaxRetryWaitTimeOnThrottledRequests ?? new TimeSpan(0, 0, 0, 30),
                    options.MaxRetryAttemptsOnThrottledRequests ?? 3);
            }

            //if (Enum.TryParse<ConsistencyLevel>(options.ConsistencyLevel, out var consistencyLevel))
            //    clientBuilder = clientBuilder
            //        .WithConsistencyLevel(consistencyLevel);

            if (options.AllowBulkExecution.HasValue)
            {
                clientBuilder = clientBuilder
                                .WithBulkExecution(options.AllowBulkExecution.Value);
            }

            if (options.CamelCasePropertyNames)
            {
                clientBuilder = clientBuilder.WithSerializerOptions(new CosmosSerializationOptions
                {
                    PropertyNamingPolicy = CosmosPropertyNamingPolicy.CamelCase
                });
            }

            return(clientBuilder.Build());
        }
        private CosmosClient GetCosmosClient(IServiceProvider provider)
        {
            var serializerOptions = new CosmosSerializationOptions()
            {
                IgnoreNullValues     = true,
                Indented             = false,
                PropertyNamingPolicy = CosmosPropertyNamingPolicy.CamelCase
            };

            var connectionString    = Configuration["Values:CosmosDB:ConnectionString"];
            var cosmosClientBuilder = new CosmosClientBuilder(connectionString);

            return(cosmosClientBuilder.WithSerializerOptions(serializerOptions).Build());
        }
Exemple #5
0
        public static CosmosClient CreateMockCosmosClient(
            bool useCustomSerializer = false,
            Action <CosmosClientBuilder> customizeClientBuilder = null)
        {
            DocumentClient      documentClient      = new MockDocumentClient();
            CosmosClientBuilder cosmosClientBuilder = new CosmosClientBuilder("http://localhost", Guid.NewGuid().ToString());

            cosmosClientBuilder.WithConnectionModeDirect();
            customizeClientBuilder?.Invoke(cosmosClientBuilder);

            if (useCustomSerializer)
            {
                cosmosClientBuilder.WithSerializerOptions(
                    new CosmosSerializationOptions()
                {
                    IgnoreNullValues = true,
                });
            }

            return(cosmosClientBuilder.Build(documentClient));
        }