PL 400 : implement a Dataverse listener for an Azure solution (Publish and consume events)

Microsoft Dataverse is a tremendous product in the cloud market as it provides a business database with many features of applications that can consume software as a service (SaaS). We have already discussed about many features regarding this product, but it also integrates easily with external systems. Azure Bus Service provides the ability to work as asynchronous with the message broker and also facilitates to develop listener applications that can receive these events and processes as per the business requirements.

Microsoft Dataverse events trigger the execution of a remote code component called the listener.

These integrations are intended to provide an easy way to use additional extensibility outside the Dataverse platform. An Azure Solution Listener application that can read and process Microsoft Dataverse messages posted to the Azure Service Bus. A message queue is a store of messages on a service bus endpoint and a queue listener is an application that reads and processes these queued messages. Microsoft Dataverse supports a variety of methods to consume Azure Messaging Service Bus queue messages; queue, one-way, two ways, or REST. if using two way and REST, we are able to return a string of information back to Dataverse.

Write a queue listener:


  • Create a C# Console Application in Visual Studio that targets .NET 4.6.2 or higher.

  • Add the following NuGet packages:
    • Azure.Messaging.ServiceBus
    • Microsoft.CrmSdk.CoreAssemblies
  • In the application's Main method, paste the following code. Replace the Endpoint URL with your Azure Service Bus Namespace's Endpoint URL and the queue name if it differs:
string connectionString =@"[ENDPOINT URL]";
string queueName = "mslearnsamplequeue";
QueueClient queueClient = QueueClient.CreateFromConnectionString(connectionString, queueName, ReceiveMode.PeekLock);

  • To consume your message, use the "OnMessage" method, which provides the ability to process a Service Bus queue message in an event-driven message pump. 

queueClient.OnMessage(message =>
            {
                //get RemoteExecutionContext based on Message Format
                RemoteExecutionContext context = null;

                if (message.ContentType == "application/msbin1") //.NETBinary Message Format
                {
                    context = message.GetBody<RemoteExecutionContext>();
                }
                else if (message.ContentType == "application/json") //JSON Message Format
                {
                    context = message.GetBody<RemoteExecutionContext>(
                                      new DataContractJsonSerializer(typeof(RemoteExecutionContext)));
                }
                else if (message.ContentType == "application/xml") //XML Message Format
                {
                    context = message.GetBody<RemoteExecutionContext>(
                        new DataContractSerializer(typeof(RemoteExecutionContext)));
                }
                try
                {
//serialize the entire context leveraging the SerializeContext extension method
                    Console.WriteLine(context.SerializeContext());
                }
                catch (Exception ex)
                {
                    Console.WriteLine(ex.ToString());
                }
            });
  • At last we need to add Console.ReadLine() to our main method to allow for multiple messages to be processed.
  • run the application

Courtesy:


Comments

Popular posts from this blog

Powering Up Your Analytics: Exploring Power Query in Power BI

Power App Component Overview : Canvas App vs Model-Driven App

Exam PL-400: Microsoft Power Platform Developer