public static IServiceCollection AddCosmosDb(this IServiceCollection collection, string endpointUrl,
                                                     string primaryKey,
                                                     string databaseName, List <ContainerInfo> containers)
        {
            var cosmosClient             = new CosmosClient(endpointUrl, primaryKey);
            var cosmosDbContainerFactory = new CosmosDbContainerFactory(cosmosClient, databaseName, containers);

            collection.AddSingleton <ICosmosDbContainerFactory>(cosmosDbContainerFactory);
            return(collection);
        }
示例#2
0
        /// <summary>
        ///     Register a singleton instance of Cosmos Db Container Factory, which is a wrapper for the CosmosClient.
        /// </summary>
        /// <param name="services"></param>
        /// <param name="endpointUrl"></param>
        /// <param name="primaryKey"></param>
        /// <param name="databaseName"></param>
        /// <param name="containers"></param>
        /// <returns></returns>
        public static IServiceCollection AddCosmosDb(this IServiceCollection services,
                                                     string endpointUrl,
                                                     string primaryKey,
                                                     string databaseName,
                                                     List <ContainerInfo> containers)
        {
            Microsoft.Azure.Cosmos.CosmosClient client = new Microsoft.Azure.Cosmos.CosmosClient(endpointUrl, primaryKey);
            var cosmosDbClientFactory = new CosmosDbContainerFactory(client, databaseName, containers);

            services.AddSingleton <ICosmosDbContainerFactory>(cosmosDbClientFactory);

            return(services);
        }
        /// <summary>
        ///     Register a singleton instance of Cosmos Db Container Factory, which is a wrapper for the CosmosClient.
        /// </summary>
        /// <param name="services"></param>
        /// <param name="endpointUrl"></param>
        /// <param name="primaryKey"></param>
        /// <param name="databaseName"></param>
        /// <param name="containers"></param>
        /// <returns></returns>
        public static IServiceCollection AddCosmosDb(this IServiceCollection services,
                                                     string endpointUrl,
                                                     string primaryKey,
                                                     string databaseName,
                                                     List <ContainerInfo> containers)
        {
            Microsoft.Azure.Cosmos.CosmosClient client = new Microsoft.Azure.Cosmos.CosmosClient(endpointUrl, primaryKey);
            CosmosDbContainerFactory            cosmosDbClientFactory = new CosmosDbContainerFactory(client, databaseName, containers);

            // Microsoft recommends a singleton client instance to be used throughout the application
            // https://docs.microsoft.com/en-us/dotnet/api/microsoft.azure.cosmos.cosmosclient?view=azure-dotnet#definition
            // "CosmosClient is thread-safe. Its recommended to maintain a single instance of CosmosClient per lifetime of the application which enables efficient connection management and performance"
            services.AddSingleton <ICosmosDbContainerFactory>(cosmosDbClientFactory);

            return(services);
        }
示例#4
0
        /// <summary>
        ///     Register a singleton instance of Cosmos Db Container Factory, which is a wrapper for the CosmosClient.
        /// </summary>
        /// <param name="services"></param>
        /// <param name="endpointUrl"></param>
        /// <param name="primaryKey"></param>
        /// <param name="databaseName"></param>
        /// <param name="containers"></param>
        /// <returns></returns>
        public static IServiceCollection AddCosmosDb(this IServiceCollection services,
                                                     string endpointUrl,
                                                     string primaryKey,
                                                     string databaseName,
                                                     List <ContainerInfo> containers)
        {
            Microsoft.Azure.Cosmos.CosmosClient client = new Microsoft.Azure.Cosmos.CosmosClient(endpointUrl, primaryKey);
            var cosmosDbClientFactory = new CosmosDbContainerFactory(client, databaseName, containers);

            // This will be done at the API level so we ONLY ensure db creation in development environment
            // cosmosDbClientFactory.EnsureDbSetupAsync().Wait();

            services.AddSingleton <ICosmosDbContainerFactory>(cosmosDbClientFactory);

            return(services);
        }
示例#5
0
        public static IServiceCollection AddSpendOps(this IServiceCollection services)
        {
            services.TryAddSingleton <ICosmosDbContainerFactory>(provider =>
            {
                var factory = new CosmosDbContainerFactory
                {
                    Provider = (creatorType, containerDefinition, cosmosDbClient) =>
                    {
                        var chargeTracker            = cosmosDbClient.ServiceProvider.GetService <IChargeTracker <CosmosDbChargedResponse> >();
                        ICosmosDbContainer container = cosmosDbClient.GetContainer(containerDefinition.ContainerId)
                                                       .ConfigureAwait(false).GetAwaiter().GetResult();
                        return(new TrackedCosmosDbContainer(containerDefinition, container, chargeTracker, creatorType.Name));
                    }
                };

                return(factory);
            });

            return(services);
        }