Azure Service Bus Topic subscribe from CRM plugin

非 Y 不嫁゛ 提交于 2019-12-24 08:47:02

问题


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.


回答1:


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"
      }
    }
  }
}



回答2:


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.



来源:https://stackoverflow.com/questions/42490316/azure-service-bus-topic-subscribe-from-crm-plugin

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!