Workflow Foundation 4.0 is introduced a significant amount of changes from the previous versions of the technology. We already discussed about various features of Workflow, data model and persistence. In this article we will discuss more about the host application, how we can invoke a workflow and about the various workflow application actions which we can handle in host application.
We can execute a workflow from host application using either WorkflowInvoker or workflowapplication.
WorkflowInvoker
If you want to invoke a simple workflow, use the WorkflowInvoker. But, if you need to use any of the workflow features like Extensions, Persistence, Actions, etc. use the WorkflowApplication.
WorkflowInvoker.Invoke(new Workflow1());
We can also pass set of InArgument values to the workflow and set a timeout for the workflow execution using the WorkflowInvoker.
WorkflowApplication
When you want to use any of the workflow features like Persistence, Extensions, Actions, etc. use the WorkflowApplication for executing the workflow.
WorkflowApplication wfApp = new WorkflowApplication(new Workflow1());
We already discussed about Extensions and Persistence. Here, let us look more on different actions available in WorkflowApplication and how to handle the same.
WorkflowApplication's Actions
WorkflowApplication support many actions for communication with Host application.
Completed
Completed action will be fired, when the workflow complete its execution, cancelled its execution, thrown some exception and went to faulted state. We can distinguish the state of the workflow application using the CompletionState of WorkflowApplicationCompletedEventArgs.
We can retrieve the OutArgument data using the Completed Action. In our sample, I am defining an inline Completed action, which will check the state of the workflow. If the workflow is completed, extract the OutArgument data and loop through the values.
Host Application
static void Main(string[] args)
{
WorkflowApplication wfApp = new WorkflowApplication(new Workflow1());
wfApp.Completed = (completedArgs) =>
{
if (completedArgs.CompletionState == ActivityInstanceState.Closed)
{
IDictionary<string, object> result = completedArgs.Outputs;
foreach (string s in result.Keys)
{
Console.WriteLine(result[s].ToString());
}
}
};
wfApp.Run();
Console.Read();
}
Workflow
For verifying the OutArguments, defined two arguments and assigned values for the same in workflow.
Result
When the workflow completes, it display the values of all OutArguments.
Handle Faulted Workflow
Uaing the Completed action, we can handle the faulted and Cancelled state of the workflow.
wfApp.Completed = (completedArgs) =>
{
if (completedArgs.CompletionState == ActivityInstanceState.Closed)
{
IDictionary<string, object> result = completedArgs.Outputs;
foreach (string s in result.Keys)
{
Console.WriteLine(result[s].ToString());
}
}
else if (completedArgs.CompletionState == ActivityInstanceState.Faulted)
{
Console.WriteLine("Workflow Faulted");
}
};
As we added the TerminateWorkflow activity in the middle of the workflow, the workflow will move to faulted state.
WorkflowApplication have following actions along with Completed action.
Aborted
When the workflow instance is aborted, this action will be called.
wfApp.Aborted = WorkflowAborted;
===============================
void WorkflowAborted(WorkflowApplicationAbortedEventArgs args)
{
Console.WriteLine(args.Reason);
}
Idle
When the workflow goes into idle state, the Idle action will be executed. For synchronizing the Bookmark resumption, we need to handle this action.
Unloaded
This action will get invoked when a workflow instance is unloaded. Unload method triggers this action.
Conclusion
Here, we discussed about the various actions available as part of the workflowapplication and how we can handle the same in Host application. Actions will enable the tracking of workflow state and communicate the same to Host application.