Пример #1
0
        private async Task Start()
        {
            this.client = new DocumentClient(new Uri(EndpointUri), PrimaryKey);

            await this.client.CreateDatabaseIfNotExistsAsync(new Database { Id = "Strength" });

            await this.client.CreateDocumentCollectionIfNotExistsAsync(UriFactory.CreateDatabaseUri("Strength"),
                                                                       new DocumentCollection { Id = "Values" });


            Random random = new Random();

            while (true)
            {
                string    id        = RandomString(10);
                int       strength  = random.Next(-90, -10);
                Radiation radiation = new Radiation {
                    Id = id, Strength = strength
                };
                await CreateFamilyDocumentIfNotExists("Strength", "Values", radiation);

                Console.WriteLine("Document Created with id: " + id);
                await Task.Delay(2000);
            }
        }
Пример #2
0
        private async void UpdateDocument(String databaseName, string collectionName, string id, Radiation updatedRadiation)
        {
            await this.client.ReplaceDocumentAsync(UriFactory.CreateDocumentUri(databaseName, collectionName, id), updatedRadiation);

            Console.WriteLine("Document updated: ", updatedRadiation);
        }
Пример #3
0
        private async Task CreateFamilyDocumentIfNotExists(string databaseName, string collectionName, Radiation radiation)
        {
            try
            {
                await this.client.ReadDocumentAsync(UriFactory.CreateDocumentUri(databaseName, collectionName, radiation.Id));

                Console.WriteLine("Found {0}", radiation.Id);
            }
            catch (DocumentClientException de)
            {
                if (de.StatusCode == HttpStatusCode.NotFound)
                {
                    await this.client.CreateDocumentAsync(UriFactory.CreateDocumentCollectionUri(databaseName, collectionName), radiation);

                    Console.WriteLine("Created Radiation {0}", radiation.Id);
                }
                else
                {
                    throw;
                }
            }
        }