Blog

Two-Way Azure Aware Plugins

– 4 Minutes

Azure-aware plugins can be used to run custom business logic on-premises and have the responses returned to Dynamics 365. This allows you to access internal integrate Dynamics 365 with your internal line-of-business applications. For example, you can save an Order in CRM, have custom logic that creates the order in your on-premises ERP (Dynamics GP, NAV, AX, etc.) and returns the order number to CRM in real-time. We'll walk through that example below.

First, we will create the plugin that will send the information to the Azure Service Bus.  This is using my base Plugin class (which maybe I'll write about someday).

namespace BGuidinger.Samples
{
    using Microsoft.Xrm.Sdk;
    using System;

    public class TwoWayPlugin : Plugin
    {
        public TwoWayPlugin(string unsecure, string secure) : base(unsecure, secure) { }

        public override void OnExecute(IPluginProvider provider)
        {
            var endpointId = Guid.Parse(UnsecureConfig.GetValue("endpointId"));
            var endpoint = new EntityReference("serviceendpoint", endpointId);

            var response = provider.ServiceEndpoint.Execute(endpoint, provider.ExecutionContext);

            provider.LoggingService.Write($"Response: {response}");
        }
    }
}

We can use the Plugin Registration tool to register this plugin on any message/entity we want.  First though, we must create a Service Endpoint.  To do this, copy the connection string for your Azure Service Bus.  Since we are creating a two-way plugin, set the Designation Type to TwoWay.  Also, you must change the protocol from sb to https.

Service Endpoint Registration

After you register the Service Endpoint, go to the properties and copy the ServiceEndpointId.

Service Endpoint Properties

With this, we can now register the plugin. Create a new step for create of salesorder. In the unsecure configuration, but the endpoint ID you copied above.

Next, we will create an endpoint plugin which will run some action and return a string to the plugin.

namespace BGuidinger.Samples
{
    using Microsoft.Xrm.Sdk;
    using System.ServiceModel;

    [ServiceBehavior]
    public class TwoWayEndpoint : ITwoWayServiceEndpointPlugin
    {
        public string Execute(RemoteExecutionContext context)
        {
            // TODO: Implement custom logic here.
            return "ORD1234567";
        }
    }
}

Next, we need to create an Azure WCF Relay which listens for messages to be posted to the Azure Service Bus and executes the endpoint plugin.  This code could be run from either a console application or a Windows Service.

...
private static ServiceHost _host;

private static void Start(string[] args)
{
    _host = new ServiceHost(typeof(TwoWayEndpoint));
    _host.Open();
}

private static void Stop()
{
    _host.Close();
}
...

In order for the ServiceHost to know where to connect, we must put this in the App.config:

<system.serviceModel>
    <services>
        <service name="BGuidinger.Samples.TwoWayEndpoint">
            <endpoint
                contract="Microsoft.Xrm.Sdk.ITwoWayServiceEndpointPlugin"
                binding="ws2007HttpRelayBinding"
                address="https://bguidinger.servicebus.windows.net/"
                behaviorConfiguration="serviceBusBehvior"/>
        </service>
    </services>
    <behaviors>
        <endpointBehaviors>
            <behavior name="serviceBusBehvior">
                <transportClientEndpointBehavior>
                    <tokenProvider>
                        <sharedAccessSignature
                            keyName="RootManageSharedAccessKey"
                            key="YOUR_KEY_GOES_HERE="/>
                    </tokenProvider>
                </transportClientEndpointBehavior>
            </behavior>
        </endpointBehaviors>
    </behaviors>
</system.serviceModel>

All we need to do now is run the WCF Relay and create an order in CRM. We should see the Order ID get set once the order is saved!:

Comments

No comments.