Skip to content

Instantly share code, notes, and snippets.

View ealsur's full-sized avatar

Matias Quaranta ealsur

View GitHub Profile
@ealsur
ealsur / functions-client.cs
Created October 21, 2022 22:19
Azure Functions CosmosClient binding
[FunctionName("DocsByUsingCosmosClient")]
public static async Task<IActionResult> Run(
[HttpTrigger(AuthorizationLevel.Anonymous, "get", "post",
Route = null)]HttpRequest req,
[CosmosDB(
databaseName: "ToDoItems",
containerName: "Items",
Connection = "CosmosDBConnection")] CosmosClient client,
ILogger log) {
@ealsur
ealsur / host.json
Created October 20, 2022 21:36
Azure Functions user agent stamping
{
"cosmosDB": {
"userAgentSuffix": "MyUniqueIdentifier"
}
}
@ealsur
ealsur / functions-trigger.cs
Created October 20, 2022 21:13
Azure Functions Trigger with custom type
[FunctionName("Function1")]
public static void Run([CosmosDBTrigger(
databaseName: "test",
containerName: "items",
LeaseContainerPrefix = "test",
Connection = "cosmosDB")]
IReadOnlyList<MyPOCOWithSystemTextJson> input, ILogger log)
{
//...
}
@ealsur
ealsur / functions-customserialization.cs
Created October 20, 2022 21:06
Azure Functions custom serializer
[assembly: FunctionsStartup(typeof(FunctionApp1.Startup))]
namespace FunctionApp1
{
public class Startup : FunctionsStartup
{
public override void Configure(IFunctionsHostBuilder builder)
{
builder.Services.AddSingleton<ICosmosDBSerializerFactory, MyCosmosDBSerializerFactory>();
}
}
public class MyBusinessClass
{
private readonly IDistributedCache cache;
public MyBusinessClass(IDistributedCache cache)
{
this.cache = cache;
}
public async Task SomeOperationAsync()
void captureDiagnostics(CosmosDiagnostics diagnostics)
{
if (diagnostics.GetClientElapsedTime() > SomePredefinedThresholdTime)
{
Console.WriteLine(diagnostics.ToString());
}
}
services.AddCosmosCache((CosmosCacheOptions cacheOptions) =>
{
services.AddCosmosCache((CosmosCacheOptions cacheOptions) =>
{
cacheOptions.ContainerName = "myContainer";
cacheOptions.DatabaseName = "myDatabase";
cacheOptions.CosmosClient = preExistingClient;
/* Creates the container if it does not exist */
cacheOptions.CreateIfNotExists = true;
});
public void ConfigureServices(IServiceCollection services)
{
/* Other service configurations */
services.AddCosmosCache((CosmosCacheOptions cacheOptions) =>
{
CosmosClientBuilder clientBuilder = new CosmosClientBuilder("myConnectionString")
.WithApplicationRegion("West US");
cacheOptions.ContainerName = "myContainer";
cacheOptions.DatabaseName = "myDatabase";
cacheOptions.ClientBuilder = clientBuilder;
CosmosClientOptions clientOptions = new CosmosClientOptions(){
// other client options
EnableContentResponseOnWrite = false
};
CosmosClient client = new CosmosClient("<connection-string>", clientOptions);
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>",