Workflow Foundation 4.0 - WorkflowApplication Actions

Ambily.raj
Posted by in WWF category on for Intermediate level | Points: 250 | Views : 15987 red flag
Rating: 5 out of 5  
 2 vote(s)

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.

Page copy protected against web site content infringement by Copyscape

About the Author

Ambily.raj
Full Name: Ambily KK
Member Level: Silver
Member Status: Member,Microsoft_MVP,MVP
Member Since: 5/18/2010 1:05:25 AM
Country: India
Thanks Ambily K K http://ambilykk.com/
http://ambilykk.com/
I have over 9 years of experience working on Microsoft Technologies. I am carrying the passion on Microsoft technologies specifically on web technologies such as ASP .Net and Ajax. My interests also include Office Open XML, Azure, Visual Studio 2010. Technology adoption and learning is my key strength and technology sharing is my passion.

Login to vote for this post.

Comments or Responses

Posted by: Mahmah2 on: 10/20/2013 | Points: 25
Thanks for the article.

Is it possible to get argument values when a workflow gets into an idle state ?


Login to post response

Comment using Facebook(Author doesn't get notification)