简体   繁体   English

HTTP 触发器 Azure Z86408593C34AF77FDD90DF932F 上的 Singleton scopeId 支持的绑定

[英]Supported bindings for Singleton scopeId on HTTP Trigger Azure Function

I am unclear on how the scopeId parameter of the SingletonAttribute works.我不清楚SingletonAttributescopeId参数是如何工作的。 Specifically does the scopeId parameter work for HTTP Trigger Azure Functions when you bind it to a route parameter?具体来说,当您将其绑定到路由参数时, scopeId参数是否适用于 HTTP 触发器 Azure 函数? How does the binding work?绑定是如何工作的? What variables/values can I bind to?我可以绑定哪些变量/值?

For example:例如:

[Singleton("{input}", Mode = SingletonMode.Listener)]
public async Task<IActionResult> Run([HttpTrigger(AuthorizationLevel.Anonymous, "post", Route = "v1/{input:length(1,30)}")] Microsoft.AspNetCore.Http.HttpRequest req, string input, ILogger log) {
    return new OkObjectResult(input + " world");
}

A HTTP POST request to this function with the URI 'v1/hello' would return: "Hello world".一个 HTTP POST 请求到这个 function 的 URI 'v1/hello' 将返回:“Hello world”。

But would the Singleton attribute work such that all requests to 'v1/hello' would run serially whereas two simultaneous requests with one to 'v1/first' and the other to 'v1/second' would run in parallel?但是 Singleton 属性是否会起作用,以便所有对“v1/hello”的请求都将串行运行,而两个同时请求,一个对“v1/first”,另一个对“v1/second”将并行运行?

I see from this answer that for Service Bus Triggers you can bind to properties within the Message object directly.我从这个答案中看到,对于服务总线触发器,您可以直接绑定到消息 object的属性。
Also in the documentation there is an example of a Queue Trigger Function with scopeId binding to a property in the WorkItem object.此外, 在文档中还有一个队列触发器 Function 的示例,其中 scopeId 绑定到 WorkItem object 中的属性。
It's unclear what's supported for HTTP Trigger Functions.目前尚不清楚 HTTP 触发功能支持什么。

You have two ways to implement singleton mode in Azure function.您有两种方法可以在 Azure function 中实现 singleton 模式。

The first:首先:

You can do this by setting WEBSITE_MAX_DYNAMIC_APPLICATION_SCALE_OUT or maxConcurrentCalls .您可以通过设置WEBSITE_MAX_DYNAMIC_APPLICATION_SCALE_OUTmaxConcurrentCalls来做到这一点。

Singleton Azure function running as separate instances Singleton Azure function 作为单独的实例运行

The second type:第二种:

Create a complete Function project, similar to webapp, and implement it in Configure .创建一个完整的Function工程,类似于webapp,在Configure中实现。

Use dependency injection in .NET Azure Functions 在 .NET Azure 函数中使用依赖注入

public class Startup : FunctionsStartup
{
    public override void Configure(IFunctionsHostBuilder builder)
    {
        builder.Services.AddHttpClient();

        builder.Services.AddSingleton<IMyService>((s) => {
            return new MyService();
        });

        builder.Services.AddSingleton<ILoggerProvider, MyLoggerProvider>();
    }
}

I got a reply from Microsoft on GitHub: https://github.com/MicrosoftDocs/azure-docs/issues/69011#issuecomment-771922910我收到了微软关于 GitHub 的回复: https://github.com/MicrosoftDocs/azure-docs/issues/69011#issuecomment-771922910

"Binding expressions for Singleton have the same behavior as those for general input/output bindings. That is, any binding data from the trigger is available for reference. Singleton的绑定表达式与一般输入/输出绑定的行为相同。也就是说,来自触发器的任何绑定数据都可供参考。

In the case of HttpTrigger, that includes any POCO members if you're binding to a POCO type, as well as any route parameters.对于 HttpTrigger,如果您绑定到 POCO 类型,则包括任何 POCO 成员,以及任何路由参数。 In regards to your code, SingletonMode.Listener is not what you want for your requirement.关于您的代码, SingletonMode.Listener不是您想要的。 If you're just trying to serialize individual invocations of the function then you should use the default mode, ie [Singleton(“{input}”)] .如果您只是想序列化 function 的单个调用,那么您应该使用默认模式,即[Singleton(“{input}”)]

To answer your question – yes, this would serialize all invocations of v1/hello, allowing v1/first and v1/second to run concurrently."回答你的问题——是的,这将序列化所有 v1/hello 的调用,允许 v1/first 和 v1/second 同时运行。”

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM