Workflow Foundation 4.0 is introduced a significant amount of change from the previous versions of the technology. This article will be the third in a series of articles. In this article we will be discussing about how to pass data from host application to workflow without using bookmarks.
Part I : Introduction to Custom Activity Development
Part II : Bookmark
Part III : WorkflowDataContext
WorkflowDataContext
As specified in Part II, we can pass data from host application to custom activities using Bookmarks. Bookmarks are event driven mechanism. Whenever we define a bookmark, workflow will go for idle state and wait for the resumption of the bookmark.
Here, we will discuss about another way of passing common data to all child activities defined in a workflow. If you want to pass some common data to all the activities defined in the workflow, we can use the WorkflowDataContext associated with the workflow.
Custom Activity
In Custom Activity, we can get the workflow level data using WorkflowDataContext. Properties associated with the WorkflowDataContext represent the workflow level Arguments and variables.
In the following example, we extracted the properties from WorkflowDataContext and assigned to our OutArgument called DataFromHost.
public class CustomActivity:NativeActivity
{
protected override bool CanInduceIdle
{
get
{
return true;
}
}
public OutArgument<string> DataFromHost
{
get;
set;
}
protected override void Execute(NativeActivityContext context)
{
string dataFromHost = string.Empty;
WorkflowDataContext dataContext = context.DataContext;
PropertyDescriptorCollection propertyDescriptorCollection = dataContext.GetProperties();
foreach (PropertyDescriptor propertyDesc in propertyDescriptorCollection)
{
dataFromHost += propertyDesc.Name + " : " + propertyDesc.GetValue(dataContext) + "\n";
}
DataFromHost.Set(context, dataFromHost);
}
}
WorkFlow
Sample workflow using our CustomActivity is defined as
Host Application
Define a dictionary for passing data from host to workflow. Add key value pairs to the dictionary. The keys should be same as the InArguments defined in the workflow. In our case, it will be data1 and data2.
Dictionary<string, object> Data = new Dictionary<string, object>();
Data.Add("data1", "Hello from Host.");
Data.Add("data2", "Color.Blue");
WorkflowApplication workflowApp =
new WorkflowApplication(new TestWorkflow(), Data);
workflowApp.Run();
Console.ReadLine();
Dictionary of data is specified along with the workflow object for creating the WorkflowApplication. Dictionary values are added to the WorkflowDataContext and this can be accessed by any child Activity using the context.DataContext.
Result
You may be noted that, even though we passed only two values, data1 and data2, it displaying a third key called dataFromHost. As I specified, WorkflowDataContext contains all Arguments and Variables defined in workflow level. dataFromHost is the variable defined in the Workflow for handling output from our CustomActivity.
Conclusion
Here, we discussed about another way of passing data from host application to workflow. This method is used for passing common value to all child activities defined in the workflow.