Inject your own activity type service
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
-
Create an interface and a concrete implementation for your service. The following example uses an
IExampleServiceinterface and anExampleServiceconcrete class:RequestResponseusing System; namespace Documentation.Examples { public interface IExampleService { void DoSomething(); } public class ExampleService() : IExampleService { public void DoSomething() { // Does something } } } -
Add the service interface to the constructor of your activity type and set a private service property as shown:
RequestResponseusing 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
-
Navigate to the following folder in the Automation Engine root:
\App_Data\Config\sitecore\MarketingAutomationTipIn a local instance, the Automation Engine is located in the
C:\path\to\xconnect\App_data\jobs\continuous\AutomationEnginefolder. -
Create a new XML file that extends
sc.MarketingAutomation.ActivityServices.xmland 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>NoteNode names must be unique - for example, there can only be one
Documentation.ExampleServicenode. The node name is up to you - it does not need to be the full namespace of the service.