1
votes

I am successfully posting messages from a CRM plugin to an Azure Service Bus Queue. Ideally I need CRM to listen to a Topic (subscription) and perform an action on receive. I do not know if this is possible with CRM and cannot find a method of implementing it. I can read from a queue with the below;

    MessagingFactory factory = MessagingFactory.CreateFromConnectionString(QueueConnectionString);

    //Receiving a message
    MessageReceiver testQueueReceiver = factory.CreateMessageReceiver(QueueName);
    while (true)
    {
        using (BrokeredMessage retrievedMessage = testQueueReceiver.Receive())
        {
            try
            {
                var message = new StreamReader(retrievedMessage.GetBody<Stream>(), Encoding.UTF8).ReadToEnd();
                retrievedMessage.Complete();
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.ToString());
                retrievedMessage.Abandon();
            }
        }
    }

However this gets called when a plugin is executed by a user action. I need to be always listening. Can this be achieved with CRM? I am using CRM 2016 on premise, with the message bus hosted in Azure.

Thanks for any pointers.

2

2 Answers

2
votes

CRM is not an always-listening application. It is a web application triggered by an HTTP request. To bridge this gap you can use Azure Functions. Since an Azure Function can be triggered by the Service Bus and can call Dynamics CRM.

You will need to create an deploy an Azure Function that is triggered by a Service Bus queue. This is well documented at: https://docs.microsoft.com/en-us/azure/azure-functions/functions-bindings-service-bus

Create a package.json for your Azure Function:

{
"bindings": [
    {
    "queueName": "testqueue",
    "connection": "MyServiceBusConnection",
    "name": "myQueueItem",
    "type": "serviceBusTrigger",
    "direction": "in"
    }
],
"disabled": false
}

Basic Function code:

public static void Run(string myQueueItem, TraceWriter log)
{
    log.Info($"C# ServiceBus queue trigger function processed message: {myQueueItem}");
}

Once you have this working you can build out your Azure Function code to connect to Dynamics CRM - same as you would any other code.

Add dependencies to project.json (must get this exactly correct as discussed here: http://crmtipoftheday.com/2016/12/12/connect-to-dynamics-365-in-azure-functions/):

{
  "frameworks": {
    "net46":{
      "dependencies": {
        "Microsoft.CrmSdk.CoreAssemblies": "8.2.0",
        "Microsoft.CrmSdk.XrmTooling.CoreAssembly": "8.2.0"
      }
    }
  }
}
0
votes

Nick suggestion is perfect. Another way would be to use Logic App. There is a connector available for Dynamics 365 which allow you to perform crud operations in graphical interface. You cannot write code in Logic App where as in Azure functions you can implement your logic using code. Logic App is similar to workflows in Dynamics 365 and has the ability to read context from a queue or subscription.