gpt4 book ai didi

c# - 使用哪种方式使用 azure 函数将消息发送到主题?

转载 作者:行者123 更新时间:2023-12-03 05:01:08 26 4
gpt4 key购买 nike

我找到了两种从 azure 函数将消息发送到服务总线主题的方法。

一个正在使用输出 -

[FunctionName("ServiceBusOutput")]
[return: ServiceBus("myqueue", Connection = "ServiceBusConnection")]
public static string ServiceBusOutput([HttpTrigger] dynamic input, ILogger log)
{
log.LogInformation($"C# function processed: {input.Text}");
return input.Text;
}

另一个是使用代码 -

const string ServiceBusConnectionString = "string";
const string TopicName = "topicName";
static ITopicClient topicClient;
topicClient = new TopicClient(ServiceBusConnectionString, TopicName);
string messageBody = "Test";
var message = new Message(Encoding.UTF8.GetBytes(messageBody));
await topicClient.SendAsync(message);

I'm not getting which one we should use and when?

  1. if we use Output how to pass queue name myqueue as a variable so that in code we can assign it.

  2. if i have array how can we return one by one message to output return which will send one by one message to queue ?

最佳答案

来自here的完整示例。例如,如何使用 ICollector

写入多条消息
public static void Run(TimerInfo myTimer, ILogger log, [ServiceBus("myqueue", Connection = "ServiceBusConnection")] ICollector<string> outputSbQueue)
{
string message = $"Service Bus queue messages created at: {DateTime.Now}";
log.LogInformation(message);
outputSbQueue.Add("1 " + message);
outputSbQueue.Add("2 " + message);
}

据我所知,第一个版本,如果您的函数中有任何异步调用,则使用 return 不起作用。使用收集器的版本也可以在异步函数中工作,但只需使用 IAsyncCollector 即可。

关于c# - 使用哪种方式使用 azure 函数将消息发送到主题?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57144158/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com