【问题标题】:Azure function output to queue AND return http responseAzure 函数输出到队列并返回 http 响应
【发布时间】:2023-03-18 16:10:01
【问题描述】:

我有一个由 http 请求触发并使用绑定输出到 Azure 存储队列并返回 http 响应的 Azure 函数。

当使用 Functions.Worker 程序集为 dotnet-isolated 编码时,此方法有效。首先,我为队列消息和 http 响应声明了一个类型:

using Microsoft.Azure.Functions.Worker;
using Microsoft.Azure.Functions.Worker.Http;

namespace SmsRouter.AzFunc
{
    public class QueueAndHttpOutputType
    {
        [QueueOutput("%SendSmsQueueName%")]
        public string QueueMessage { get; set; } = "";

        public HttpResponseData HttpResponse { get; set; }
    }
}

然后我将其用作 Azure 函数的返回类型:

[Function(nameof(SendSimpleSms))]
        public async Task<QueueAndHttpOutputType> SendSimpleSms([HttpTrigger(AuthorizationLevel.Function, "post", Route = "v1.0/simple-sms")] HttpRequestData req,
            FunctionContext executionContext)

不幸的是,由于this issue,我需要降级我的解决方案以使用 dotnet 3.1 和 Azure Functions 的进程内模型。

有谁知道我如何使用旧式进程内 Azure 函数实现相同的行为?

【问题讨论】:

  • 我正在尝试使用具有 Queue 作为输出绑定的 Http 触发器与 .Net 5 一起使用类似的东西。我看到的大多数示例都是针对 3.1 或更低版本的。如果您可以检查我的问题stackoverflow.com/questions/69910895/…,将不胜感激
  • 看起来你已经有了答案:)
  • 很难在 MultiResponse 中找到命名空间或块 4 输出绑定 [QueueOutput("xx-data")]。使用 System.Net;使用 Microsoft.Azure.Functions.Worker.Extensions;使用 Microsoft.Azure.Functions.Worker.Http;使用 Azure.Storage.Queues;使用 Azure.Storage.Queues.Models; namespace XXX.Models { public class MultiResponse { [QueueOutput("xxx-data")] stackoverflow.com/questions/69910895

标签: azure-functions


【解决方案1】:

您可以通过在函数本身中注入ServiceBus 输出绑定来实现。

public async Task<IActionResult> SendSimpleSms(
        [HttpTrigger(AuthorizationLevel.Function, "post", Route = "v1.0/simple-sms")] HttpRequestData req,
        [Queue("%SendSmsQueueName%", Connection = "QueueConnectionString")] IAsyncCollector<string> queue
            ExecutionContext executionContext)

在服务总线中添加消息调用AddAsync方法如下所示

await queue.AddAsync(message);

并通过return语句返回http响应;像下面的东西

return new OkObjectResult(<<Your data here>>);

【讨论】:

  • 感谢您的回答,但我需要发布到 Azure 存储帐户队列而不是服务总线队列。
  • 在这种情况下,您可以将Queue 输出绑定用作[Queue("%SendSmsQueueName%", Connection = "QueueConnectionString")] IAsyncCollector&lt;CloudQueueMessage&gt; queue ExecutionContext executionContext)。您也可以使用out T 来编写单条消息,多条消息使用IAsyncCollector。更多信息here
  • 再次感谢,但在我的情况下,Azure 函数需要返回 http 响应以及将单个消息发布到存储队列。我认为您的原始答案让我走上了正确的轨道,返回 IActionResult 并使用 queue.AddAsync。
  • 是的,我的最后一条评论是关于将[ServiceBus 更改为[Queue 绑定...根据我的回答第3 行。现在添加消息使用,await queue.AddAsync(message);
【解决方案2】:

要写入存储帐户队列,而不是接受答案中的服务总线队列,我使用了以下内容:

[FunctionName(nameof(SendSimpleSms))]
        public async Task<IActionResult> SendSimpleSms([HttpTrigger(AuthorizationLevel.Function, "post", Route = "v1.0/simple-sms")] HttpRequest req,
            [Queue("%SendSmsQueueName%")] IAsyncCollector<string> queue)
        {
                await queue.AddAsync(jsonString);
                ...
                return new OkObjectResult(JsonConvert.SerializeObject(serviceRefResponse));
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-09-29
    • 2020-12-01
    • 2021-09-21
    • 1970-01-01
    • 2012-11-14
    • 2017-04-03
    相关资源
    最近更新 更多