Azure 函数输出到队列 AND return http 响应

Azure function output to queue AND return http response

我有一个由 http 请求触发并使用绑定输出到 Azure 存储队列和 return 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 函数的 return 类型:

[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 函数实现相同的行为?

您可以通过在函数本身中注入 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通过return语句的http响应;类似下面

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

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

[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));
}