Redaguoti

Bendrinti naudojant


Commerce runtime (CRT) extensibility and triggers

This article explains trigger support for the Dynamics 365 commerce runtime (CRT). CRT supports pre-triggers and post-triggers for every request.

CRT trigger overview

Commerce runtime (CRT) triggers give you a way to extend the CRT workflow, and let you add business logic before and after every CRT request is executed. The following two methods are used:

  • OnExecuting – This method is invoked before a request has been processed by a corresponding IRequestHandler implementation.
  • OnExecuted – This method is invoked after the request has been processed by a corresponding IRequestHandler implementation.

Note

If you extend a CRT request that is used by a high-volume API (for example, Carts/AddCartLines or Products/GetByIds), and your extension needs to read from the channel database, Microsoft recommends that you enable the cache for the database reading. Otherwise, the extension will consume too much Retail Server and channel database resources, which can cause overall Commerce Scale Unit (CSU) performance issues that impact your business.

CRT trigger extension

To implement a trigger, you must complete these tasks, as shown in the code example that follows:

  1. Implement IRequestTriggerAsync.
  2. Specify SupportedRequestTypes to define the request types that the trigger must be executed for.
  3. Write a trigger implementation in the OnExecuting method if business logic must be run before the request is addressed.
  4. Write a trigger implementation in the OnExecuted method if business logic must be run after the request is addressed.

Sample trigger implementation for Get customer data request:


        using Microsoft.Dynamics.Commerce.Runtime;
        using Microsoft.Dynamics.Commerce.Runtime.DataServices.Messages;
        using Microsoft.Dynamics.Commerce.Runtime.Messages;
        using System;
        using System.Collections.Generic;
        using System.Threading.Tasks;

        public class GetCustomerTriggers : IRequestTriggerAsync
        {
            /// <summary>
            /// Gets the supported requests for this trigger.
            /// </summary>
            public IEnumerable<Type> SupportedRequestTypes
            {
                get
                {
                    return new[] { typeof(GetCustomerDataRequest) };
                }
            }

            /// <summary>
            /// Post trigger code.
            /// </summary>
            /// <param name="request">The request.</param>
            /// <param name="response">The response.</param>
            public async Task OnExecuted(Request request, Response response)
            {
                //Custom logic

            // The only stub to handle async signature
                await Task.CompletedTask;
            }

            /// <summary>
            /// Pre trigger code
            /// </summary>
            /// <param name="request">The request.</param>
            public async Task OnExecuting(Request request)
            {
                // custom logic
                await Task.CompletedTask;
            }
        }

Register the extension

Copy and paste the extension library to ...\RetailServer\webroot\bin\ext folder and update the commerceRuntime.ext.config file with the custom extension library information under composition section. In this example, Contoso.Commerce.Runtime.Services is the custom extension name.

<add source="assembly" value="Contoso.Commerce.Runtime.Services" />

For the CRT extension to work in offline mode, update ...\Microsoft Dynamics 365\70\Retail Modern POS\ClientBroker\ext\CommerceRuntime.MPOSOffline.ext.config with the extension library information under the composition section. Then copy and paste the extension library to ...\Microsoft Dynamics 365\70\Retail Modern POS\ClientBroker\ext.

Debugging CRT

To debug CRT from POS, attach the CRT extension project to the w3wp.exe (IIS process for Retail server) when POS is connected to Retail server. For offline mode, attach the CRT extension project to the dllhost.exe process.