gpt4 book ai didi

azure - 有两种方法可以从 azure 函数 HTTP 触发器读取所有服务总线消息

转载 作者:行者123 更新时间:2023-12-03 05:40:20 24 4
gpt4 key购买 nike

有 2 种方法可以从服务总线主题读取所有消息

推荐用于 azure 函数的哪一个

选项 1 -

var messageReceiver = new MessageReceiver(SBConnString, QueueName, ReceiveMode.PeekLock);
Message message = await messageReceiver.ReceiveAsync();

选项 2 -

static async Task ReceiveOrProcessMessagesAsync(Message message, CancellationToken token)
{
// Process the message
Console.WriteLine($"Received message: SequenceNumber:{message.SystemProperties.SequenceNumber} Body:{Encoding.UTF8.GetString(message.Body)}");

// Complete the message so that it is not received again.
// This can be done only if the queueClient is created in ReceiveMode.PeekLock mode (which is default).
await queueClient.CompleteAsync(message.SystemProperties.LockToken);

// Note: Use the cancellationToken passed as necessary to determine if the queueClient has already been closed.
// If queueClient has already been Closed, you may chose to not call CompleteAsync() or AbandonAsync() etc. calls
// to avoid unnecessary exceptions.
}

azure 函数 http 触发器应该使用什么?

最佳答案

检查 azure binding table ,您会发现azure函数暂时不支持服务总线输入绑定(bind)。

所以如果你想在函数中接收消息,你只能使用服务总线触发器或服务总线sdk。您提供的第一个示例是 sdk,第二个示例是触发器。

而且你说你要在http触发函数中接收消息,所以你必须使用Service Bus SDK来实现。您可以引用下面的代码。

public static class Function1
{
[FunctionName("Function1")]
public static async void Run(
[HttpTrigger(AuthorizationLevel.Function, "get", "post", Route = null)] HttpRequest req,
ILogger log)
{
log.LogInformation("C# HTTP trigger function processed a request.");

string serviceBusConnectionString = Environment.GetEnvironmentVariable("servicebuscon");
var messageReceiver = new MessageReceiver(serviceBusConnectionString, "myqueue", ReceiveMode.PeekLock, null, 500);


var tempMessages = await messageReceiver.ReceiveAsync(500, TimeSpan.FromSeconds(1));

foreach (Message m1 in tempMessages)
{

log.LogInformation($"C# HTTP trigger function processed message: {Encoding.UTF8.GetString(m1.Body)}");
}
}
}

关于azure - 有两种方法可以从 azure 函数 HTTP 触发器读取所有服务总线消息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59238528/

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