Skip to content

Instantly share code, notes, and snippets.

Avatar

Matias Quaranta ealsur

View GitHub Profile
View enablecontentresponseonwrite-sample-java.cs
CosmosAsyncClient client = new CosmosClientBuilder()
.endpoint("account endpoint")
.key("account key")
.consistencyLevel(ConsistencyLevel.EVENTUAL)
.contentResponseOnWriteEnabled(false)
.buildAsyncClient();
View enablecontentresponseonwrite-sample-write-disable.cs
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;
View enablecontentresponseonwrite-sample-delete-disable.cs
ItemResponse<ToDoActivity> itemResponse = await this.container.DeleteItemAsync<ToDoActivity>(
"<id>",
new PartitionKey("<partitionKeyValue>"),
requestOptions : new ItemRequestOptions() { EnableContentResponseOnWrite = false });
ToDoActivity responseContent = itemResponse.Resource; // <-- This will be null now
double consumedRus = itemResponse.Headers.RequestCharge;
string etag = itemResponse.Headers.ETag;
string session = itemResponse.Headers.Session;
View enablecontentresponseonwrite-sample-delete.cs
ItemResponse<ToDoActivity> itemResponse = await this.container.DeleteItemAsync<ToDoActivity>(
"<id>",
new PartitionKey("<partitionKeyValue>"));
ToDoActivity responseContent = itemResponse.Resource;
double consumedRus = itemResponse.Headers.RequestCharge;
string etag = itemResponse.Headers.ETag;
string session = itemResponse.Headers.Session;
View enablecontentresponseonwrite-sample-write.cs
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;
View httpclientfactory-simple.cs
public void ConfigureServices(IServiceCollection services)
{
services.AddHttpClient();
services.AddSingleton<CosmosClient>(serviceProvider =>
{
IHttpClientFactory httpClientFactory = serviceProvider.GetRequiredService<IHttpClientFactory>();
CosmosClientOptions cosmosClientOptions = new CosmosClientOptions()
{
HttpClientFactory = httpClientFactory.CreateClient
View httpclientfactory-sockethandler.cs
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 =>
{
View httpclientfactory-blazor.cs
public void ConfigureServices(IServiceCollection services)
{
services.AddHttpClient();
services.AddSingleton<CosmosClient>(serviceProvider =>
{
IHttpClientFactory httpClientFactory = serviceProvider.GetRequiredService<IHttpClientFactory>();
CosmosClientOptions cosmosClientOptions = new CosmosClientOptions()
{
HttpClientFactory = httpClientFactory.CreateClient,
View httpclientfactory-usedi.cs
public void ConfigureServices(IServiceCollection services)
{
// other services
services.AddSingleton((s) =>
{
return new CosmosClient("<connection-string>");
});
}
@ealsur
ealsur / transactionalbatch-error.cs
Created Jan 6, 2020
TransactionalBatch - Error view
View transactionalbatch-error.cs
// 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)