Nov 25, 2016 Karthik AMR
Microsoft Dynamics CRM and Azure cloud together can bring great potential to any technology solution. This brings us to the point of integrating these two systems. We had previously shown you how to integrate Azure with Dynamics CRM employing the on premise listener. Apart from this method, we can also use the two way listener using Azure service runtime and Azure hosted cloud service to achieve the same integration. This would be another means to communicate to the Azure service bus from Dynamics CRM, and the two way listener can also respond with some processed values to the CRM system. This post looks to understand how to effectively integrate and use this combination to achieve superior results from your technology solution.
Microsoft Dynamics CRM is a feature rich SaaS platform whereas Azure – a diverse cloud platform with multiple powers grown as IaaS and PaaS. By integrating Dynamics CRM and Azure the enterprise can utilize SaaS, IaaS and PaaS. Without any further explanation, you can see how powerful this *-As-A-Service can turn out to be. With these power packed performances here we understand how to integrate both of them.
MS Dynamics CRM can be integrated with Azure by CRM’s Azure aware event execution pipeline (plug-ins) and Service bus of Azure.
Creating Service bus namespace and getting tokens
2. Create a new Service bus from left menu pane
4. Then select a resource group and region where it should be hosted
5. Then click on the create button to create a Service bus
6. Go to the created service bus namespace
8. Open the RootManageSharedAccessKey
Creating the Service Endpoint
1. Open up the Plug-in registration tool and connect to the CRM
2. On the Register button, select Register new Service End point
3. Paste the Connection string in the text field of the opened dialog
5. Once the Service end point is created, click on the Service Endpoint and go to the properties tab
6. Select the ServiceEndpointId – which is needed for the next step
Creating Azure aware Plug-in
Plug-in creation
1. In the plug-in Execute method, paste the following code
IOrganizationService service = null;
IPlugincontextcontext = executionContext.GetExtension<IPlugincontext>();
IOrganizationServiceFactory serviceFactory = executionContext.GetExtension<IOrganizationServiceFactory>();
service = serviceFactory.CreateOrganizationService(context.UserId);
IServiceEndpointNotificationService endpointService = executionContext.GetExtension<IServiceEndpointNotificationService>();
context.InputParameters[“Param”] = “From Plug-In”;
string output = endpointService.Execute(“serviceendpoint”, <serviceendpoint id>), context);
2. Register the assembly using Plugin registration tool
3. Fill in all the other values and create the Plug-in
namespace CTCloudServiceWorkerRole
{
public class WorkerRole : RoleEntryPoint, ITwoWayServiceEndpointPlugin
{
private readonly CancellationTokenSource cancellationTokenSource = new CancellationTokenSource();
private readonly ManualResetEvent runCompleteEvent = new ManualResetEvent(false);
private string OrganizationName;
private CancellationTokenSource _cancellationTokenSource;
public override void Run()
{
Trace.TraceInformation(“CloudServiceWorkerRole is running”);
try
{
this.RunAsync(this.cancellationTokenSource.Token).Wait();
}
finally
{
this.runCompleteEvent.Set();
}
}
public override bool OnStart()
{
// Set the maximum number of concurrent connections
ServicePointManager.DefaultConnectionLimit = int.MaxValue;
// For information on handling configuration changes
// see the MSDN topic at http://go.microsoft.com/fwlink/?LinkId=166357.
// Activate the Listener
CRMFacade.Instance.Init();
MetaDataCache.Instance.Init();
ActivateListener();
bool result = base.OnStart();
Trace.TraceInformation(“CloudServiceWorkerRole has been started”);
return result;
}
public void ActivateListener()
{
ServiceBusEnvironment.SystemConnectivity.Mode = ConnectivityMode.Http;
var appSettings = ConfigurationManager.AppSettings;
// Add service bus namespace
string serviceNamespace = appSettings[“serviceNamespace”];
// Add Default issuer name
string issuerName = appSettings[“issuerName”];
// Add Service bus Default Key
string issuerKey = appSettings[“issuerKey”];
string servicePath = “WorkerRole”;
// Leverage the Azure API to create the correct URI.
Uri address = ServiceBusEnvironment.CreateServiceUri(
Uri.UriSchemeHttps,
serviceNamespace,
servicePath);
// Create the shared secret credentials object for the endpoint matching the
// Azure access control services issuer
var sharedSecretServiceBusCredential = new TransportClientEndpointBehavior()
{
TokenProvider = TokenProvider.CreateSharedSecretTokenProvider(issuerName, issuerKey)
};
// Using an HTTP binding instead of a SOAP binding for this endpoint.
WS2007HttpRelayBinding binding = new WS2007HttpRelayBinding();
binding.Security.Mode = EndToEndSecurityMode.Transport;
// Create the service host for Azure to post messages to.
ServiceHost host = new ServiceHost(typeof(WorkerRole));
((ServiceBehaviorAttribute)host.Description.Behaviors[typeof(ServiceBehaviorAttribute)]).InstanceContextMode = InstanceContextMode.Single;
host.AddServiceEndpoint(typeof(ITwoWayServiceEndpointPlugin), binding, address);
// Create the ServiceRegistrySettings behavior for the endpoint.
var serviceRegistrySettings = new ServiceRegistrySettings(DiscoveryType.Public);
ServicePointManager.SetTcpKeepAlive(true, 1000, 1000);
ServicePointManager.Expect100Continue = false;
// Add the service bus credentials to all endpoints specified in configuration.
foreach (var endpoint in host.Description.Endpoints)
{
endpoint.Behaviors.Add(serviceRegistrySettings);
endpoint.Behaviors.Add(sharedSecretServiceBusCredential);
}
// Begin listening for messages posted to Azure.
host.Open();
}
public override void OnStop()
{
Trace.TraceInformation(“CloudServiceWorkerRole is stopping”);
this.cancellationTokenSource.Cancel();
this.runCompleteEvent.WaitOne();
base.OnStop();
Trace.TraceInformation(“CloudServiceWorkerRole has stopped”);
}
private async Task RunAsync(CancellationToken cancellationToken)
{
// TODO: Replace the following with your own logic.
while (!cancellationToken.IsCancellationRequested)
{
//Trace.TraceInformation(“Working”);
await Task.Delay(1000);
}
}
public string Execute(RemoteExecutionContext executionContext)
{
return (string)(executionContext.InputParameters[“Param”]);
}
}
}
Here you go with 2 ways to integrate Azure with Dynamics CRM.
Hope that helps!
What is Microsoft Power Pages? [Examples & Use Cases]
Oct 17, 2023
Expert Advice: Prerequisites for a Swift Migration from Salesforce to Dynamics 365
Oct 27, 2020
Don’t start off on the wrong foot; you deserve a better software partner
Jun 10, 2020
An MCSA who loves to solve problems, especially complex ones which makes me rise a notch in this tech game.
We have plans which will meet your needs, and if not we can tweak them around a bit too!