libraries/Microsoft.Bot.Builder.Azure/CosmosDbPartitionedStorage.cs
/// <summary>
/// Inserts or updates one or more items into the Cosmos DB container. 
/// </summary>
/// <param name="changes">A dictionary of items to be inserted or updated. The dictionary item key
/// is used as the ID for the inserted / updated item.</param>
/// <param name="cancellationToken">A <see cref="CancellationToken"/>.</param>
/// <returns>A Task representing the work to be executed.</returns>
/// <exception cref="ArgumentNullException">Exception thrown if the changes dictionary is null.</exception>
/// <exception cref="Exception">Exception thrown is the etag is empty on any of the items within the changes dictionary.</exception>
Inserts or updates one or more items into the Cosmos DB container.
Inserts or updates one or more items into the Cosmos DB container .
public async Task WriteAsync(IDictionary<string, object> changes, CancellationToken cancellationToken = default(CancellationToken))
        {
            if (changes == null)
            {
                throw new ArgumentNullException(nameof(changes));
            }
            if (changes.Count == 0)
            {
                return;
            }
            await InitializeAsync().ConfigureAwait(false);
            foreach (var change in changes)
            {
                var json = JObject.FromObject(change.Value, _jsonSerializer);
                json.Remove("eTag");
                var documentChange = new DocumentStoreItem
                {
                    Id = CosmosDbKeyEscape.EscapeKey(change.Key, _cosmosDbStorageOptions.KeySuffix, _cosmosDbStorageOptions.CompatibilityMode),
                    RealId = change.Key,
                    Document = json,
                };
                var etag = (change.Value as IStoreItem)?.ETag;
                if (etag == null || etag == "*")
                {
                    await _container.UpsertItemAsync(
                            documentChange,
                            GetPartitionKey(documentChange.PartitionKey),
                            cancellationToken: cancellationToken)
                        .ConfigureAwait(false);
                }
                else if (etag.Length > 0)
                {
                    await _container.UpsertItemAsync(
                            documentChange,
                            GetPartitionKey(documentChange.PartitionKey),
                            new ItemRequestOptions() { IfMatchEtag = etag, },
                            cancellationToken)
                        .ConfigureAwait(false);
                }
                else
                {
                    throw new ArgumentException("etag empty");
                }
            }
        }
public async Task WriteAsync ( IDictionary < string , object > changes , CancellationToken cancellationToken = default ( CancellationToken ) ) { if ( changes == null ) { throw new ArgumentNullException ( nameof ( changes ) ) ; } if ( changes . Count == 0 ) { return ; } await InitializeAsync ( ) . ConfigureAwait ( false ) ; foreach ( var change in changes ) { var json = JObject . FromObject ( change . Value , _jsonSerializer ) ; json . Remove ( " " ) ; var documentChange = new DocumentStoreItem { Id = CosmosDbKeyEscape . EscapeKey ( change . Key , _cosmosDbStorageOptions . KeySuffix , _cosmosDbStorageOptions . CompatibilityMode ) , RealId = change . Key , Document = json , } ; var etag = ( change . Value as IStoreItem ) ? . ETag ; if ( etag == null || etag == " " ) { await _container . UpsertItemAsync ( documentChange , GetPartitionKey ( documentChange . PartitionKey ) , cancellationToken : cancellationToken ) . ConfigureAwait ( false ) ; } else if ( etag . Length > 0 ) { await _container . UpsertItemAsync ( documentChange , GetPartitionKey ( documentChange . PartitionKey ) , new ItemRequestOptions ( ) { IfMatchEtag = etag , } , cancellationToken ) . ConfigureAwait ( false ) ; } else { throw new ArgumentException ( " " ) ; } } }
Inserts or updates one or more items into the Cosmos DB container.
Inserts or updates one or more items into the Cosmos DB container .
// Ensure Initialization has been run // Remove etag from JSON object that was copied from IStoreItem. // The ETag information is updated as an _etag attribute in the document metadata. // if new item or * then insert or replace unconditionally // if we have an etag, do opt. concurrency replace
A Task representing the work to be executed.
A Task representing the work to be executed .
Exception thrown if the changes dictionary is null.
Exception thrown if the changes dictionary is null .
Exception thrown is the etag is empty on any of the items within the changes dictionary.
Exception thrown is the etag is empty on any of the items within the changes dictionary .
changes
IDictionary<string, object>
A dictionary of items to be inserted or updated. The dictionary item key
is used as the ID for the inserted / updated item.
A dictionary of items to be inserted or updated . The dictionary item key is used as the ID for the inserted / updated item .
cancellationToken
CancellationToken
None