简体   繁体   English

Azure 服务总线 | 启用会话 | 会话没有收到

[英]Azure Service Bus | Enable Session | Session does not receive

I have a requirement to process the same set of messages together and for this, I was trying Azure Service Bus Sessions Enabled feature.我需要一起处理同一组消息,为此,我尝试了 Azure 服务总线会话启用功能。 To test this, I created a very simple application, a message is submitted successfully in a queue, however, while trying to receive the message in "ReceiveSessionMessage" function, a message session is not returned and the program exits after this line.为了测试这一点,我创建了一个非常简单的应用程序,在队列中成功提交了一条消息,但是,当尝试在“ReceiveSessionMessage”函数中接收消息时,没有返回消息会话并且程序在此行之后退出。

I am not able to figure out the exact root cause, any help would be much appreciated.我无法找出确切的根本原因,任何帮助将不胜感激。 Thanks谢谢

[var messageSession = await sessionClient.AcceptMessageSessionAsync();] [var messageSession = await sessionClient.AcceptMessageSessionAsync();]

Program程序

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);
            }
        }
    }
}

Console Output控制台输出

在此处输入图片说明

The issue is with the declaration of问题在于声明

static void Main(string[] args) , and the calling method "ReceiveSessionMessage()" in it. static void Main(string[] args) ,以及其中的调用方法“ReceiveSessionMessage()”。 The correct way of calling this function from the Program.cs was从 Program.cs 调用此函数的正确方法是

  static async Task Main(string[] args)
    {

        Console.WriteLine("Message Session Handler..");
        await MessageSessionReceiver();
    }

"ReceiveSessionMessage" function was an async function and the calling function did not have the await keyword mentioned due to which the program exited. “ReceiveSessionMessage”函数是一个异步函数,调用函数没有提到await关键字,因为程序退出。 After changing the syntax to add await, it worked.更改语法以添加等待后,它起作用了。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM