Skip to content

Instantly share code, notes, and snippets.

View ealsur's full-sized avatar

Matias Quaranta ealsur

View GitHub Profile
CosmosAsyncClient client = new CosmosClientBuilder()
.endpoint("account endpoint")
.key("account key")
.consistencyLevel(ConsistencyLevel.EVENTUAL)
.contentResponseOnWriteEnabled(false)
.buildAsyncClient();
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;
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>",
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>",
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;
public void ConfigureServices(IServiceCollection services)
{
services.AddHttpClient();
services.AddSingleton<CosmosClient>(serviceProvider =>
{
IHttpClientFactory httpClientFactory = serviceProvider.GetRequiredService<IHttpClientFactory>();
CosmosClientOptions cosmosClientOptions = new CosmosClientOptions()
{
HttpClientFactory = httpClientFactory.CreateClient
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 =>
{
public void ConfigureServices(IServiceCollection services)
{
services.AddHttpClient();
services.AddSingleton<CosmosClient>(serviceProvider =>
{
IHttpClientFactory httpClientFactory = serviceProvider.GetRequiredService<IHttpClientFactory>();
CosmosClientOptions cosmosClientOptions = new CosmosClientOptions()
{
HttpClientFactory = httpClientFactory.CreateClient,
public void ConfigureServices(IServiceCollection services)
{
// other services
services.AddSingleton((s) =>
{
return new CosmosClient("<connection-string>");
});
}
@ealsur
ealsur / transactionalbatch-error.cs
Created January 6, 2020 18:42
TransactionalBatch - Error view
// 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)