Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Azure Functions: binding to DocumentClient versus static instance - what's recommended?

I know how to bind queries directly to an Azure Function and use Cosmos DB triggers in functions.

However, I'm looking for direction around using DocumentClient (Nuget package Microsoft.Azure.Cosmos) directly.

  1. There's documentation that explains how to reuse a static client instance between executions.
  2. It is also possible to get a DocumentClient instance as a binding by adding [DocumentDB("test", "test", ConnectionStringSetting = "CosmosDB")] DocumentClient client to the function's parameters.
  3. Finally, it is possible to create a DocumentClient instance in the function's body: var client = new DocumentClient(...).

I do not find a clear recommendation when to use what approach except that number 3 never is a good option because of performance, memory usage and connection limits. Also, I understand that using a static instance has advantages.

Questions

  • Azure functions have a connection limit (discussed here). Does this also apply when using approach 2 (bind to client)?
  • What are the pros and cons of using approach 2 (binding) versus 1 (static)?
  • What's the advantage of binding to a SQL query compared to binding to a DocumentClient and creating the query in the function's body?
like image 378
Krumelur Avatar asked May 24 '19 07:05

Krumelur


People also ask

What are bindings in Azure Functions?

In Azure Functions, we use Bindings as a way of connecting resources to our functions. We can use input and output bindings and the data from our bindings is provided to our Functions as parameters.

Should functions be written inside static classes in azure?

Azure functions - should functions be written inside static classes. A static class can only contains static members and it can't be instantiated. Changed the class as non-static will allow you to add non-static members and create a instance of this class.

What is input binding in azure cosmosclient?

This concept is known as input binding and there are multiple bindings available for various Azure resources (See Azure Functions triggers and bindings concepts ). Unfortunately, there is no input binding for the new CosmosClient class that is part of the .NET SDK v3.

How do I use Azure Functions with cosmosdb?

Azure Functions allows you to inject an instance of the DocumentClient class to perform, read or write operations on your CosmosDB. This concept is known as input binding and there are multiple bindings available for various Azure resources (See Azure Functions triggers and bindings concepts ).


1 Answers

There is another way to use DocumentClient. Starting Version 1.0.28 of Microsoft.NET.Sdk.Functions, one can now use a FunctionsStartup class to initialize DocumentClient once, and then register it for DI (dependency injection), and then use the same instance every time.

The FunctionsStartup class is documented here. And a better explanation is here.

In your Startup's configure method, build your client.

using Microsoft.Azure.Functions.Extensions.DependencyInjection;
using Microsoft.Extensions.DependencyInjection;
[assembly: FunctionsStartup(typeof(MyApp.Startup))]
namespace MyApp
{
    public class Startup : FunctionsStartup
    {
        public override void Configure(IFunctionsHostBuilder builder)
        {
            IDocumentClient client = GetCustomClient();
            builder.Services.AddSingleton<IDocumentClient>(client);
        }
}

This can be then injected into the function constructor and used by the methods.

public class MyFunction
{
    private IDocumentClient _client;

    public MyFunction(IDocumentClient client)
    {
        _client = client;
    }

    [FunctionName("MyFunction")]
    public async Task<IActionResult> Run(
        [HttpTrigger(AuthorizationLevel.Function, "get", "post", Route = null)] HttpRequest req,
        ILogger log)
    {
        // use _client here.
    }
}

When Azure creates an instance of this class to serve a request, it passes the IDocumentClient instance that was created in FunctionsStartup class.

This strategy allows one to reuse the same instance of DocumentClient. Singeton-ness of this client is not forced by making it static, but by making sure we only create it once. This also helps with testability as tests can inject a different instance of IDocumentClient.

like image 80
Turbo Avatar answered Oct 20 '22 16:10

Turbo