WorkflowService Configuration Based Extensions
Note: This feature is now released in Microsoft.Activities v1.8.5
Activities can access extensions using the
But how do extensions get added to the extensions collection? There are two options provided by WF4
- Add the extension to the
- The activity can use the
ActivityMetadata.AddDefaultExtensionProvider<T> property to provide a Func<T> which will create the extension if necessary
However, there are cases where either option is not sufficient.
- WorkflowServices do not have a way for you to add to the
- There may be cases where you want to provide a different extension than the activity would create for itself using the AddDefaultExtensionProvider.
Add an abitrary extension
A workflow service uses activities which require an extension but do not provide a Default Extension Provider. The developer can at runtime specify zero or more extension types in web.config which will be created and added to the extensions collection
Dependency Injection via Extension
A developer who wants to use Dependency Injection uses a
Common Service Locator
which is added to the extensions collection. At runtime activities which require an extension can obtain it via the DI container of their choice.
Suppose you have an activity which requires an extension. In
the activity indicates that it requires an extension by calling
public class ActivityExtensionTest : CodeActivity<bool>
protected override void CacheMetadata(CodeActivityMetadata metadata)
protected override bool Execute(CodeActivityContext context)
var extension = context.GetExtension<TestExtension>();
return extension != null;
If you create a WorkflowService which uses this activity and try to load it you will get an error.
Test method Microsoft.Activities.Tests.WorkflowExtensionsBehaviorTest.WorkflowExtensionsBehaviorAddsExtension threw exception:
System.Activities.ValidationException: An extension of type 'Microsoft.Activities.Tests.TestExtension' must be configured in order to run this workflow.
This service behavior solves this problem by allowing you to define in configuration the extension types you want to create.
To use this behavior
- Modify your config file to add the necessary elements to provide the configuration
In this example, we are adding an extension Microsoft.Activities.Tests.TestExtension to the WorkflowService
<serviceDebug includeExceptionDetailInFaults="True" />
<!-- The WorkflowExtensionsBehavior allows you to add extensions -->
<add extension="Microsoft.Activities.Tests.TestExtension, Microsoft.Activities.Tests" />
<!-- Must register the extension behavior -->
<add name="workflowExtensions" type="Microsoft.Activities.ServiceModel.WorkflowExtensionsElement, Microsoft.Activities, Version=188.8.131.521, Culture=neutral, PublicKeyToken=23b0c89d0d5ad43f"/>