We create first a console application that will be the host of our workflow. After, we create a custom activity, and finally, we will use an extension (a business service) called from our custom activity.
Create a workflow Console Application in Visual studio 2010 (be sure that the target framework is 4.0 😉
Click ok : Visual studio 2010 create a console application and call your workflow in the main through Invoke method of WorkflowInvoker class. On the solution explorer,You also see the xaml file of your workflow (xml file and code behind that defines your workflow foundation) named workflow1 by default
rename the workflow by right clicking on Workflow1.xaml, and call it “SampleWorkflow” :
double-click on the xaml file and you will see the workflow designer.Go the properties panel on the bottom right side and change the name of our workflow to “SampleWorkflow”
now rename the workflow instantiated in the main method:
Create a custom activity:
Add a new item to the solution:
select class item and call it MyCustomActivity:
Make this class public and inherit from CodeActivity interface ( add System.Activities namespace). Override the execute method with a simple console print:
now, double click on SampleWorkflow xaml file and in the worklfow designer you will see that our custom activity exists in the toolbox :
drop it in the designer:
and run it, your custom activity works!
Create a local service BusinessService and its interface to make business class in it:
and its interface:
Modify program.cs file in order to use workflowApplication instead of WorkflowInvoker (we will add services called extensions in Workflow Foundation words)
the AutoResetEvent allows host to wait the completion of the workflow.
we add the businessService object using Extensions collection of WorkflowApplication: by this way, all activities can get from their context the extension – that is the service – from WorkflowApplication.
the completed event is added to set the AutoResetEvent at true when the workflow is finished
Modify the custom activity to call our service that is extension in using GetExtension method of CodeActivityContext
It is interesting to use extension in workflow in order to make business in service and not directly in our custom activity. It simplifies unit testing focus on business and not Workflow infrastructure ( that is test at level activity instead logic business).