Workflow Foundation 4.0 is introduced a significant amount of change from the previous versions of the technology. In WF4.0, we are using Extensions for communication between the host and workflow, the workflow and its child activities and between child activities. There are two types of extensions in WF4.0, in this article we will look into the normal extensions.
Extension
Extension is a normal class with public methods and properties. Once we add the extension class to the workflow application, we can use the same in any child activities. For each instance of the workflow application will have one instance of its extension class. Using the context associated with the custom activity, we can access the extensions.
Sample Extension
For our sample, I am using a simple extension with one property and a Method. The dictionary property is used to pass data from host application to child activity. Same is used for communicating to another child activity.
public class SimpleExtension
{
private Dictionary<string, object> data;
public Dictionary<string, object> Data
{
get
{
if (data == null)
{
data = new Dictionary<string, object>();
}
return data;
}
}
public void AddData(string key, object value)
{
if (string.IsNullOrEmpty(key))
return;
Data.Add(key, value);
}
}
Custom Activity to Access the Extension
Here, we are creating a custom activity, which will access the extension and pass values to the extension property.
Our custom activity will have an InArgument called Message. Custom activity will access the extension using the Context object. In the following code, we are extracting the data passed by the host application and combining the same with the Message of the custom activity and updating the same back to the extension.
public class CustomActivity:NativeActivity
{
public InArgument<string> Message
{
get;
set;
}
protected override void Execute(NativeActivityContext context)
{
SimpleExtension extensionObj = context.GetExtension<SimpleExtension>();
if (extensionObj != null)
{
// extract the data added from host application
object data=null;
if (extensionObj.Data.ContainsKey("Host"))
{
data = extensionObj.Data["Host"];
}
//Pass data to the extension
extensionObj.AddData(this.DisplayName, Message.Get(context)+data);
}
}
}
Define the Workflow
Now we are creating a new workflow using the custom activity. Our sample workflow will look like
Attaching to Workflow Application
After creating the workflow application object, add the extension object to the workflow application’s Extensions collection. The code used by the Host application is
static void Main(string[] args)
{
// Create workflowapplication object
WorkflowApplication wfApp = new WorkflowApplication(new SampleWorkflow());
//Create the extension object
SimpleExtension extensionObj = new SimpleExtension();
extensionObj.AddData("Host", " Host Application Data ");
//Attach the extension to the workflow application
wfApp.Extensions.Add(extensionObj);
//When the workflow complete, display the data from extension
wfApp.Completed=delegate(WorkflowApplicationCompletedEventArgs a)
{
foreach (string key in extensionObj.Data.Keys)
{
Console.WriteLine("{0} : {1}", key, extensionObj.Data[key].ToString());
}
};
// Start the workflow
wfApp.Run();
Console.Read();
}
Result
From the result screen, you can observe the messages from both the custom activities and host application. Custom activity message is appended by the host application message.
Adding Extensions from Custom Activity
Extensions can be added using the CacheMetadata of the custom activity. In the following example, we add a second extension to our workflow using the custom activity.
protected override void CacheMetadata(NativeActivityMetadata metadata)
{
base.CacheMetadata(metadata);
metadata.AddDefaultExtensionProvider(() => new SecondExtension());
}
Conclusion
Here, we discussed about the Extension used in custom activity development in Windows Workflow Foundation 4.0. Extensions are used for communication between the host and workflow, communication between the workflow and its child activities and communication between child activities.