本文介绍了Azure Functions:绑定到 DocumentClient 与静态实例 - 推荐什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道如何将查询直接绑定到 Azure 函数并在函数中使用 Cosmos DB 触发器.

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

但是,我正在寻找直接使用 DocumentClient(Nuget 包 Microsoft.Azure.Cosmos)的方向.

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

  1. 文档,说明如何在执行之间重用静态客户端实例.
  2. 也可以通过添加 [DocumentDB("test", "test", ConnectionStringSetting = "CosmosDB")] DocumentClient 客户端DocumentClient 实例作为绑定> 到函数的参数.
  3. 最后,可以在函数体中创建一个DocumentClient实例:var client = new DocumentClient(...).
  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(...).

我没有找到明确的建议何时使用哪种方法,除了由于性能、内存使用和连接限制,数字 3 永远不是一个好的选择.另外,我知道使用静态实例具有优势.

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.

问题

  • Azure 函数有连接限制(在此处讨论).这在使用方法 2(绑定到客户端)时是否也适用?
  • 使用方法 2(绑定)与方法 1(静态)的优缺点是什么?
  • 与绑定到 DocumentClient 并在函数体中创建查询相比,绑定到 SQL 查询有什么优势?
  • 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?

推荐答案

还有另一种使用 DocumentClient 的方法.从 Microsoft.NET.Sdk.Functions 1.0.28 版本开始,现在可以使用 FunctionsStartup 类初始化 DocumentClient 一次,然后将其注册为 DI(依赖注入),然后每次都使用相同的实例.

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.

此处记录了 FunctionsStartup 类此处.更好的解释是这里.

在您的 Startup 的配置方法中,构建您的客户端.

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.
    }
}

当 Azure 创建此类的实例来处理请求时,它会传递在 FunctionsStartup 类中创建的 IDocumentClient 实例.

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

此策略允许重用 DocumentClient 的相同实例.这个客户端的单一性不是通过使其静态来强制的,而是通过确保我们只创建一次.这也有助于提高可测试性,因为测试可以注入不同的 IDocumentClient 实例.

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.

这篇关于Azure Functions:绑定到 DocumentClient 与静态实例 - 推荐什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-16 00:06