Consuming Azure Service Bus Queue Messages Created In Dynamics CRM

Welcome to this article, in previous articles we discussed how to create an Azure Aware plugin for Dynamics CRM.

When Dynamics CRM Online places a message in the queue it uses the RemoteExecutionContext object (Microsoft.Xrm.Sdk). This article will explain how to consume an Azure Service Bus Queue and the Dynamics CRM data.

To start with we need to get a couple of SDK’s.

Click Here for the Azure SDK, or alternatively use NPM manager.

Click Here for the Dynamics SDK

What are we going to achieve?

We going to consume Service Bus Queued messages using 3 different methods and output the Dynamics CRM changes to the console.

Lets Begin

First of all lets load visual studio and create a new console application.

Add a reference to Microsoft.Xrm.Sdk found [SDK Extract Path]\SDK\Bin

Next, in program.cs we’re going to add the following code:

Uri uri = ServiceBusEnvironment.CreateServiceUri("sb",
"[ServiceBus]", string.Empty);
string name = "RootManageSharedAccessKey";
string key = "[Key]";
TokenProvider tokenProvider =
TokenProvider.CreateSharedAccessSignatureTokenProvider(name, key);
MessagingFactory factory= MessagingFactory.Create(uri,tokenProvider);

For this example we will use SAS authentication, [ServiceBus] and [Key] can be found on the azure portal:

Log into Azure Here.

Navigate to your queue and select connection information.

crm4

AzureSB7

What have we done so far?

  1. We used the Azure SDK to create the URI for our ServiceBus.
  2. We used the Azure SDK to create a SAS Token Provider.
  3. Finally we created a Messaging Factory which is the anchor class used for run-time operations that send and receive messages to and from Service Bus queues.

 

What Next?

We’re now ready to retrieve data created by Dynamics CRM from the queue, we can do this in a number of ways:

  • Read and Delete – when the Service Bus receives the request, it marks the message as being consumed and returns it to the application.
  • Peek – a 2 stage process, this gives you the opportunity to retrieve the message, process it and then either call Complete or Abandon, when calling Abandon the Service Bus will unlock the message so it may be retrieved again.
  • Queue Client – the QueueClient class supports both send and receive operations.

 

Read and Delete
Console.WriteLine();
Console.WriteLine("Read and Delete---------------------");
MessageReceiver receiver = factory.CreateMessageReceiver("queue_test", ReceiveMode.ReceiveAndDelete);
BrokeredMessage receivedMessage = receiver.Receive();
RemoteExecutionContext context = receivedMessage.GetBody();
Entity contact = (Entity)context.InputParameters["Target"];
foreach (KeyValuePair<string,object> item in contact.Attributes)
{
Console.WriteLine(string.Format("Key: {0} Value: {1}", item.Key, item.Value));
}

Consume5

Peek
Console.WriteLine();
Console.WriteLine("Peek--------------------------------");
MessageReceiver receiverPeek = factory.CreateMessageReceiver("queue_test");
BrokeredMessage receivedMessagePeek = receiverPeek.Receive();
RemoteExecutionContext contextPeek = receivedMessagePeek.GetBody();
Entity contactPeek = (Entity)contextPeek.InputParameters["Target"];
try
{
foreach (KeyValuePair<string, object> item in contactPeek.Attributes)
{
Console.WriteLine(string.Format("Key: {0} Value: {1}", item.Key, item.Value));
}
receivedMessagePeek.Complete();
}
catch (Exception ex)
{
receivedMessagePeek.Abandon();
}

Consume6

Queue Client
Console.WriteLine();
Console.WriteLine("Queue Client------------------------");
QueueClient queueClient = factory.CreateQueueClient("queue_test");
BrokeredMessage receivedMessageQueue = queueClient.Receive();
RemoteExecutionContext contextQueue = receivedMessageQueue.GetBody();
Entity contactQueue = (Entity)contextQueue.InputParameters["Target"];
try
{
foreach (KeyValuePair<string, object> item in contactQueue.Attributes)
{
Console.WriteLine(string.Format("Key: {0} Value: {1}", item.Key, item.Value));
}
receivedMessageQueue.Complete();
}
catch (Exception ex)
{
receivedMessageQueue.Abandon();
}

Consume4

So the Service Bus Queue has been processed by our application and the update we made in CRM has been captured, consumed and processed.

In conclusion always do your own research and find the best solution to your problem, I hope this article has given you some insight into consuming Azure Service Bus Queue.
The next article describes how to create a BizTalk Decoder to handle the RemoteExecutionContext and process changes from Dynamics CRM in a BizTalk ESB.

Leave a Reply

Your email address will not be published. Required fields are marked *