是否可以在 Azure Functions .Net 5 中使用多个输出?
Is that possible to use multiple output in Azure Functions .Net 5?
我在 ServiceBus 中使用 .net 5 azure 函数。我想从触发器功能发送多条消息。
在以前的版本中,您可以使用 IAsyncCollector 来执行类似的操作:
[FunctionName("HttpToServiceBusQueue")]
public static async Task<IActionResult> Run(
[HttpTrigger(AuthorizationLevel.Anonymous, "get", "post", Route = null)] HttpRequest req,
[ServiceBus("testqueue",Connection ="connectionString")] IAsyncCollector<string> outputEvents,
ILogger log)
{
log.LogInformation("C# HTTP trigger function processed a request.");
// ...
await outputEvents.AddAsync("message1");
await outputEvents.AddAsync("message2");
// ...
return new OkObjectResult(responseMessage);
}
但是正如我在 documentation 中读到的那样,新版本不支持 IAsyncCollector。
有没有其他方法可以做到这一点?
我的代码示例:
[Function("FileTriggerFunction")]
[ServiceBusOutput("fileupload", Connection = "ServiceBusConnectionWrite")]
public string Run(
[BlobTrigger("file-storage/{name}", Connection = "ConnectionString")] string myBlob, string name,
FunctionContext context)
{
var logger = context.GetLogger("FileTriggerFunction");
var res = JsonConvert.SerializeObject(*List of messages*);
logger.LogInformation(res);
return res;
}
实际上它将列表中的每个对象分开,用于单独的消息,但我认为这样做是不正确的。
我们在 .net 5 中有多个输出绑定,而不是 IAsyncCollector
在这个函数上,我们将给出多个输出值。
public static class MultiOutput
{
[Function("MultiOutput")]
public static MyOutputType Run([HttpTrigger(AuthorizationLevel.Anonymous, "get")] HttpRequestData req,
FunctionContext context)
{
var response = req.CreateResponse(HttpStatusCode.OK);
response.WriteString("Success!");
string myQueueOutput = "some output";
return new MyOutputType()
{
Name = myQueueOutput,
HttpResponse = response
};
}
}
public class MyOutputType
{
[QueueOutput("myQueue")]
public string Name { get; set; }
public HttpResponseData HttpResponse { get; set; }
}
参考.NET isolated process guide for .NET 5.0 in Azure Functions
我曾尝试使用 SeviceBus Client 将多条消息发送到我能够处理的队列。
参考here
这是一个示例代码,如何操作
[Function("MultiOutput")]
public static DispatchedMessages Run([HttpTrigger(AuthorizationLevel.Anonymous, "get")] HttpRequestData req,
FunctionContext context)
{
return new DispatchedMessages
{
Messages = new List<string> { "aaa", "bbb" } // todo: serialize
};
}
public class DispatchedMessages
{
[ServiceBusOutput(queueOrTopicName: "dest", Connection = "AzureServiceBus")]
public IEnumerable<string> Messages { get; set; }
}
}
我在 ServiceBus 中使用 .net 5 azure 函数。我想从触发器功能发送多条消息。 在以前的版本中,您可以使用 IAsyncCollector 来执行类似的操作:
[FunctionName("HttpToServiceBusQueue")]
public static async Task<IActionResult> Run(
[HttpTrigger(AuthorizationLevel.Anonymous, "get", "post", Route = null)] HttpRequest req,
[ServiceBus("testqueue",Connection ="connectionString")] IAsyncCollector<string> outputEvents,
ILogger log)
{
log.LogInformation("C# HTTP trigger function processed a request.");
// ...
await outputEvents.AddAsync("message1");
await outputEvents.AddAsync("message2");
// ...
return new OkObjectResult(responseMessage);
}
但是正如我在 documentation 中读到的那样,新版本不支持 IAsyncCollector。 有没有其他方法可以做到这一点?
我的代码示例:
[Function("FileTriggerFunction")]
[ServiceBusOutput("fileupload", Connection = "ServiceBusConnectionWrite")]
public string Run(
[BlobTrigger("file-storage/{name}", Connection = "ConnectionString")] string myBlob, string name,
FunctionContext context)
{
var logger = context.GetLogger("FileTriggerFunction");
var res = JsonConvert.SerializeObject(*List of messages*);
logger.LogInformation(res);
return res;
}
实际上它将列表中的每个对象分开,用于单独的消息,但我认为这样做是不正确的。
我们在 .net 5 中有多个输出绑定,而不是 IAsyncCollector
在这个函数上,我们将给出多个输出值。
public static class MultiOutput
{
[Function("MultiOutput")]
public static MyOutputType Run([HttpTrigger(AuthorizationLevel.Anonymous, "get")] HttpRequestData req,
FunctionContext context)
{
var response = req.CreateResponse(HttpStatusCode.OK);
response.WriteString("Success!");
string myQueueOutput = "some output";
return new MyOutputType()
{
Name = myQueueOutput,
HttpResponse = response
};
}
}
public class MyOutputType
{
[QueueOutput("myQueue")]
public string Name { get; set; }
public HttpResponseData HttpResponse { get; set; }
}
参考.NET isolated process guide for .NET 5.0 in Azure Functions
我曾尝试使用 SeviceBus Client 将多条消息发送到我能够处理的队列。
参考here
这是一个示例代码,如何操作
[Function("MultiOutput")]
public static DispatchedMessages Run([HttpTrigger(AuthorizationLevel.Anonymous, "get")] HttpRequestData req,
FunctionContext context)
{
return new DispatchedMessages
{
Messages = new List<string> { "aaa", "bbb" } // todo: serialize
};
}
public class DispatchedMessages
{
[ServiceBusOutput(queueOrTopicName: "dest", Connection = "AzureServiceBus")]
public IEnumerable<string> Messages { get; set; }
}
}