Creating an Activity at Runtime with DynamicActivity
This topic applies to Windows Workflow Foundation 4 (WF4).
DynamicActivity is a concrete, sealed class with a public constructor. DynamicActivity can be used to assemble activity functionality at runtime using an activity DOM.
DynamicActivity Features
DynamicActivity has access to execution properties, arguments and variables, but no access to run-time services such as scheduling child activities or tracking.
Top-level properties can be set using workflow Argument objects. In imperative code, these arguments are created using CLR properties on a new type. In XAML, they are declared using x:Class
and x:Member
tags.
Activities constructed using DynamicActivity interface with the designer using ICustomTypeDescriptor. Activities created in the designer can be loaded dynamically using Load, as demonstrated in the following procedure.
To create an activity at runtime using imperative code
OpenVisual Studio 2010.
Select File, New, Project. Select Workflow 4.0 under Visual C# in the Project Types window, and select the v2010 node. Select Sequential Workflow Console Application in the Templates window. Name the new project DynamicActivitySample.
Right-click Workflow1.xaml in the HelloActivity project and select Delete.
Open Program.cs. Add the following directive to the top of the file.
using System.Collections.Generic;
Replace the contents of the
Main
method with the following code, which creates a Sequence activity that contains a single WriteLine activity and assigns it to the Implementation property of a new dynamic activity.//Define the input argument for the activity var textOut = new InArgument<string>(); //Create the activity, property, and implementation Activity dynamicWorkflow = new DynamicActivity() { Properties = { new DynamicActivityProperty { Name = "Text", Type = typeof(InArgument<String>), Value = textOut } }, Implementation = () => new Sequence() { Activities = { new WriteLine() { Text = new InArgument<string>(env => textOut.Get(env)) } } } }; //Execute the activity with a parameter dictionary WorkflowInvoker.Invoke(dynamicWorkflow, new Dictionary<string, object> { { "Text", "Hello World!" } }); Console.ReadLine();
Execute the application. A console window with the text “Hello World!” displays.
To create an activity at runtime using XAML
Open Visual Studio 2010.
Select File, New, Project. Select Workflow 4.0 under Visual C# in the Project Types window, and select the v2010 node. Select Workflow Console Application in the Templates window. Name the new project DynamicActivitySample.
Open Workflow1.xaml in the HelloActivity project. Click the Arguments option at the bottom of the designer. Create a new In argument called
TextToWrite
of type String.Drag a WriteLine activity from the Primitives section of the toolbox onto the designer surface. Assign the value TextToWrite to the Text property of the activity.
Open Program.cs. Add the following directive to the top of the file.
using System.Activities.XamlIntegration;
Replace the contents of the
Main
method with the following code.Activity act2 = ActivityXamlServices.Load(@"Workflow1.xaml"); results = WorkflowInvoker.Invoke(act2, new Dictionary<string, object> { { "TextToWrite", "HelloWorld!" } }); Console.ReadLine();
Execute the application. A console window with the text “Hello World!” appears.
Right-click the Workflow1.xaml file in the Solution Explorer and select View Code. Note that the activity class is created with
x:Class
and the property is created withx:Property
.
See Also
Tasks
Concepts
Authoring Workflows, Activities, and Expressions Using Imperative Code