Inject your own activity type service

Version: 10.4

You can use your own service in the context of an activity type. You must register custom services  in configuration and inject them in the activity type constructor. 

Create a service and add it to the activity type constructor

  1. Create an interface and a concrete implementation for your service. The following example uses an IExampleService interface and an ExampleService concrete class:

    RequestResponse
    using System;
    
    namespace Documentation.Examples
    {
        public interface IExampleService
        {
            void DoSomething();
        }
    
        public class ExampleService() : IExampleService
        {
            public void DoSomething()
            {
                // Does something
            }
        }
    }
    
  2. Add the service interface to the constructor of your activity type and set a private service property as shown:

    RequestResponse
    using Sitecore.Xdb.MarketingAutomation.Core.Activity;
    using Sitecore.Xdb.MarketingAutomation.Core.Processing.Plan;
    using Microsoft.Extensions.Logging;
    
    namespace Documentation.Examples
    {
        public class DoNothingActivity : IActivity
        {
            private IExampleService ExampleService { get; set; }
    
            public IActivityServices Services { get; set; }
    
            public DoNothingActivity(IExampleService exampleService)
            {
                ExampleService = exampleService;
            }
    
            public ActivityResult Invoke(IContactProcessingContext context)
            {
                ExampleService.DoSomething();
    
                return new SuccessMove();
            }
        }
    }
    

Add the service to the configuration

  1. Navigate to the following folder in the Automation Engine root: \App_Data\Config\sitecore\MarketingAutomation

    Tip

    In a local instance, the Automation Engine is located in the C:\path\to\xconnect\App_data\jobs\continuous\AutomationEngine folder.

  2. Create a new XML file that extends sc.MarketingAutomation.ActivityServices.xml and registers your custom services:

    RequestResponse
    <Settings>
    <!--
        Marketing Automation Activity Services configuration
    -->
    <Sitecore>
        <XConnect>
        <MarketingAutomation>
            <Engine>
            <Services>
                <Documentation.ExampleService>
                    <Type>Documentation.Examples.ExampleService, Documentation.Examples</Type>
                    <As>Documentation.Examples.IExampleService, Documentation.Examples</As>
                    <LifeTime>Singleton</LifeTime>
                </Documentation.ExampleService>
            </Services>
            </Engine>
        </MarketingAutomation>
        </XConnect>
    </Sitecore>
    </Settings>
    
    Note

    Node names must be unique - for example, there can only be one Documentation.ExampleService node. The node name is up to you - it does not need to be the full namespace of the service.

Do you have some feedback for us?

If you have suggestions for improving this article,