public CosmosLinqQuery( ContainerCore container, CosmosResponseFactory responseFactory, CosmosQueryClientCore queryClient, string continuationToken, QueryRequestOptions cosmosQueryRequestOptions, Expression expression, bool allowSynchronousQueryExecution, CosmosSerializationOptions serializationOptions = null) { this.container = container ?? throw new ArgumentNullException(nameof(container)); this.responseFactory = responseFactory ?? throw new ArgumentNullException(nameof(responseFactory)); this.queryClient = queryClient ?? throw new ArgumentNullException(nameof(queryClient)); this.continuationToken = continuationToken; this.cosmosQueryRequestOptions = cosmosQueryRequestOptions; this.Expression = expression ?? Expression.Constant(this); this.allowSynchronousQueryExecution = allowSynchronousQueryExecution; this.correlatedActivityId = Guid.NewGuid(); this.serializationOptions = serializationOptions; this.queryProvider = new CosmosLinqQueryProvider( container, responseFactory, queryClient, this.continuationToken, cosmosQueryRequestOptions, this.allowSynchronousQueryExecution, this.queryClient.OnExecuteScalarQueryCallback, this.serializationOptions); }
/// <summary> /// Creates a Cosmos DB database and a container with the specified partition key. /// </summary> /// <returns></returns> private static async Task <CosmosDbClientFactory> InitializeCosmosClientInstanceAsync(IConfigurationSection configurationSection) { string databaseName = configurationSection.GetSection("DatabaseName").Value; string tutorLearningProfilesContainerName = configurationSection.GetSection("TutorLearningProfilesContainerName").Value; string chatMessagesContainerName = configurationSection.GetSection("ChatMessagesContainerName").Value; string account = configurationSection.GetSection("Account").Value; string key = configurationSection.GetSection("Key").Value; CosmosClientBuilder clientBuilder = new CosmosClientBuilder(account, key); var serializerOptions = new CosmosSerializationOptions { PropertyNamingPolicy = CosmosPropertyNamingPolicy.CamelCase }; CosmosClient client = clientBuilder .WithConnectionModeDirect() .WithSerializerOptions(serializerOptions) .Build(); CosmosDbClientFactory cosmosDbService = new CosmosDbClientFactory(client); DatabaseResponse database = await client.CreateDatabaseIfNotExistsAsync(databaseName); await database.Database.CreateContainerIfNotExistsAsync(tutorLearningProfilesContainerName, "/id"); await database.Database.CreateContainerIfNotExistsAsync(chatMessagesContainerName, "/id"); return(cosmosDbService); }
private void InitCosmosFromConfiguration(IConfiguration configuration) { string cosmosDB_Endpoint = configuration["CosmosDB_Endpoint"]; string cosmosDB_Database = configuration["CosmosDB_Database"]; string cosmosDB_Container = configuration["CosmosDB_Container"]; string cosmosDB_LeaseContainer = configuration["CosmosDB_LeaseContainer"]; string cosmosDB_KeyName = configuration["CosmosDB_KeyName"]; string keyVaultURL = configuration["KeyVault_URL"]; var kvClient = new SecretClient(new Uri(keyVaultURL), new DefaultAzureCredential()); string cosmosDB_Key = kvClient.GetSecret(cosmosDB_KeyName).Value.Value; CosmosSerializationOptions ignoreNullSerialization = new CosmosSerializationOptions { IgnoreNullValues = true }; CosmosClientOptions cosmosDB_Options = new CosmosClientOptions { SerializerOptions = ignoreNullSerialization }; _cosmosClient = new CosmosClient(cosmosDB_Endpoint, cosmosDB_Key, cosmosDB_Options); _container = _cosmosClient.GetContainer(cosmosDB_Database, cosmosDB_Container); _leaseContainer = _cosmosClient.GetContainer(cosmosDB_Database, cosmosDB_LeaseContainer); }
/// <summary> /// Converts a list of CosmosElements into a memory stream. /// </summary> /// <param name="cosmosElements">The cosmos elements</param> /// <param name="cosmosSerializationOptions">The custom serialization options. This allows custom serialization types like BSON, JSON, or other formats</param> /// <returns>Returns a memory stream of cosmos elements. By default the memory stream will contain JSON.</returns> internal static Stream ToStream( IEnumerable <CosmosElement> cosmosElements, CosmosSerializationOptions cosmosSerializationOptions = null) { IJsonWriter jsonWriter; if (cosmosSerializationOptions != null) { jsonWriter = cosmosSerializationOptions.CreateCustomWriterCallback(); } else { jsonWriter = JsonWriter.Create(JsonSerializationFormat.Text); } jsonWriter.WriteArrayStart(); foreach (CosmosElement cosmosElement in cosmosElements) { cosmosElement.WriteTo(jsonWriter); } jsonWriter.WriteArrayEnd(); return(new MemoryStream(jsonWriter.GetResult())); }
internal static SqlQuerySpec TranslateQuery( Expression inputExpression, CosmosSerializationOptions serializationOptions = null) { inputExpression = ConstantEvaluator.PartialEval(inputExpression); SqlQuery query = ExpressionToSql.TranslateQuery(inputExpression, serializationOptions); return(new SqlQuerySpec(query.ToString())); }
internal static string TranslateExpressionOld( Expression inputExpression, CosmosSerializationOptions serializationOptions = null) { TranslationContext context = new TranslationContext(serializationOptions); inputExpression = ConstantFolding.Fold(inputExpression); SqlScalarExpression scalarExpression = ExpressionToSql.VisitNonSubqueryScalarExpression(inputExpression, context); return(scalarExpression.ToString()); }
/// <summary> /// Create a serializer that uses the JSON.net serializer /// </summary> /// <remarks> /// This is internal to reduce exposure of JSON.net types so /// it is easier to convert to System.Text.Json /// </remarks> internal CosmosJsonDotNetSerializer(CosmosSerializationOptions cosmosSerializerOptions) { JsonSerializerSettings jsonSerializerSettings = new JsonSerializerSettings() { NullValueHandling = cosmosSerializerOptions.IgnoreNullValues ? NullValueHandling.Ignore : NullValueHandling.Include, Formatting = cosmosSerializerOptions.Indented ? Formatting.Indented : Formatting.None, ContractResolver = cosmosSerializerOptions.PropertyNamingPolicy == CosmosPropertyNamingPolicy.CamelCase ? new CamelCasePropertyNamesContractResolver() : null }; this.Serializer = JsonSerializer.Create(jsonSerializerSettings); }
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()); }
public async Task ItemQueryStreamSerializationSetting() { IList <ToDoActivity> deleteList = await this.CreateRandomItems(101, randomPartitionKey : true); CosmosSqlQueryDefinition sql = new CosmosSqlQueryDefinition("SELECT * FROM toDoActivity t ORDER BY t.taskNum"); CosmosSerializationOptions options = new CosmosSerializationOptions( ContentSerializationFormat.CosmosBinary.ToString(), (content) => JsonNavigator.Create(content), () => JsonWriter.Create(JsonSerializationFormat.Binary)); CosmosQueryRequestOptions requestOptions = new CosmosQueryRequestOptions() { CosmosSerializationOptions = options }; List <ToDoActivity> resultList = new List <ToDoActivity>(); double totalRequstCharge = 0; CosmosResultSetIterator setIterator = this.Container.Items.CreateItemQueryAsStream(sql, maxConcurrency: 5, maxItemCount: 5, requestOptions: requestOptions); while (setIterator.HasMoreResults) { using (CosmosQueryResponse iter = await setIterator.FetchNextSetAsync()) { Assert.IsTrue(iter.IsSuccess); Assert.IsNull(iter.ErrorMessage); Assert.IsTrue(iter.Count <= 5); totalRequstCharge += iter.RequestCharge; IJsonReader reader = JsonReader.Create(iter.Content); IJsonWriter textWriter = JsonWriter.Create(JsonSerializationFormat.Text); textWriter.WriteAll(reader); string json = Encoding.UTF8.GetString(textWriter.GetResult()); Assert.IsNotNull(json); ToDoActivity[] responseActivities = JsonConvert.DeserializeObject <ToDoActivity[]>(json); resultList.AddRange(responseActivities); } } Assert.AreEqual(deleteList.Count, resultList.Count); Assert.IsTrue(totalRequstCharge > 0); List <ToDoActivity> verifiedOrderBy = deleteList.OrderBy(x => x.taskNum).ToList(); for (int i = 0; i < verifiedOrderBy.Count(); i++) { Assert.AreEqual(verifiedOrderBy[i].taskNum, resultList[i].taskNum); Assert.AreEqual(verifiedOrderBy[i].id, resultList[i].id); } }
/// <summary> /// Converts a list of CosmosElements into a list of objects. /// </summary> /// <param name="cosmosElements">The cosmos elements</param> /// <param name="jsonSerializer">The JSON </param> /// <param name="cosmosSerializationOptions">The custom serialization options. This allows custom serialization types like BSON, JSON, or other formats</param> /// <returns>Returns a memory stream of cosmos elements. By default the memory stream will contain JSON.</returns> internal static IEnumerable <T> Deserialize <T>( IEnumerable <CosmosElement> cosmosElements, CosmosJsonSerializer jsonSerializer, CosmosSerializationOptions cosmosSerializationOptions = null) { if (!cosmosElements.Any()) { return(Enumerable.Empty <T>()); } Stream stream = CosmosElementSerializer.ToStream(cosmosElements, cosmosSerializationOptions); IEnumerable <T> typedResults = jsonSerializer.FromStream <List <T> >(stream); return(typedResults); }
private static CosmosClientProvider GetCosmosClient(Container container, bool bulkConfiguration) { var configuration = container.GetService <IConfiguration>(); var connectionString = configuration.GetSetting(Settings.MessagesDbConnectionString); var cosmosSerializationOptions = new CosmosSerializationOptions { PropertyNamingPolicy = CosmosPropertyNamingPolicy.CamelCase }; var cosmosClient = new CosmosClientBuilder(connectionString) .WithBulkExecution(bulkConfiguration) .WithSerializerOptions(cosmosSerializationOptions) .Build(); return(new CosmosClientProvider(cosmosClient)); }
private static async Task <Container> CosmosContainerFactory() { string connectionString = "AccountEndpoint=https://localhost:8081/;AccountKey=C2y6yDjf5/R+ob0N8A7Cgv30VRDJIWEHLM+4QDU5DE2nQ9nDuVTqobD4b8mGGyPMbIZnqyMsEcaGQy67XIw/Jw=="; var serializerOptions = new CosmosSerializationOptions { PropertyNamingPolicy = CosmosPropertyNamingPolicy.CamelCase }; CosmosClient client = new CosmosClientBuilder(connectionString).WithSerializerOptions(serializerOptions).Build(); await client.CreateDatabaseIfNotExistsAsync("UnitTesting"); Database database = client.GetDatabase("UnitTesting"); var containerProperties = new ContainerProperties(id: "Default", partitionKeyPath: "/id"); await database.CreateContainerIfNotExistsAsync(containerProperties); return(database.GetContainer("Default")); }
public CosmosLinqQuery( ContainerCore container, CosmosResponseFactory responseFactory, CosmosQueryClientCore queryClient, string continuationToken, QueryRequestOptions cosmosQueryRequestOptions, bool allowSynchronousQueryExecution, CosmosSerializationOptions serializationOptions = null) : this( container, responseFactory, queryClient, continuationToken, cosmosQueryRequestOptions, null, allowSynchronousQueryExecution, serializationOptions) { }
/// <summary> /// Create a serializer that uses the JSON.net serializer /// </summary> /// <remarks> /// This is public to reduce exposure of JSON.net types so /// it is easier to convert to System.Text.Json /// </remarks> public CosmosJsonDotNetSerializer(CosmosSerializationOptions cosmosSerializerOptions) { if (cosmosSerializerOptions == null) { SerializerSettings = null; return; } JsonSerializerSettings jsonSerializerSettings = new JsonSerializerSettings() { NullValueHandling = cosmosSerializerOptions.IgnoreNullValues ? NullValueHandling.Ignore : NullValueHandling.Include, Formatting = cosmosSerializerOptions.Indented ? Formatting.Indented : Formatting.None, ContractResolver = cosmosSerializerOptions.PropertyNamingPolicy == CosmosPropertyNamingPolicy.CamelCase ? new CamelCasePropertyNamesContractResolver() : null }; SerializerSettings = jsonSerializerSettings; }
public CosmosLinqQueryProvider( ContainerCore container, CosmosResponseFactory responseFactory, CosmosQueryClientCore queryClient, string continuationToken, QueryRequestOptions cosmosQueryRequestOptions, bool allowSynchronousQueryExecution, Action <IQueryable> onExecuteScalarQueryCallback = null, CosmosSerializationOptions serializationOptions = null) { this.container = container; this.responseFactory = responseFactory; this.queryClient = queryClient; this.continuationToken = continuationToken; this.cosmosQueryRequestOptions = cosmosQueryRequestOptions; this.allowSynchronousQueryExecution = allowSynchronousQueryExecution; this.onExecuteScalarQueryCallback = onExecuteScalarQueryCallback; this.serializationOptions = serializationOptions; }
private static SqlQuerySpec HandleMethodCallExpression( MethodCallExpression expression, CosmosSerializationOptions serializationOptions = null) { if (DocumentQueryEvaluator.IsTransformExpression(expression)) { if (string.Compare(expression.Method.Name, DocumentQueryEvaluator.SQLMethod, StringComparison.Ordinal) == 0) { return(DocumentQueryEvaluator.HandleAsSqlTransformExpression(expression)); } else { throw new DocumentQueryException( string.Format(CultureInfo.CurrentUICulture, ClientResources.BadQuery_InvalidExpression, expression.ToString())); } } return(SqlTranslator.TranslateQuery(expression, serializationOptions)); }
public void GetCosmosSerializerWithWrapperOrDefaultWithOptionsTest() { CosmosSerializationOptions serializerOptions = new CosmosSerializationOptions(); Assert.IsFalse(serializerOptions.IgnoreNullValues); Assert.IsFalse(serializerOptions.Indented); Assert.AreEqual(CosmosPropertyNamingPolicy.Default, serializerOptions.PropertyNamingPolicy); CosmosClientOptions options = new CosmosClientOptions() { SerializerOptions = new CosmosSerializationOptions() { IgnoreNullValues = true, Indented = true, PropertyNamingPolicy = CosmosPropertyNamingPolicy.CamelCase } }; CosmosSerializer cosmosSerializer = options.GetCosmosSerializerWithWrapperOrDefault(); Assert.AreNotEqual(cosmosSerializer, options.PropertiesSerializer, "Serializer should be custom not the default"); CosmosJsonSerializerWrapper cosmosJsonSerializerWrapper = cosmosSerializer as CosmosJsonSerializerWrapper; Assert.IsNotNull(cosmosJsonSerializerWrapper); // Verify the custom settings are being honored dynamic testItem = new { id = "testid", description = (string)null, CamelCaseProperty = "TestCamelCase" }; using (Stream stream = cosmosSerializer.ToStream <dynamic>(testItem)) { using (StreamReader sr = new StreamReader(stream)) { string jsonString = sr.ReadToEnd(); // Notice description is not included, camelCaseProperty starts lower case, the white space shows the indents string expectedJsonString = $"{{{Environment.NewLine} \"id\": \"testid\",{Environment.NewLine} \"camelCaseProperty\": \"TestCamelCase\"{Environment.NewLine}}}"; Assert.AreEqual(expectedJsonString, jsonString); } } }
/// <summary> /// Converts a list of CosmosElements into a list of objects. /// </summary> /// <param name="containerRid">Container Rid</param> /// <param name="cosmosElements">The cosmos elements</param> /// <param name="resourceType">The resource type</param> /// <param name="jsonSerializer">The JSON </param> /// <param name="cosmosSerializationOptions">The custom serialization options. This allows custom serialization types like BSON, JSON, or other formats</param> /// <returns>Returns a list of deserialized objects</returns> internal static IEnumerable <T> Deserialize <T>( string containerRid, IEnumerable <CosmosElement> cosmosElements, ResourceType resourceType, CosmosJsonSerializer jsonSerializer, CosmosSerializationOptions cosmosSerializationOptions = null) { if (!cosmosElements.Any()) { return(Enumerable.Empty <T>()); } Stream stream = CosmosElementSerializer.ToStream( containerRid, cosmosElements, resourceType, cosmosSerializationOptions); IEnumerable <T> typedResults = jsonSerializer.FromStream <CosmosFeedResponseUtil <T> >(stream).Data; return(typedResults); }
public static SqlQuerySpec Evaluate( Expression expression, CosmosSerializationOptions serializationOptions = null) { switch (expression.NodeType) { case ExpressionType.Constant: { return(DocumentQueryEvaluator.HandleEmptyQuery((ConstantExpression)expression)); } case ExpressionType.Call: { return(DocumentQueryEvaluator.HandleMethodCallExpression((MethodCallExpression)expression, serializationOptions)); } default: throw new DocumentQueryException( string.Format(CultureInfo.CurrentUICulture, ClientResources.BadQuery_InvalidExpression, expression.ToString())); } }
internal static SqlQuerySpec TranslateQuery( Expression inputExpression, CosmosSerializationOptions serializationOptions, IDictionary <object, string> parameters) { inputExpression = ConstantEvaluator.PartialEval(inputExpression); SqlQuery query = ExpressionToSql.TranslateQuery(inputExpression, parameters, serializationOptions); string queryText = null; SqlParameterCollection sqlParameters = new SqlParameterCollection(); if (parameters != null && parameters.Count > 0) { foreach (KeyValuePair <object, string> keyValuePair in parameters) { sqlParameters.Add(new Microsoft.Azure.Cosmos.SqlParameter(keyValuePair.Value, keyValuePair.Key)); } } queryText = query.ToString(); SqlQuerySpec sqlQuerySpec = new SqlQuerySpec(queryText, sqlParameters); return(sqlQuerySpec); }
public static string GetMemberName(this MemberInfo memberInfo, CosmosSerializationOptions cosmosSerializationOptions = null) { string memberName = null; // Json.Net honors JsonPropertyAttribute more than DataMemberAttribute // So we check for JsonPropertyAttribute first. JsonPropertyAttribute jsonPropertyAttribute = memberInfo.GetCustomAttribute <JsonPropertyAttribute>(true); if (jsonPropertyAttribute != null && !string.IsNullOrEmpty(jsonPropertyAttribute.PropertyName)) { memberName = jsonPropertyAttribute.PropertyName; } else { DataContractAttribute dataContractAttribute = memberInfo.DeclaringType.GetCustomAttribute <DataContractAttribute>(true); if (dataContractAttribute != null) { DataMemberAttribute dataMemberAttribute = memberInfo.GetCustomAttribute <DataMemberAttribute>(true); if (dataMemberAttribute != null && !string.IsNullOrEmpty(dataMemberAttribute.Name)) { memberName = dataMemberAttribute.Name; } } } if (memberName == null) { memberName = memberInfo.Name; } if (cosmosSerializationOptions != null) { memberName = CosmosSerializationUtil.GetStringWithPropertyNamingPolicy(cosmosSerializationOptions, memberName); } return(memberName); }
/// <summary> /// DI Configure using Autofac container /// </summary> /// <param name="builder"></param> private void ConfigureAutofacContainer(ContainerBuilder builder) { builder.Register(activator => { var xeroConfig = new XeroConfiguration(); var config = activator.Resolve <IConfiguration>(); config.GetSection(nameof(XeroConfiguration)).Bind(xeroConfig); return(xeroConfig); }) .AsSelf() .SingleInstance(); // register azure storage account builder.Register(activator => { var storageAccount = activator.Resolve <StorageAccountProvider>().GetHost(); return(storageAccount); }) .AsSelf() .SingleInstance(); // register Cosmos DB builder.Register(activator => { var config = activator.Resolve <IConfiguration>(); var connectionString = config["CosmosDB:ConnectionString"]; var serializerOptions = new CosmosSerializationOptions() { IgnoreNullValues = true, Indented = false, PropertyNamingPolicy = CosmosPropertyNamingPolicy.CamelCase }; var cosmosClientBuilder = new CosmosClientBuilder(connectionString) .WithSerializerOptions(serializerOptions); return(cosmosClientBuilder.Build()); }) .AsSelf() .SingleInstance(); // Register all functions that resides in a given namespace // The function class itself will be created using autofac builder .RegisterAssemblyTypes(typeof(Startup).Assembly) .InNamespace(typeof(IAzFunc).Namespace ?? string.Empty) .AsSelf() // Azure Functions core code resolves a function class by itself. .InstancePerTriggerRequest(); // This will scope nested dependencies to each function execution builder .RegisterAssemblyTypes(typeof(Startup).Assembly) .InNamespace(typeof(IStorageEnitity).Namespace ?? string.Empty) .AsImplementedInterfaces() .SingleInstance(); // This will scope nested dependencies to each function execution builder .RegisterAssemblyTypes(typeof(AccountingApi).Assembly) .InNamespace(typeof(AccountingApi).Namespace ?? string.Empty) .AsSelf() .SingleInstance(); builder.RegisterType <TokenTable>() .As <ITokenStore>() .SingleInstance(); }
public void VerifyCosmosConfigurationPropertiesGetUpdated() { string endpoint = AccountEndpoint; string key = MockCosmosUtil.RandomInvalidCorrectlyFormatedAuthKey; string region = Regions.WestCentralUS; ConnectionMode connectionMode = ConnectionMode.Gateway; TimeSpan requestTimeout = TimeSpan.FromDays(1); int maxConnections = 9001; string userAgentSuffix = "testSuffix"; RequestHandler preProcessHandler = new TestHandler(); ApiType apiType = ApiType.Sql; int maxRetryAttemptsOnThrottledRequests = 9999; TimeSpan maxRetryWaitTime = TimeSpan.FromHours(6); bool enableTcpConnectionEndpointRediscovery = true; CosmosSerializationOptions cosmosSerializerOptions = new CosmosSerializationOptions() { IgnoreNullValues = true, PropertyNamingPolicy = CosmosPropertyNamingPolicy.CamelCase, }; TimeSpan idleTcpConnectionTimeout = new TimeSpan(0, 10, 0); TimeSpan openTcpConnectionTimeout = new TimeSpan(0, 0, 5); int maxRequestsPerTcpConnection = 30; int maxTcpConnectionsPerEndpoint = 65535; Cosmos.PortReuseMode portReuseMode = Cosmos.PortReuseMode.PrivatePortPool; IWebProxy webProxy = new TestWebProxy(); Cosmos.ConsistencyLevel consistencyLevel = Cosmos.ConsistencyLevel.ConsistentPrefix; CosmosClientBuilder cosmosClientBuilder = new CosmosClientBuilder( accountEndpoint: endpoint, authKeyOrResourceToken: key); CosmosClient cosmosClient = cosmosClientBuilder.Build(new MockDocumentClient()); CosmosClientOptions clientOptions = cosmosClient.ClientOptions; Assert.AreEqual(endpoint, cosmosClient.Endpoint.OriginalString, "AccountEndpoint did not save correctly"); Assert.AreEqual(key, cosmosClient.AccountKey, "AccountKey did not save correctly"); //Verify the default values are different from the new values Assert.AreNotEqual(region, clientOptions.ApplicationRegion); Assert.IsNull(clientOptions.ApplicationPreferredRegions); Assert.AreNotEqual(connectionMode, clientOptions.ConnectionMode); Assert.AreNotEqual(maxConnections, clientOptions.GatewayModeMaxConnectionLimit); Assert.AreNotEqual(requestTimeout, clientOptions.RequestTimeout); Assert.AreNotEqual(userAgentSuffix, clientOptions.ApplicationName); Assert.AreNotEqual(apiType, clientOptions.ApiType); Assert.IsFalse(clientOptions.AllowBulkExecution); Assert.AreEqual(0, clientOptions.CustomHandlers.Count); Assert.IsNull(clientOptions.SerializerOptions); Assert.IsNotNull(clientOptions.Serializer); Assert.IsNull(clientOptions.WebProxy); Assert.IsFalse(clientOptions.LimitToEndpoint); Assert.IsTrue(clientOptions.EnableTcpConnectionEndpointRediscovery); Assert.IsNull(clientOptions.HttpClientFactory); Assert.AreNotEqual(consistencyLevel, clientOptions.ConsistencyLevel); Assert.IsFalse(clientOptions.EnablePartitionLevelFailover); //Verify GetConnectionPolicy returns the correct values for default ConnectionPolicy policy = clientOptions.GetConnectionPolicy(clientId: 0); Assert.AreEqual(ConnectionMode.Direct, policy.ConnectionMode); Assert.AreEqual(Protocol.Tcp, policy.ConnectionProtocol); Assert.AreEqual(clientOptions.GatewayModeMaxConnectionLimit, policy.MaxConnectionLimit); Assert.AreEqual(clientOptions.RequestTimeout, policy.RequestTimeout); Assert.IsNull(policy.IdleTcpConnectionTimeout); Assert.IsNull(policy.OpenTcpConnectionTimeout); Assert.IsNull(policy.MaxRequestsPerTcpConnection); Assert.IsNull(policy.MaxTcpConnectionsPerEndpoint); Assert.IsTrue(policy.EnableEndpointDiscovery); Assert.IsTrue(policy.EnableTcpConnectionEndpointRediscovery); Assert.IsNull(policy.HttpClientFactory); Assert.AreNotEqual(Cosmos.ConsistencyLevel.Session, clientOptions.ConsistencyLevel); Assert.IsFalse(policy.EnablePartitionLevelFailover); cosmosClientBuilder.WithApplicationRegion(region) .WithConnectionModeGateway(maxConnections, webProxy) .WithRequestTimeout(requestTimeout) .WithApplicationName(userAgentSuffix) .AddCustomHandlers(preProcessHandler) .WithApiType(apiType) .WithThrottlingRetryOptions(maxRetryWaitTime, maxRetryAttemptsOnThrottledRequests) .WithBulkExecution(true) .WithSerializerOptions(cosmosSerializerOptions) .WithConsistencyLevel(consistencyLevel) .WithPartitionLevelFailoverEnabled(); cosmosClient = cosmosClientBuilder.Build(new MockDocumentClient()); clientOptions = cosmosClient.ClientOptions; //Verify all the values are updated Assert.AreEqual(region, clientOptions.ApplicationRegion); Assert.IsNull(clientOptions.ApplicationPreferredRegions); Assert.AreEqual(connectionMode, clientOptions.ConnectionMode); Assert.AreEqual(maxConnections, clientOptions.GatewayModeMaxConnectionLimit); Assert.AreEqual(requestTimeout, clientOptions.RequestTimeout); Assert.AreEqual(userAgentSuffix, clientOptions.ApplicationName); Assert.AreEqual(preProcessHandler, clientOptions.CustomHandlers[0]); Assert.AreEqual(apiType, clientOptions.ApiType); Assert.AreEqual(maxRetryAttemptsOnThrottledRequests, clientOptions.MaxRetryAttemptsOnRateLimitedRequests); Assert.AreEqual(maxRetryWaitTime, clientOptions.MaxRetryWaitTimeOnRateLimitedRequests); Assert.AreEqual(cosmosSerializerOptions.IgnoreNullValues, clientOptions.SerializerOptions.IgnoreNullValues); Assert.AreEqual(cosmosSerializerOptions.PropertyNamingPolicy, clientOptions.SerializerOptions.PropertyNamingPolicy); Assert.AreEqual(cosmosSerializerOptions.Indented, clientOptions.SerializerOptions.Indented); Assert.IsTrue(object.ReferenceEquals(webProxy, clientOptions.WebProxy)); Assert.IsTrue(clientOptions.AllowBulkExecution); Assert.AreEqual(consistencyLevel, clientOptions.ConsistencyLevel); Assert.IsTrue(clientOptions.EnablePartitionLevelFailover); //Verify GetConnectionPolicy returns the correct values policy = clientOptions.GetConnectionPolicy(clientId: 0); Assert.AreEqual(region, policy.PreferredLocations[0]); Assert.AreEqual(ConnectionMode.Gateway, policy.ConnectionMode); Assert.AreEqual(Protocol.Https, policy.ConnectionProtocol); Assert.AreEqual(maxConnections, policy.MaxConnectionLimit); Assert.AreEqual(requestTimeout, policy.RequestTimeout); Assert.IsTrue(policy.UserAgentSuffix.Contains(userAgentSuffix)); Assert.IsTrue(policy.UseMultipleWriteLocations); Assert.AreEqual(maxRetryAttemptsOnThrottledRequests, policy.RetryOptions.MaxRetryAttemptsOnThrottledRequests); Assert.AreEqual((int)maxRetryWaitTime.TotalSeconds, policy.RetryOptions.MaxRetryWaitTimeInSeconds); Assert.AreEqual((Documents.ConsistencyLevel)consistencyLevel, clientOptions.GetDocumentsConsistencyLevel()); Assert.IsTrue(policy.EnablePartitionLevelFailover); IReadOnlyList <string> preferredLocations = new List <string>() { Regions.AustraliaCentral, Regions.AustraliaCentral2 }; //Verify Direct Mode settings cosmosClientBuilder = new CosmosClientBuilder( accountEndpoint: endpoint, authKeyOrResourceToken: key); cosmosClientBuilder.WithConnectionModeDirect( idleTcpConnectionTimeout, openTcpConnectionTimeout, maxRequestsPerTcpConnection, maxTcpConnectionsPerEndpoint, portReuseMode, enableTcpConnectionEndpointRediscovery) .WithApplicationPreferredRegions(preferredLocations); cosmosClient = cosmosClientBuilder.Build(new MockDocumentClient()); clientOptions = cosmosClient.ClientOptions; //Verify all the values are updated Assert.AreEqual(idleTcpConnectionTimeout, clientOptions.IdleTcpConnectionTimeout); Assert.AreEqual(openTcpConnectionTimeout, clientOptions.OpenTcpConnectionTimeout); Assert.AreEqual(maxRequestsPerTcpConnection, clientOptions.MaxRequestsPerTcpConnection); Assert.AreEqual(maxTcpConnectionsPerEndpoint, clientOptions.MaxTcpConnectionsPerEndpoint); Assert.AreEqual(portReuseMode, clientOptions.PortReuseMode); Assert.IsTrue(clientOptions.EnableTcpConnectionEndpointRediscovery); CollectionAssert.AreEqual(preferredLocations.ToArray(), clientOptions.ApplicationPreferredRegions.ToArray()); //Verify GetConnectionPolicy returns the correct values policy = clientOptions.GetConnectionPolicy(clientId: 0); Assert.AreEqual(idleTcpConnectionTimeout, policy.IdleTcpConnectionTimeout); Assert.AreEqual(openTcpConnectionTimeout, policy.OpenTcpConnectionTimeout); Assert.AreEqual(maxRequestsPerTcpConnection, policy.MaxRequestsPerTcpConnection); Assert.AreEqual(maxTcpConnectionsPerEndpoint, policy.MaxTcpConnectionsPerEndpoint); Assert.AreEqual(portReuseMode, policy.PortReuseMode); Assert.IsTrue(policy.EnableTcpConnectionEndpointRediscovery); CollectionAssert.AreEqual(preferredLocations.ToArray(), policy.PreferredLocations.ToArray()); }
/// <summary> /// Set a custom serializer option. /// </summary> /// <param name="cosmosSerializerOptions">The custom class that implements <see cref="CosmosSerializer"/> </param> /// <returns>The <see cref="CosmosClientBuilder"/> object</returns> /// <seealso cref="CosmosSerializer"/> /// <seealso cref="CosmosClientOptions.SerializerOptions"/> public CosmosClientBuilder WithSerializerOptions(CosmosSerializationOptions cosmosSerializerOptions) { this.clientOptions.SerializerOptions = cosmosSerializerOptions; return(this); }
public void VerifyCosmosConfigurationPropertiesGetUpdated() { string endpoint = AccountEndpoint; string key = Guid.NewGuid().ToString(); string region = Regions.WestCentralUS; ConnectionMode connectionMode = ConnectionMode.Gateway; TimeSpan requestTimeout = TimeSpan.FromDays(1); int maxConnections = 9001; string userAgentSuffix = "testSuffix"; RequestHandler preProcessHandler = new TestHandler(); ApiType apiType = ApiType.Sql; int maxRetryAttemptsOnThrottledRequests = 9999; TimeSpan maxRetryWaitTime = TimeSpan.FromHours(6); CosmosSerializationOptions cosmosSerializerOptions = new CosmosSerializationOptions() { IgnoreNullValues = true, PropertyNamingPolicy = CosmosPropertyNamingPolicy.CamelCase, }; TimeSpan idleTcpConnectionTimeout = new TimeSpan(0, 10, 0); TimeSpan openTcpConnectionTimeout = new TimeSpan(0, 0, 5); int maxRequestsPerTcpConnection = 30; int maxTcpConnectionsPerEndpoint = 65535; CosmosClientBuilder cosmosClientBuilder = new CosmosClientBuilder( accountEndpoint: endpoint, accountKey: key); CosmosClient cosmosClient = cosmosClientBuilder.Build(new MockDocumentClient()); CosmosClientOptions clientOptions = cosmosClient.ClientOptions; Assert.AreEqual(endpoint, cosmosClient.Endpoint.OriginalString, "AccountEndpoint did not save correctly"); Assert.AreEqual(key, cosmosClient.AccountKey, "AccountKey did not save correctly"); //Verify the default values are different from the new values Assert.AreNotEqual(region, clientOptions.ApplicationRegion); Assert.AreNotEqual(connectionMode, clientOptions.ConnectionMode); Assert.AreNotEqual(maxConnections, clientOptions.GatewayModeMaxConnectionLimit); Assert.AreNotEqual(requestTimeout, clientOptions.RequestTimeout); Assert.AreNotEqual(userAgentSuffix, clientOptions.ApplicationName); Assert.AreNotEqual(apiType, clientOptions.ApiType); Assert.AreEqual(0, clientOptions.CustomHandlers.Count); Assert.IsNull(clientOptions.SerializerOptions); Assert.IsNull(clientOptions.Serializer); //Verify GetConnectionPolicy returns the correct values for default ConnectionPolicy policy = clientOptions.GetConnectionPolicy(); Assert.AreEqual(ConnectionMode.Direct, policy.ConnectionMode); Assert.AreEqual(Protocol.Tcp, policy.ConnectionProtocol); Assert.AreEqual(clientOptions.GatewayModeMaxConnectionLimit, policy.MaxConnectionLimit); Assert.AreEqual(clientOptions.RequestTimeout, policy.RequestTimeout); Assert.IsNull(policy.IdleTcpConnectionTimeout); Assert.IsNull(policy.OpenTcpConnectionTimeout); Assert.IsNull(policy.MaxRequestsPerTcpConnection); Assert.IsNull(policy.MaxTcpConnectionsPerEndpoint); cosmosClientBuilder.WithApplicationRegion(region) .WithConnectionModeGateway(maxConnections) .WithRequestTimeout(requestTimeout) .WithApplicationName(userAgentSuffix) .AddCustomHandlers(preProcessHandler) .WithApiType(apiType) .WithThrottlingRetryOptions(maxRetryWaitTime, maxRetryAttemptsOnThrottledRequests) .WithSerializerOptions(cosmosSerializerOptions); cosmosClient = cosmosClientBuilder.Build(new MockDocumentClient()); clientOptions = cosmosClient.ClientOptions; //Verify all the values are updated Assert.AreEqual(region, clientOptions.ApplicationRegion); Assert.AreEqual(connectionMode, clientOptions.ConnectionMode); Assert.AreEqual(maxConnections, clientOptions.GatewayModeMaxConnectionLimit); Assert.AreEqual(requestTimeout, clientOptions.RequestTimeout); Assert.AreEqual(userAgentSuffix, clientOptions.ApplicationName); Assert.AreEqual(preProcessHandler, clientOptions.CustomHandlers[0]); Assert.AreEqual(apiType, clientOptions.ApiType); Assert.AreEqual(maxRetryAttemptsOnThrottledRequests, clientOptions.MaxRetryAttemptsOnRateLimitedRequests); Assert.AreEqual(maxRetryWaitTime, clientOptions.MaxRetryWaitTimeOnRateLimitedRequests); Assert.AreEqual(cosmosSerializerOptions.IgnoreNullValues, clientOptions.SerializerOptions.IgnoreNullValues); Assert.AreEqual(cosmosSerializerOptions.PropertyNamingPolicy, clientOptions.SerializerOptions.PropertyNamingPolicy); Assert.AreEqual(cosmosSerializerOptions.Indented, clientOptions.SerializerOptions.Indented); //Verify GetConnectionPolicy returns the correct values policy = clientOptions.GetConnectionPolicy(); Assert.AreEqual(region, policy.PreferredLocations[0]); Assert.AreEqual(ConnectionMode.Gateway, policy.ConnectionMode); Assert.AreEqual(Protocol.Https, policy.ConnectionProtocol); Assert.AreEqual(maxConnections, policy.MaxConnectionLimit); Assert.AreEqual(requestTimeout, policy.RequestTimeout); Assert.IsTrue(policy.UserAgentSuffix.Contains(userAgentSuffix)); Assert.IsTrue(policy.UseMultipleWriteLocations); Assert.AreEqual(maxRetryAttemptsOnThrottledRequests, policy.RetryOptions.MaxRetryAttemptsOnThrottledRequests); Assert.AreEqual((int)maxRetryWaitTime.TotalSeconds, policy.RetryOptions.MaxRetryWaitTimeInSeconds); //Verify Direct Mode settings cosmosClientBuilder = new CosmosClientBuilder( accountEndpoint: endpoint, accountKey: key); cosmosClientBuilder.WithConnectionModeDirect( idleTcpConnectionTimeout, openTcpConnectionTimeout, maxRequestsPerTcpConnection, maxTcpConnectionsPerEndpoint ); cosmosClient = cosmosClientBuilder.Build(new MockDocumentClient()); clientOptions = cosmosClient.ClientOptions; //Verify all the values are updated Assert.AreEqual(idleTcpConnectionTimeout, clientOptions.IdleTcpConnectionTimeout); Assert.AreEqual(openTcpConnectionTimeout, clientOptions.OpenTcpConnectionTimeout); Assert.AreEqual(maxRequestsPerTcpConnection, clientOptions.MaxRequestsPerTcpConnection); Assert.AreEqual(maxTcpConnectionsPerEndpoint, clientOptions.MaxTcpConnectionsPerEndpoint); //Verify GetConnectionPolicy returns the correct values policy = clientOptions.GetConnectionPolicy(); Assert.AreEqual(idleTcpConnectionTimeout, policy.IdleTcpConnectionTimeout); Assert.AreEqual(openTcpConnectionTimeout, policy.OpenTcpConnectionTimeout); Assert.AreEqual(maxRequestsPerTcpConnection, policy.MaxRequestsPerTcpConnection); Assert.AreEqual(maxTcpConnectionsPerEndpoint, policy.MaxTcpConnectionsPerEndpoint); }
/// <summary> /// Converts a list of CosmosElements into a memory stream. /// </summary> /// <param name="containerRid">Container Rid</param> /// <param name="cosmosElements">The cosmos elements</param> /// <param name="resourceType">The resource type</param> /// <param name="cosmosSerializationOptions">The custom serialization options. This allows custom serialization types like BSON, JSON, or other formats</param> /// <returns>Returns a memory stream of cosmos elements. By default the memory stream will contain JSON.</returns> internal static Stream ToStream( string containerRid, IEnumerable <CosmosElement> cosmosElements, ResourceType resourceType, CosmosSerializationOptions cosmosSerializationOptions = null) { IJsonWriter jsonWriter; if (cosmosSerializationOptions != null) { jsonWriter = cosmosSerializationOptions.CreateCustomWriterCallback(); } else { jsonWriter = JsonWriter.Create(JsonSerializationFormat.Text); } // The stream contract should return the same contract as read feed. // { // "_rid": "qHVdAImeKAQ=", // "Documents": [{ // "id": "03230", // "_rid": "qHVdAImeKAQBAAAAAAAAAA==", // "_self": "dbs\/qHVdAA==\/colls\/qHVdAImeKAQ=\/docs\/qHVdAImeKAQBAAAAAAAAAA==\/", // "_etag": "\"410000b0-0000-0000-0000-597916b00000\"", // "_attachments": "attachments\/", // "_ts": 1501107886 // }], // "_count": 1 // } jsonWriter.WriteObjectStart(); // Write the rid field and value jsonWriter.WriteFieldName("_rid"); jsonWriter.WriteStringValue(containerRid); // Write the array of elements string rootName = CosmosElementSerializer.GetRootNodeName(resourceType); jsonWriter.WriteFieldName(rootName); int count = 0; jsonWriter.WriteArrayStart(); foreach (CosmosElement element in cosmosElements) { count++; element.WriteTo(jsonWriter); } jsonWriter.WriteArrayEnd(); // Write the count field and value jsonWriter.WriteFieldName("_count"); jsonWriter.WriteNumberValue(count); jsonWriter.WriteObjectEnd(); return(new MemoryStream(jsonWriter.GetResult())); }
/// <summary> /// Converts a list of CosmosElements into a memory stream. /// </summary> /// <param name="memoryStream">The memory stream response from Azure Cosmos</param> /// <param name="resourceType">The resource type</param> /// <param name="cosmosSerializationOptions">The custom serialization options. This allows custom serialization types like BSON, JSON, or other formats</param> /// <returns>Returns a memory stream of cosmos elements. By default the memory stream will contain JSON.</returns> internal static CosmosArray ToCosmosElements( MemoryStream memoryStream, ResourceType resourceType, CosmosSerializationOptions cosmosSerializationOptions = null) { if (!memoryStream.CanRead) { throw new InvalidDataException("Stream can not be read"); } // Execute the callback an each element of the page // For example just could get a response like this // { // "_rid": "qHVdAImeKAQ=", // "Documents": [{ // "id": "03230", // "_rid": "qHVdAImeKAQBAAAAAAAAAA==", // "_self": "dbs\/qHVdAA==\/colls\/qHVdAImeKAQ=\/docs\/qHVdAImeKAQBAAAAAAAAAA==\/", // "_etag": "\"410000b0-0000-0000-0000-597916b00000\"", // "_attachments": "attachments\/", // "_ts": 1501107886 // }], // "_count": 1 // } // And you should execute the callback on each document in "Documents". long responseLengthBytes = memoryStream.Length; byte[] content = memoryStream.ToArray(); IJsonNavigator jsonNavigator = null; // Use the users custom navigator if (cosmosSerializationOptions != null) { jsonNavigator = cosmosSerializationOptions.CreateCustomNavigatorCallback(content); if (jsonNavigator == null) { throw new InvalidOperationException("The CosmosSerializationOptions did not return a JSON navigator."); } } else { jsonNavigator = JsonNavigator.Create(content); } string resourceName = CosmosElementSerializer.GetRootNodeName(resourceType); if (!jsonNavigator.TryGetObjectProperty( jsonNavigator.GetRootNode(), resourceName, out ObjectProperty objectProperty)) { throw new InvalidOperationException($"Response Body Contract was violated. QueryResponse did not have property: {resourceName}"); } IJsonNavigatorNode cosmosElements = objectProperty.ValueNode; if (!(CosmosElement.Dispatch( jsonNavigator, cosmosElements) is CosmosArray cosmosArray)) { throw new InvalidOperationException($"QueryResponse did not have an array of : {resourceName}"); } return(cosmosArray); }
public TranslationContext(CosmosSerializationOptions serializationOptions) : this() { this.serializationOptions = serializationOptions; }
public TranslationContext(CosmosSerializationOptions serializationOptions, IDictionary <object, string> parameters = null) : this() { this.serializationOptions = serializationOptions; this.parameters = parameters; }