{Dynamics 365 + Azure + Queues} Part4– Developing a queue listener to read messages from the queue

This is the final post in the series and if the you are directly in here, I strongly suggest to start from the link – https://debajmecrm.com/blog-series-complete-in-depth-walkthrough-of-dynamics-crm-plugins-with-azure-service-bus-queues/

So we are almost at the end of our journey and we just have to build a client to read the message we have posted in the queue.

If you remember, in my third post, I have explained that are three sets of permissions for a queue

  • Send – If a client is provided with Send privileges for the queue, the client can post messages to the queue
  • Listen – If a client is provided with Listen privileges for the queue, the client can process incoming messages to the queue.
  • Manage – Manage is like the admin credentials for the queue. If you give manage, send and listen privileges are included by default

Since you are just building a listener here, you should provide only listen privileges to the queue to the client. It is very important to understand this. If you do not understand how the privileges work and simply follow the steps documented in the SDK, you might end up with using the Management credentials of the entire service bus in your code. Believe me I have seen real life examples where the queue listener is constructed with the service bus root credentials. Those are like super-admin credentials and a person gaining access to those credentials can end up modifying the entire service bus configuration and not just the queue.

So let’s see how we can set-up a Access Policy with just the listen privilege.

Go to your service bus  and then open the ‘testqueue’ that we created earlier.

Click on Configure. A screen like below will come up.

image

Go to ‘Shared Access Policies’ section and type in a new policy name called ListenerClient. Select Listen privilege only. After all, I just need the client to read messages right?

image

Click on Save at the bottom. Once saved in the ‘Shared Access Key generator’ section, select the ListenerClient Policy. You would see both the primary key and secondary key.

image

Now we need the connection string for the Queue for the ListenerClient policy. To find the connection string, click on the Dashboard of the queue and then click on View Connection String

image

Copy the connection string of the Listener client from the pop-up window and store it somewhere. You will need it very soon.

image

 

So you are all set. Now I create a console application called QueueListener. 

Using Nuget, install the azure service bus dlls. Also include reference to the Microsoft.Xrm.Sdk dll

image

Below is the code for reading data from the queue.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Configuration;
using System.ServiceModel;
using System.Runtime.Serialization;

using Microsoft.ServiceBus;
using Microsoft.ServiceBus.Messaging;
using Microsoft.Xrm.Sdk;

namespace QueueListener
{
    class Program
    {
        static void Main(string[] args)
        {
            var queueProcesssor = new QueueProcessor();
            queueProcesssor.CreateQueueClient();
            queueProcesssor.ProcessMessages();

            Console.Read();
        }
    }

    internal class QueueProcessor
    {
        private const string QueueName = "TestQueue";
        internal QueueClient _queueClient;

        public QueueProcessor()
        {
        }

        internal void CreateQueueClient()
        {
            _queueClient = QueueClient.CreateFromConnectionString(ConfigurationManager.AppSettings["Microsoft.ServiceBus.ConnectionString"]);
        }

        internal void ProcessMessages()
&#16
0;       {
            while(true)
            {
                var message = _queueClient.Receive();

                if(message != null)
                {
                    var context = message.GetBody<RemoteExecutionContext>();

                    Console.WriteLine("User Id: {0}", context.UserId);
                    Console.WriteLine("Organization Id: {0}", context.OrganizationId);

                    message.Complete();
                }
            }   
        }
    }
}

The code reads the connection string from configuration file from the Key – Microsoft.ServiceBus.ConnectionString. So add an app settings value like the one below

<appSettings>
        <!– Service Bus specific app setings for messaging connections –>
        <add key="Microsoft.ServiceBus.ConnectionString"
            value="<Paste the connection string copied for Listener client earlier>”/>

Just a few lines of code and you are done. And when I run the code, below is the screenshot from debugger.

image

 

I hope my series of blogs have not only helped you to show how you can configure dynamics crm with azure but also helped you to understand the inner workings as well.

Leave a comment on how you liked it. Till then happy CRMing