Skip to content

Instantly share code, notes, and snippets.

Avatar

Matias Quaranta ealsur

View GitHub Profile
@ealsur
ealsur / functions-client.cs
Created Oct 21, 2022
Azure Functions CosmosClient binding
View functions-client.cs
[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 Oct 20, 2022
Azure Functions user agent stamping
View host.json
{
"cosmosDB": {
"userAgentSuffix": "MyUniqueIdentifier"
}
}
@ealsur
ealsur / functions-trigger.cs
Created Oct 20, 2022
Azure Functions Trigger with custom type
View functions-trigger.cs
[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 Oct 20, 2022
Azure Functions custom serializer
View functions-customserialization.cs
[assembly: FunctionsStartup(typeof(FunctionApp1.Startup))]
namespace FunctionApp1
{
public class Startup : FunctionsStartup
{
public override void Configure(IFunctionsHostBuilder builder)
{
builder.Services.AddSingleton<ICosmosDBSerializerFactory, MyCosmosDBSerializerFactory>();
}
}
View cache-use.cs
public class MyBusinessClass
{
private readonly IDistributedCache cache;
public MyBusinessClass(IDistributedCache cache)
{
this.cache = cache;
}
public async Task SomeOperationAsync()
View cache-diagnostics.cs
void captureDiagnostics(CosmosDiagnostics diagnostics)
{
if (diagnostics.GetClientElapsedTime() > SomePredefinedThresholdTime)
{
Console.WriteLine(diagnostics.ToString());
}
}
services.AddCosmosCache((CosmosCacheOptions cacheOptions) =>
{
View caching-create-inject.cs
services.AddCosmosCache((CosmosCacheOptions cacheOptions) =>
{
cacheOptions.ContainerName = "myContainer";
cacheOptions.DatabaseName = "myDatabase";
cacheOptions.CosmosClient = preExistingClient;
/* Creates the container if it does not exist */
cacheOptions.CreateIfNotExists = true;
});
View caching-create.cs
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;
View enablecontentresponseonwrite-sample-clientoptions.cs
CosmosClientOptions clientOptions = new CosmosClientOptions(){
// other client options
EnableContentResponseOnWrite = false
};
CosmosClient client = new CosmosClient("<connection-string>", clientOptions);
View enablecontentresponseonwrite-sample-delete-disable.cs
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>",