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>();
}
}
void captureDiagnostics(CosmosDiagnostics diagnostics)
{
if (diagnostics.GetClientElapsedTime() > SomePredefinedThresholdTime)
{
Console.WriteLine(diagnostics.ToString());
}
}
services.AddCosmosCache((CosmosCacheOptions cacheOptions) =>
{
public class MyBusinessClass
{
private readonly IDistributedCache cache;
public MyBusinessClass(IDistributedCache cache)
{
this.cache = cache;
}
public async Task SomeOperationAsync()
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);
@ealsur
ealsur / clientinitializeasync.cs
Created April 16, 2021 17:56
Create and initialize CosmosClient
List<(string, string)> containers = new List<(string, string)>
{
("DatabaseNameForContainer1", "ContainerName1"),
("DatabaseNameForContainer2", "ContainerName2")
};
CosmosClientOptions cosmosClientOptions = new CosmosClientOptions
{
ApplicationName = "MyApplicationName",
// any other setting I want to customize