Exemplo n.º 1
0
        private async Task StoreConfigRecordAsync(
            IDocumentClient client,
            DocumentDbConfig dbConfig,
            ServiceConfigRecord newRecord,
            Document existingDocument)
        {
            var collectionUri = GetDocumentCollectionUri(dbConfig);

            if (existingDocument == null)
            {
                // No record exists yet. Use the creation method to ensure failure if another process has modified the
                // record that a failure is produced.
                await client.CreateDocumentAsync(collectionUri, newRecord);
            }
            else
            {
                // Add IfMatch header to ensure the record has not been changed by another process.
                RequestOptions options = new RequestOptions
                {
                    AccessCondition = new AccessCondition
                    {
                        Condition = existingDocument.ETag,
                        Type      = AccessConditionType.IfMatch
                    }
                };

                await client.UpsertDocumentAsync(collectionUri, newRecord, options);
            }
        }
Exemplo n.º 2
0
        private async Task EnsureServiceConfigAsync(
            IDocumentClient client,
            DocumentDbConfig dbConfig,
            string configSignature,
            List <DocumentStoreConfig> configs)
        {
            var existingDocument = await GetConfigRecordAsync(client, dbConfig);

            if (existingDocument != null)
            {
                // Config already exists.
                ServiceConfigRecord existingConfigRecord = (dynamic)existingDocument;

                if (existingConfigRecord.Signature == configSignature)
                {
                    // No changes to the config. Nothing to do.
                    return;
                }

                // Config has been updated. Perform an update.
                await UpdateCollectionConfigAsync(client, dbConfig, configs);
            }

            // There has been a config change. Update the config record.

            var configRecordId = GetConfigRecordId();

            var configRecord = new ServiceConfigRecord
            {
                Id           = configRecordId,
                Signature    = configSignature,
                PartitionKey = configRecordId
            };

            // Update the store config record.
            await StoreConfigRecordAsync(client, dbConfig, configRecord, existingDocument);
        }