question

technavi avatar image
0 Votes"
technavi asked ·

Need to understand specific usage of these Namespaces Microsoft.Azure.ServiceBus vs Azure.Messaging.ServiceBus?

Just want to understand in Azure Message Queue. In various examples and references, used different namespaces -

Microsoft.Azure.ServiceBus and Azure.Messaging.ServiceBus

Could you please let me know what are the differences between them?

To understand further, I also read from the below link - https://msft.it/6010VB26q

But still having the same question.

To use Service Bus Queue, need to use the Namespace Azure.Messaging.ServiceBus.

Following the link (https://docs.microsoft.com/en-us/azure/service-bus-messaging/service-bus-dotnet-get-started-with-queues).

But on the other end, if I want to use the Message Session feature, How can I do with Azure.Messaging.ServiceBus namespace?

Using C# .NET Core.

Please suggest



azure-service-bus
10 |1000 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

MayankBargali-MSFT avatar image
1 Vote"
MayankBargali-MSFT answered ·

Hi @technavi

Welcome to Microsoft Q&A! Thanks for posting the question.

Sharing the previous discussion on the difference between both libraries. You can also refer to this if you are confused about which one to choose while developing your application.

To use Service Bus Queue you can choose any of the libraries. You can refer here for the service bus samples.
Azure.Messaging.ServiceBus does expose the Session operation: https://docs.microsoft.com/en-us/dotnet/api/azure.messaging.servicebus?view=azure-dotnet

You can refer to Sample for processing message from a session enabled queue and for Azure.Messaging.ServiceBus Samples please refer to this.

Hope the above helps. Feel free to get back to me if you have any queries or concerns.

Please 'Accept as answer' and ‘Upvote’ if it helped so that it can help others in the community looking for help on similar topics.

· 3 ·
10 |1000 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Thanks for your response. Your guidance really matters.

I was also reading from this article - In which it is comparing all the available SDK's - and also said, Azure.Messaging.ServiceBus SDK is the latest one.

Next, I need to read the message from Session Enabled Service Bus Queue using Azure Function (ServiceBusTrigger) by Azure.Messaging.ServiceBus SDK.

Could you please suggest relevant references/samples for the same?

0 Votes 0 ·

@technavi ·Thanks for your feedback. Yes, Azure.Messaging.ServiceBus SDK is the latest SDK. If you are using service bus trigger in the function app then you need to set the property "IsSessionsEnabled" to true. The code remains the same and you don't have to explicitly define the SDK to be used. Connectors exposed the attributes and annotations along with the configuration.

<1/2>

0 Votes 0 ·

Starting with extension version 3.1.0, you can trigger on a session-enabled queue or topic.

<2/2>

0 Votes 0 ·
technavi avatar image
0 Votes"
technavi answered ·

Yes, I am following the same by setting up the property IsSessionEnabled to true.

But how can I get the SessionId?

Please go through the below ServiceBusTrigger function -

 public static void Run([ServiceBusTrigger("mytqueue", Connection = "QConnection", **IsSessionsEnabled = true**)] string myQueueItem, string messageId, ILogger log)
         {
             log.LogInformation($"C# ServiceBus queue trigger function processed message: {myQueueItem}");
             log.LogInformation($"Message Id: {messageId}");
    
             **//log.LogInformation($"Session Id: {sessionId}");** //How to get session information
         }

On the other hand, I am able to get sessionId, if use the below parameters that are associated with Microsoft.Azure.ServiceBus


 public static void Run([ServiceBusTrigger("mytqueue", Connection = "QConnection", **IsSessionsEnabled = true**)] **Message message**, ILogger log)
         {
             log.LogInformation($"C# ServiceBus queue trigger function processed message: {Encoding.UTF8.GetString(message.Body)}");
             log.LogInformation($"Session Id processed message: **message.SessionId**");
         }


Please suggest the correct way to achieve the ServiceBusTrigger with Session Information.

· 5 ·
10 |1000 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

@technavi You can look into the usage section and if you pass the parameter as the string type then it will have the message content only if it is in text. You can also pass it as the Message type and it will have all the properties of the Message object. If your required is to log/get the session ID then the second code will be the better approach as the Message object will have the session properties whereas in your first code the parameter myQueueItem will only have the message content only if it is text.

Feel free to get back to me if you have any queries or concerns.

1 Vote 1 ·
technavi avatar image technavi MayankBargali-MSFT ·

Thanks for your clarification.

I further moved with the suggested option i.e. second code. I tuned the same according to DI implementation in ServiceBusTrigger. Below is for reference -

Here I used IMessageSession as the Message parameter is generating an error with DI.

The question in below code - Created Azure Function for Service Bus Trigger

  1. How can I utilize Session Feature in the below trigger, I don't think actual usage of the session currently implemented and seems not the right way to manage and use Sessions. Please suggest...

  2. In the Current code follow, it's just getting Messages from the queue, create the entry for a new queue for further use.

  3. How can I complete the session?

Please find the reference code below -

















0 Votes 0 ·

@technavi The connectors have the implementation of completing the session. You don't have to explicitly write any code to complete it. If you are using the service bus SDK in your application then you need to define your connection string, call different methods, etc to do operation on the message. Service bus is triggered whenever a new message arrives in the entity (queue/topic). You can refer to Ordered queue processing in Azure Functions with Sessions that is written by one of our PM from azure function that will help you working with service bus sessions in the function app.

0 Votes 0 ·
technavi avatar image technavi MayankBargali-MSFT ·

public async Task Run([ServiceBusTrigger("mytqueue", Connection = "QConnection", IsSessionsEnabled = true)] string myQueueItem, string messageId, IMessageSession messageSession, Int32 deliveryCount, ILogger log)
{

            //Process Queue Item to Database and get the response 

//TODO: DB Logic

                     //Prepare confirmation queue for the particular session
                     Confirmation jConfirm = new Confirmation
                     {
                         SessionId = messageSession.SessionId,
                          
                     };
                        
                     //Add SessionId and Confirmation message to Another Queue
                     await manageQueue.AddToConfirmQueueAsync(JsonSerializer.Serialize(jConfirm));
                   
              
         }



0 Votes 0 ·
technavi avatar image technavi MayankBargali-MSFT ·

73759-code-sample.png




Not sure but didn't post the code snippet, so attaching the screenshot of the same.

0 Votes 0 ·
code-sample.png (30.5 KiB)