问题描述
我知道如何将查询直接绑定到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.
- 文档,该文档说明了如何在两次执行之间重用静态客户端实例.
- 还可以通过在函数的参数中添加
[DocumentDB("test", "test", ConnectionStringSetting = "CosmosDB")] DocumentClient client
来获取DocumentClient
实例作为绑定. - 最后,可以在函数主体中创建
DocumentClient
实例:var client = new DocumentClient(...)
.
- There's documentation that explains how to reuse a static client instance between executions.
- 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. - 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.
在启动程序的configure方法中,构建您的客户端.
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函数:绑定到DocumentClient与静态实例-建议什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!