This page provides general guidelines for creating an Azure Worker Role for use with Dynamics CRM 2015.

Prerequisites


Create the Azure Worker Role Project

  1. Create folder(s) in source control to store the worker role. For example, in TFS, create folder contoso\MyIntegration\Trunk.
  2. In Visual Studio 2013 or 2015: Select File > New > Project.
  3. Select "Azure Cloud Service" from the Visual C# \ Cloud template folder.
  4. Set the .NET Framework to version 4.5.2 (or newer if supported by the CRM SDK)
  5. Set the name of the Cloud Service to something like MyAppCloudService. Adding "CloudService" at the end of the project name will help distinguish the cloud service project from the worker role(s) added to the solution. Also set the Location for the cloud service project.
  6. After creating the Cloud Service, Visual Studio prompts to create a Role for the service. Add a "Worker Role" to the solution and then set the name of the worker role (you can rename the project from the same dialog box. Name the worker role project to something like MyAppWorkerRole. Adding "WorkerRole" to the name of the project will help make the project type clear.
  7. Compile and run the project to verify that your machine is able to run the worker role successfully. Set a breakpoint in the OnStart method to verify that the application started without error. The stop the application.
  8. Check-in this base set of projects and code.

Add Microsoft Dynamics CRM SDK to the Worker Role project via NuGet

In order for the worker role application to work with the RemoteExecuteContext objects sent by Dynamics CRM, it's necessary to add the Dynamics CRM SDK to the worker role project. This can be done with NuGet within Visual Studio or by downloading the Dynamics SDK and adding the core assemblies. In NuGet, search for "crm 2015 core" to locate the NuGet package "Microsoft Dynamics CRM 2015 SDK core assemblies".

Dynamics CRM SDK Core assemblies:
  • Microsoft.Xrm.Sdk.dll
  • Microsoft.Crm.Sdk.Proxy.dll

Note: The assembly Microsoft.IdentityModel.dll is also added to the project via NuGet.

Add Microsoft Azure Service Bus to the Worker Role project via NuGet

The NuGet package to add to the project is named "Microsoft Azure Service Bus".

Add Code to the Worker Role class to Receive Messages from CRM


The Microsoft article "How to Use Service Bus queues" provides most of the information and code samples that's needed to add functionality to the worker role to receive messages from a Service Bus queue (where CRM will send messages of type RemoteExecuteContext through a configured Service Endpoint).

Your code will need to get the message in a way similar to the following:
RemoteExecutionContext crmExecutionContext = receivedMessage.GetBody<RemoteExecutionContext>();

Application Tracing

It's important to use the Trace methods in a worker role to facilitate debugging and troubleshooting.

The following code will create a tracing prefix that includes the class and method name. Using this prefix within tracing calls will also help pinpoint issues.

string tracePrefix = this.GetType().Name + "." + MethodBase.GetCurrentMethod().Name;
// Usage example:
Trace.TraceInformation("{0}: Begin", tracePrefix);





.