How it works...

First, we attached a step to our Azure Service Bus endpoint that triggers when a record is retrieved. The event is triggered when a record is opened through the user interface, or when the record is retrieved using the Dynamics 365 SDK, or even when using the web API. This leverages the built-in Dynamics 365 Azure-aware plugin to write the plugin's context to the Azure endpoint.

In step 2 to step 5, we created a console application that consumes the execution context from the queue and stores the details in a NoSQL database.

Make sure you replace the values in step 5 with your specific ones. The first four parameters are endpointUri, primaryKey, databaseName, and collectionName related to your Cosmos DB instance (for guidance on where to get the values, read the Getting ready section of this recipe). ConnectionString is related to your Azure Service Bus queue's connection string (the Consuming messages from an Azure Service Bus recipe in Chapter 5, External Integration, describes how to get the connection string).

This abridged recipe does not cover exception handling; it is at your discretion to enhance the consumer's code to deal with exceptions.

As we saw in the Consuming messages from an Azure Service Bus recipe earlier in this chapter, the OnMessage method listens to the queue's events (triggered when the message is queued) and calls a delegate method to execute some logic. In our case, it sends a JSON-formatted message with the read details to a Cosmos DB database.

The remote execution context contains details about who made the call, details about the query, as well as the returned result set (attribute values). For example, if the record name is included, you can retrieve it using the following:

((Entity)result.OutputParameters.FirstOrDefault().Value).Attributes["name"] 

One thing to note: when we are writing the message back to Cosmos DB, we are using an asynchronous method synchronously; best practice would dictate that you turn your method asynchronously as well. For simplicity, we've opted to use Wait() instead.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset