使Azure函数在ServiceBusTrigger上顺序运行-maxConcurrentCalls不起作用 [英] Make Azure Function run sequential on ServiceBusTrigger - maxConcurrentCalls not working

查看:132
本文介绍了使Azure函数在ServiceBusTrigger上顺序运行-maxConcurrentCalls不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个看起来像这样的Azure函数

I have an Azure Function which looks like this

[FunctionName("Dispatcher")]
public static void Run([ServiceBusTrigger(queueName: "defaultqueue", Connection = "AzureWebJobsQueue")]string queueMessage, TraceWriter log)
{

    Dictionary<string, string> payload = JsonConvert.DeserializeObject<Dictionary<string, string>>(queueMessage);
    string url = payload["url"];

    try
    {
        log.Info("Collect result: " + url);

        using (HttpResponseMessage response = httpClient.GetAsync(url).Result)
        {
            int statusCode = (int)response.StatusCode;

            if (statusCode == 200)
            {
                var responseString = response.Content.ReadAsStringAsync().Result;
                DoStuff(log, responseString);
            }
        }
    }
}

HttpClient上的GET大约需要一分钟.我要等待此调用完成,直到从服务总线队列中检索到下一条消息为止.

The GET on the HttpClient takes roughly one minute. I want to wait for this call to finish until the next message is retrieved from the Service Bus Queue.

我的host.json看起来像这样

My host.json looks like this

{
    "functionTimeout": "23:00:00",
    "serviceBus": {
        "maxConcurrentCalls": 1,
        "prefetchCount": 1,
        "autoRenewTimeout": "00:05:00"
    }
}

但是,当我将消息放入Service Bus队列并运行功能时,我立即看到多个收集结果: http://example.com ",这显然意味着该功能的多个实例正在并行运行.如何配置Azure Functions以等待HttpClient和DoStuff()完成,直到从队列中提取新消息?

However, when I put messages in the Service Bus Queue and run the Function, I instantly see multiple "Collect result: http://example.com" in the logs which apparently means, multiple instances of the Function are running in parallel. How can I configure Azure Functions to wait until HttpClient and DoStuff() is finished until pulling a new message from the Queue?

推荐答案

我认为您仍然可以使用从Web作业中移植的功能.为了在所有实例中仅执行一次该功能,请将此属性放在您的方法上方.

I think you can still use the functionality that was ported from web jobs. In order to only execute the function once across all instances put this attribute above your method.

  [Singleton(Mode = SingletonMode.Listener)]

此功能未记录在Function sdk中,但看起来仍然存在.这是webjobs文档. Azure Web Jobs SDK Singleton

This isnt documented in the Functions sdk but it looks like its still there. Here is the webjobs documentation. Azure Web Jobs SDK Singleton

这篇关于使Azure函数在ServiceBusTrigger上顺序运行-maxConcurrentCalls不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆