Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Function1
using Bogus;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Azure.Cosmos;
using Microsoft.Azure.Cosmos.Fluent;
using Microsoft.Azure.WebJobs;
using Microsoft.Azure.WebJobs.Extensions.Http;
using Microsoft.Extensions.Logging;
using Newtonsoft.Json;
using System;
using System.Threading.Tasks;
namespace MyFuncApp
{
public static class Function1
{
[FunctionName("Function1")]
public static async Task<IActionResult> Run(
[HttpTrigger(AuthorizationLevel.Function, "post", Route = null)] HttpRequest req,
ILogger log)
{
log.LogInformation("Function1 processed a request.");
var cosmosClient = new CosmosClientBuilder(
Environment.GetEnvironmentVariable("AzureCosmosDocumentStoreOptions:ConnectionString"))
.WithConnectionModeDirect()
.WithThrottlingRetryOptions(new TimeSpan(0, 0, 0, 10), 0)
.Build();
var cosmosDatabase =
cosmosClient.GetDatabase("Items");
var container =
cosmosDatabase.GetContainer("ToDos");
var toDo =
new Faker<ToDo>()
.RuleFor(u => u.Description, f => f.Lorem.Sentences(20000))
.Generate();
var toDoResponse =
await container.CreateItemAsync<ToDo>(
toDo,
new PartitionKey(toDo.Id.ToString()));
for (var i = 0; i < 5; i++)
{
log.LogInformation($"Performing update #{i + 1}...");
try
{
toDoResponse =
await container.ReplaceItemAsync<ToDo>(
toDo,
toDo.Id.ToString(),
new PartitionKey(toDo.Id.ToString()));
}
catch (CosmosException ex)
{
log.LogError(ex, ex.Message);
}
}
return new OkResult();
}
}
public class ToDo
{
[JsonProperty("id")]
public Guid Id { get; set; }
[JsonProperty("description")]
public string Description { get; set; }
public ToDo()
{
this.Id = Guid.NewGuid();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.