This file contains hidden or 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
CosmosAsyncClient client = new CosmosClientBuilder() | |
.endpoint("account endpoint") | |
.key("account key") | |
.consistencyLevel(ConsistencyLevel.EVENTUAL) | |
.contentResponseOnWriteEnabled(false) | |
.buildAsyncClient(); |
This file contains hidden or 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
ToDoActivity item = new ToDoActivity() { ...}; | |
ItemResponse<ToDoActivity> itemResponse = await this.container.CreateItemAsync( | |
item, | |
requestOptions: new ItemRequestOptions() { EnableContentResponseOnWrite = false }); | |
ToDoActivity responseContent = itemResponse.Resource; // This is really the same as "item" | |
double consumedRUs = itemResponse.Headers.RequestCharge; | |
string etag = itemResponse.Headers.ETag; | |
string session = itemResponse.Headers.Session; |
This file contains hidden or 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
ItemResponse<ToDoActivity> readItemResponse = await this.container.ReadItemAsync( | |
id: "<id>", | |
new PartitionKey("<partitionKeyValue>")); | |
ToDoActivity someItem = readItemResponse.Resource; | |
// Apply some modification | |
someItem.value = 5; | |
ItemResponse<ToDoActivity> itemResponse = await this.container.ReplaceItemAsync<ToDoActivity>( | |
id: "<id>", |
This file contains hidden or 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
ItemResponse<ToDoActivity> readItemResponse = await this.container.ReadItemAsync( | |
id: "<id>", | |
new PartitionKey("<partitionKeyValue>")); | |
ToDoActivity someItem = readItemResponse.Resource; | |
// Apply some modification | |
someItem.value = 5; | |
ItemResponse<ToDoActivity> itemResponse = await this.container.ReplaceItemAsync<ToDoActivity>( | |
id: "<id>", |
This file contains hidden or 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
ToDoActivity item = new ToDoActivity(){...}; | |
ItemResponse<ToDoActivity> itemResponse = await this.container.CreateItemAsync(item); | |
ToDoActivity responseContent = itemResponse.Resource; // This is really the same as "item" | |
double consumedRUs = itemResponse.Headers.RequestCharge; | |
string etag = itemResponse.Headers.ETag; | |
string session = itemResponse.Headers.Session; |
This file contains hidden or 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) | |
{ | |
services.AddHttpClient(); | |
services.AddSingleton<CosmosClient>(serviceProvider => | |
{ | |
IHttpClientFactory httpClientFactory = serviceProvider.GetRequiredService<IHttpClientFactory>(); | |
CosmosClientOptions cosmosClientOptions = new CosmosClientOptions() | |
{ | |
HttpClientFactory = httpClientFactory.CreateClient |
This file contains hidden or 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) | |
{ | |
// Maintain a single instance of the SocketsHttpHandler for the lifetime of the application | |
SocketsHttpHandler socketsHttpHandler = new SocketsHttpHandler(); | |
socketsHttpHandler.PooledConnectionLifetime = TimeSpan.FromMinutes(10); // Customize this value based on desired DNS refresh timer | |
socketsHttpHandler.MaxConnectionsPerServer = 20; // Customize the maximum number of allowed connections | |
services.AddSingleton<SocketsHttpHandler>(socketsHttpHandler); | |
services.AddSingleton<CosmosClient>(serviceProvider => | |
{ |
This file contains hidden or 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) | |
{ | |
services.AddHttpClient(); | |
services.AddSingleton<CosmosClient>(serviceProvider => | |
{ | |
IHttpClientFactory httpClientFactory = serviceProvider.GetRequiredService<IHttpClientFactory>(); | |
CosmosClientOptions cosmosClientOptions = new CosmosClientOptions() | |
{ | |
HttpClientFactory = httpClientFactory.CreateClient, |
This file contains hidden or 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>"); | |
}); | |
} |
This file contains hidden or 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) |