我需要一起处理同一组消息,为此,我尝试了 Azure Service Bus Sessions Enabled 功能。为了测试这一点,我创建了一个非常简单的应用程序,一条消息在队列中成功提交,但是,当尝试在“ReceiveSessionMessage”函数中接收消息时,没有返回消息会话并且程序在此行之后退出。
我无法找出确切的根本原因,任何帮助将不胜感激。谢谢
[var messageSession = 等待 sessionClient.AcceptMessageSessionAsync();]
程序
using Microsoft.Azure.ServiceBus;
using System;
using System.Text;
using System.Threading.Tasks;
namespace TestSendReceiveMessagesAzure
{
class Program
{
static string connectionString = "";
static string queueName = "demosessionqueue";
static void Main(string[] args)
{
Console.WriteLine("Test Service Bus Session! enable feature");
SendMessage();
Console.WriteLine("Message Pushed");
ReceiveSessionMessage();
}
private static void SendMessage()
{
QueueClient queueClilent = new QueueClient(connectionString, queueName, ReceiveMode.PeekLock);
string msgJson = "{PizzaType:Veggie,SessionID:SessionId0101}";
Message message = new Message(Encoding.UTF8.GetBytes(msgJson))
{
SessionId = "SessionId0101"
};
Console.WriteLine(msgJson);
queueClilent.SendAsync(message).Wait();
}
private static async Task ReceiveSessionMessage()
{
var sessionClient = new SessionClient(connectionString, queueName, ReceiveMode.PeekLock);
Console.WriteLine("Accepting a message session...");
try
{
var messageSession = await sessionClient.AcceptMessageSessionAsync();
Console.WriteLine($"Message.SessionID={messageSession.SessionId}");
}
catch (Exception ex)
{
Console.WriteLine(ex.StackTrace);
}
}
}
}
控制台输出