WF workflows are executed by hosting them in hosting applications using WF runtime. Sometimes we need data exchange between workflow and hosting application. Windows WF supports to communicate data in both direction. It is as simple as creating public properties in a class.
Passing Data to Workflow:
The data to workflow is sent using arguments when workflow object is instantiated. These arguments are sent using dictionary object. The workflow expects this as generic dictionary object with string keys and object values.
Dictionary<string, object> wfArguments = new Dictionary<string, object>();
wfArguments.Add("MyMessage", "From Hosting Application");
WorkflowInstance instance = workflowRuntime2.CreateWorkflow(typeof(
WFDynamicRuleUpdate.Workflow1), wfArguments);
We create a Sequential Workflow console application and name it as TestWorkflowParameters.
Designing Workflow:
Open Workflow1.cs in Workflow designer. Drag and drop a code activity to the designer window for the workflow.
Open workflow code and define a string property with the name of MyMessage.
public string MyMessage { get; set; }
Generate ExecuteCode handler for CodeActivity as follows:
private void codeActivity1_ExecuteCode(object sender, EventArgs e)
{
Console.WriteLine("Hello " + MyMessage);
MyMessage = "From Workflow";
}
Here we have updated the MyMessage argument. If we want we can get this value in the hosting application. To verify that we define the event handler for workflow completed event as follows:
Getting data from Workflow:
Workflow provides its public properties as output parameters. We can read the value of these properties using this OutputParameter collection. To discuss the most simple example let us define the WorkCompleted handler using lambda expression as follows:
workflowRuntime.WorkflowCompleted += (sender, arg) =>
{
waitHandle.Set();
Console.WriteLine("Hello " + arg.OutputParameters["MyMessage"].ToString());
};
When we run this application, it should show the following on the console:
Hello From Hosting Application
Hello From Workflow
In this discussion we discussed how easy it is to communicate between hosting application and workflow. We just have to define some public properties in the workflow and update their values. These values can be read by both workflow and hosting application.
No comments:
Post a Comment