Commerce runtime extensibility and triggers

Important

This topic applies to Dynamics 365 for Retail, Dynamics 365 for Finance and Operations, Enterprise edition, and Dynamics 365 for Talent.

This article explains trigger support for the Dynamics 365 for Retail 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.

CRT trigger interface

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

  1. Implement IRequestTrigger.
  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.
/// <summary>
/// The interface for request trigger.
/// </summary>
public interface IRequestTrigger
{
    /// <summary>
    /// Gets the collection of request types supported by this trigger.
    /// </summary>
    /// <remarks>If null or empty collection returned trigger will be called for all request.</remarks>
    IEnumerable<Type> SupportedRequestTypes { get; }
    /// <summary>
    /// Invoked before request has been processed by <see cref="IRequestHandler"/>.
    /// </summary>
    /// <param name="request">The incoming request message.</param>
    void OnExecuting(Request request);
    /// <summary>
    /// Invoked after request has been processed by <see cref="IRequestHandler"/>.
    /// </summary>
    /// <param name="request">The request message processed by handler.</param>
    /// <param name="response">The response message generated by handler.</param>
    void OnExecuted(Request request, Response response);
}

Trigger CommerceRunTime.config updates

When you extend the CRT, make sure that you always write your extension in your own assembly. After you write the trigger extension in your assembly, you must add an entry in the composition section of the .config file for the CRT, so that the trigger is loaded at run time. The following example shows a .config file that includes an entry for a trigger implementation in the CRTExtensionTrigger assembly. CRTExtensionTrigger