我有一个由http请求触发的Azure函数,它使用绑定来输出到Azure存储队列并返回http响应。
当使用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,我需要降级我的解决方案来使用DotNet3.1和Azure函数的进程内模型。
有人知道我如何使用老式的进程内Azure函数实现相同的行为吗?
发布于 2021-07-27 17:47:16
您可以通过在函数本身中注入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>>);发布于 2021-07-29 03:13:10
为了写入存储帐户队列,而不是在接受的答案中写入服务总线队列,我使用了以下命令:
[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));
}https://stackoverflow.com/questions/68542402
复制相似问题