public DocumentRegistry()
        {
            var connectionString = ConfigurationHelper.GetConnectionString("CosmosDbConnectionString");

            if (string.IsNullOrEmpty(connectionString))
            {
                throw new InvalidOperationException("No 'DocumentConnectionString' connection string found.");
            }
            var documentConnectionString = new DocumentSessionConnectionString {
                ConnectionString = connectionString
            };

            ValidateConnectionStringSetting(documentConnectionString.Database, "Database");
            ValidateConnectionStringSetting(documentConnectionString.AccountEndpoint, "AccountEndpoint");
            ValidateConnectionStringSetting(documentConnectionString.Collection, "Collection");
            ValidateConnectionStringSetting(documentConnectionString.ThroughputOffer, "ThroughputOffer");
            ForSingletonOf <DocumentSessionConnectionString>().Use(documentConnectionString);

            var client = new DocumentClient(new Uri(documentConnectionString.AccountEndpoint), documentConnectionString.AccountKey);

            client.CreateDatabaseIfNotExistsAsync(new Database {
                Id = documentConnectionString.Database
            }).Wait();
            ForSingletonOf <IDocumentClient>().Use(client);
            client.CreateDocumentCollectionIfNotExistsAsync(
                UriFactory.CreateDatabaseUri(documentConnectionString.Database), new DocumentCollection
            {
                Id = documentConnectionString.Collection
            },
                new RequestOptions {
                OfferThroughput = int.Parse(documentConnectionString.ThroughputOffer)
            }).Wait();
        }
        protected IDocumentSession CreateDocumentSession()
        {
            var connectionString = ConfigurationHelper.GetConnectionString("CosmosDbConnectionString");

            if (string.IsNullOrEmpty(connectionString))
            {
                throw new InvalidOperationException("No 'DocumentConnectionString' connection string found.");
            }
            var documentConnectionString = new DocumentSessionConnectionString {
                ConnectionString = connectionString
            };

            var client = new DocumentClient(new Uri(documentConnectionString.AccountEndpoint), documentConnectionString.AccountKey);

            client.CreateDatabaseIfNotExistsAsync(new Microsoft.Azure.Documents.Database {
                Id = documentConnectionString.Database
            }).Wait();

            client.CreateDocumentCollectionIfNotExistsAsync(
                UriFactory.CreateDatabaseUri(documentConnectionString.Database), new DocumentCollection
            {
                Id = documentConnectionString.Collection
            },
                new RequestOptions {
                OfferThroughput = int.Parse(documentConnectionString.ThroughputOffer)
            }).Wait();
            return(new DocumentSession(client, documentConnectionString));
        }
Ejemplo n.º 3
0
 public DocumentSession(IDocumentClient client, DocumentSessionConnectionString connectionString)
 {
     _client             = client ?? throw new ArgumentNullException(nameof(client));
     _databaseId         = connectionString.Database;
     _documentCollection = client
                           .ReadDocumentCollectionAsync(UriFactory.CreateDocumentCollectionUri(connectionString.Database, connectionString.Collection))
                           .Result.Resource;
 }
        public async Task Run(Func <DocumentClient, DocumentCollection, Task> func)
        {
            var connectionString = ConfigurationManager.ConnectionStrings["CosmosDbConnectionString"]?.ConnectionString;

            if (string.IsNullOrEmpty(connectionString))
            {
                throw new InvalidOperationException("No 'DocumentConnectionString' connection string found.");
            }
            var documentConnectionString = new DocumentSessionConnectionString {
                ConnectionString = connectionString
            };


            using (var client = new DocumentClient(new Uri(documentConnectionString.AccountEndpoint), documentConnectionString.AccountKey))
            {
                var documentCollection = client
                                         .ReadDocumentCollectionAsync(UriFactory.CreateDocumentCollectionUri(documentConnectionString.Database, documentConnectionString.Collection))
                                         .Result.Resource;
                await func(client, documentCollection);
            }
        }