This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
private class CosmosDBConnectionString | |
{ | |
public CosmosDBConnectionString(string connectionString) | |
{ | |
// Use this generic builder to parse the connection string | |
DbConnectionStringBuilder builder = new DbConnectionStringBuilder | |
{ | |
ConnectionString = connectionString | |
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
[FunctionName("Trigger")] | |
public static void Trigger([CosmosDBTrigger( | |
databaseName: "%MonitoredDatabase%", | |
collectionName: "%MonitoredCollection%", | |
ConnectionStringSetting = "CosmosDB", | |
LeaseCollectionPrefix = "%MonitoredDatabaseLeasePrefix%", | |
LeaseCollectionName = "leases", | |
CreateLeaseCollectionIfNotExists = true)] IReadOnlyList<Document> documents, | |
ILogger log) | |
{ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// To view the default settings, hold "alt" while clicking on the "Settings" button. | |
// For documentation on these settings, see: https://aka.ms/terminal-documentation | |
{ | |
"$schema": "https://aka.ms/terminal-profiles-schema", | |
"defaultProfile": "{61c54bbd-c2c6-5271-96e7-009a87ff44bf}", | |
"profiles": |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
CosmosClientOptions options = new CosmosClientOptions() { | |
AllowBulkExecution = true }; | |
CosmosClient cosmosclient = new CosmosClient(connectionString, options); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Container container = cosmosClient.GetContainer("myDb", "myCollection"); | |
// Assuming your have your data available to be inserted or read | |
List<Task> concurrentTasks = new List<Task>(); | |
foreach(Item itemToInsert in ReadYourData()) | |
{ | |
concurrentTasks.Add(container.CreateItemAsync(itemToInsert, new PartitionKey(itemToInsert.MyPk))); | |
} | |
await Task.WhenAll(concurrentTasks); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
string partitionKey = "The Family"; | |
ParentClass parent = new ParentClass(){ Id = "The Parent", PartitionKey = partitionKey, Name = "John", Age = 30 }; | |
ChildClass child = new ChildClass(){ Id = "The Child", ParentId = parent.Id, PartitionKey = partitionKey }; | |
TransactionalBatch batch = container.CreateTransactionalBatch(new PartitionKey(parent.PartitionKey)) | |
.CreateItem<ParentClass>(parent) | |
.CreateItem<ChildClass>(child); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
TransactionalBatchResponse batchResponse = await batch.ExecuteAsync(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using (batchResponse) | |
{ | |
if (batchResponse.IsSuccessStatusCode) | |
{ | |
TransactionalBatchOperationResult<ParentClass> parentResult = batchResponse.GetOperationResultAtIndex<ParentClass>(0); | |
ParentClass parentClassResult = parentResult.Resource; | |
TransactionalBatchOperationResult<ChildClass> childResult = batchResponse.GetOperationResultAtIndex<ChildClass>(1); | |
ChildClass childClassResult = childResult.Resource; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Parent's birthday! | |
parent.Age = 31; | |
// Naming two childs with the same name, should abort the transaction | |
ChildClass anotherChild = new ChildClass(){ Id = "The Child", ParentId = parent.Id, PartitionKey = partitionKey }; | |
TransactionalBatchResponse failedBatchResponse = await container.CreateTransactionalBatch(new PartitionKey(partitionKey)) | |
.ReplaceItem<ParentClass>(parent.Id, parent) | |
.CreateItem<ChildClass>(anotherChild) | |
.ExecuteAsync(); | |
using (failedBatchResponse) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public void ConfigureServices(IServiceCollection services) | |
{ | |
// other services | |
services.AddSingleton((s) => | |
{ | |
return new CosmosClient("<connection-string>"); | |
}); | |
} |