Click here to Skip to main content
12,998,759 members (68,568 online)
Click here to Skip to main content
Add your own
alternative version


53 bookmarked
Posted 9 Jun 2012

Best Practice Architecture with Workflow Foundation (WF)

, 10 Jun 2012
Rate this:
Please Sign up or sign in to vote.
This article demonstrates how WF can be consumed as a business layer.


When to go for Workflow Foundation

  1. For long-running business process.
  2. Frequently Changing Business Logic or Rules.
  3. When need visual model/execution of the process.


In this post I am going to demonstrate how you can architect enterprise application using WWF. You can find lots of learning stuff for Window Workflow Foundation but difficult to find is how to adjust and where to adjust it in architecture.

<shapetype id="_x0000_t75" coordsize="21600,21600" filled="f" o:preferrelative="t" o:spt="75" path="m@4@5l@4@11@9@11@9@5xe" stroked="f" /><stroke joinstyle="miter" /><path gradientshapeok="t" o:connecttype="rect" o:extrusionok="f" /><lock aspectratio="t" v:ext="edit" /></path /></stroke /></shapetype />

Architecture diagram

Have a look to architecture diagram, in this UI is interacting with business layer via WCF service. WF is used as part of business layer. You can also put WF service in front of WCF but then you will not know the entities of WCF in UI. As per best practice nothing should be directly exposed to UI layer, so it is not good idea to put WF as library in UI Layer. Generally people use WF for business rules, State persistence, Transaction or for some kind of time taking process so I would suggest to put it in Business layer. In my attached sample, for simplicity I am not involving entity framework and DB part. But off course you can add Entity framework with POCO classes. There are lot of articles are available on this so I don’t think you should face any kind of difficulty in implementation. In future I shall also come up with some sample on this.


To implement this application, I am going to use the following technologies:

To implement this project person should have basic knowledge of .net technologies like C#, ASP.NET, WCF, WF.

Sample Proof of Concept

ADD blank solution name as ‘BestPracticeArchitectureWWF’.

Business Layer (C# Library)

Add C# library project to existing solution. Add following code for method in ‘Class1.cs’. This method will take one parameter and return it with some modification. This is the place where you can call to database by using entity framework.

public static string BusinessMethod(string parm1) 
    return parm1 + " Returned by Business layer.";

Business Layer (WF Activity)

Add WF Activity library project to existing solution.

In Activity1.xaml add flowchart.

Drag Sequence to indicated place.

Select Sequence and declare 2 arguments in argument tab as shown in screen shot. One is to accept argument and another to return.

Now drag two more activities to Sequence as shown in above screen shot. One is Assign activity and another is Invoke Method from Primitive group panel. In Assign activity will assign Argument2 variable to Argument1 + “Modified in WF”. Invoke method is used to call business C# library. Add reference to C# library. To call library method set Target Type as class. Method name simply type ‘BusinessMethod’. No need to fill TargetObject(In TargetType and TargetObject one need to be filled). One strange thing is even though you work in c# project in Activities expression it takes only vb code.

Finally XAML of this activity will look like this. No need to modify anything in XAML but some time you rename something it fails to change in XAML then we need to look into it.

<Activity mc:Ignorable="sap" x:Class="ActivityLibrary1.Activity1" 






















<x:Property Name="argument1" Type="InArgument(x:String)" />
<x:Property Name="argument2" Type="OutArgument(x:String)" />
<mva:VisualBasic.Settings>Assembly references and imported namespaces 
             for internal implementation</mva:VisualBasic.Settings>
<Flowchart sad:XamlDebuggerXmlReader.FileName=

<scg3:Dictionary x:TypeArguments="x:String, x:Object">
<x:Boolean x:Key="IsExpanded">False</x:Boolean>
<av:Point x:Key="ShapeLocation">270,2.5</av:Point>
<av:Size x:Key="ShapeSize">60,75</av:Size>
<av:PointCollection x:Key="ConnectorLocation">300,77.5 300,127.5</av:PointCollection>
<FlowStep x:Name="__ReferenceID0">
<scg3:Dictionary x:TypeArguments="x:String, x:Object">
<av:Point x:Key="ShapeLocation">200,127.5</av:Point>
<av:Size x:Key="ShapeSize">200,51</av:Size>
<Sequence sap:VirtualizedContainerService.HintSize="264,352">
<scg3:Dictionary x:TypeArguments="x:String, x:Object">
<x:Boolean x:Key="IsExpanded">True</x:Boolean>
<Assign sap:VirtualizedContainerService.HintSize="242,58">
<OutArgument x:TypeArguments="x:String">[argument1]</OutArgument>
<InArgument x:TypeArguments="x:String">[argument1 + "Modified in WF"]</InArgument>
<InvokeMethod sap:VirtualizedContainerService.HintSize="242,130" 

          MethodName="BusinessMethod" TargetType="b:Class1">
<OutArgument x:TypeArguments="x:String">[argument2]</OutArgument>
<InArgument x:TypeArguments="x:String">[argument1]</InArgument>

Build the WF Activity and add reference to WCF Service.

Service Layer (WCF)

Add WCF service. In Service1.svc.cs add following code to call activity. Don’t forget to update contract in IService1.cs.

public string GetData(string value)
    ActivityLibrary1.Activity1 ca = new ActivityLibrary1.Activity1();
    ca.argument1 = value.ToString();
    IDictionary<string, object> dr = new Dictionary<string, object>();
    var output = WorkflowInvoker.Invoke(ca,dr);
    string st = output["argument2"].ToString();
    return string.Format("You entered: {0}", st);

This method takes string variable from UI and process it in WF Activity and in C# business library. Build WCF service.


Add project to existing solution. In place of ASP.NET you can use Silverlight/WPF project too. If you want to see how WCF can be called in Silverlight project see my other Article.

Add service reference to this project. Now come to Default.aspx to add some controls as per following code.

WF Calling Sample
<br />
<asp:Label ID="Label2" runat="server" Text="Enter Your Name: ">
</asp:Label><asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
<br />
<asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="Button" />
<br />
<asp:Label ID="Label1" runat="server" Text=""></asp:Label>

Add click event for button. In code behind we are going to call WCF service.

protected void Button1_Click(object sender, EventArgs e)
    Service1Client sr = new ServiceReference1.Service1Client();
    int i = 0;
    if (int.TryParse(TextBox1.Text, out i))
        Label1.Text = sr.GetData(i);
        Label1.Text = "Please enter Number only.";

Set Web Application as startup project and Default.aspx as Startup page. Run the application. Enter name and click Button to get modified from WF activity and C# Business library. Hope this idea will help you in designing applications.


In this article you have learned how you can utilize WF activities in business layer. Hope this would be pretty simple example to elaborate architecture.

If this article helps you in designing/coding application don’t forget to hit voting option. Please comment your suggestions and improvements.

Happy coding!!


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

United States United States
Manoj Kumar

Architect, Lead Software Engineer and Senior BI Developer | Microsoft Certified Technology Specialist(MCTS) | Exp in C#, ASP.Net, MVC, Razor View Engine and EF Code First, HTML5, CSS3, JQuery, Javascript, AngularJS, JSON, AJAX, KnockoutJS, Silverlight, WPF, WCF, WF, SSRS, SSAS, SSIS, ETL, SQL Server, Usability/User Experience etc.

He lives with his wife Supriya and daughter Tisya in Bay Area.

You may also be interested in...


Comments and Discussions

Questionworkflow foundation architecture Pin
Raja Chandrasekaran2-Aug-14 3:31
memberRaja Chandrasekaran2-Aug-14 3:31 
QuestionWorkflowApplication as a host instead of WorkflowInvoker as a host Pin
wkalter28-Jan-13 23:36
memberwkalter28-Jan-13 23:36 
GeneralMy vote of 4 Pin
wkalter28-Jan-13 23:23
memberwkalter28-Jan-13 23:23 
GeneralMy vote of 2 Pin
Prasyee31-Aug-12 1:54
memberPrasyee31-Aug-12 1:54 
QuestionFormatting Pin
Mehdi Gholam9-Jun-12 23:17
mvpMehdi Gholam9-Jun-12 23:17 
AnswerRe: Formatting Pin
ManojKumar1910-Jun-12 6:00
memberManojKumar1910-Jun-12 6:00 
SuggestionRe: Formatting Pin
Vitaly Tomilov10-Jun-12 7:19
memberVitaly Tomilov10-Jun-12 7:19 
AnswerRe: Formatting Pin
ManojKumar1910-Jun-12 7:29
memberManojKumar1910-Jun-12 7:29 
GeneralRe: Formatting Pin
Andreas Gieriet10-Jun-12 12:08
memberAndreas Gieriet10-Jun-12 12:08 
SuggestionCouple things Pin
Tim Corey9-Jun-12 5:12
memberTim Corey9-Jun-12 5:12 
GeneralRe: Couple things Pin
ManojKumar199-Jun-12 13:26
memberManojKumar199-Jun-12 13:26 
AnswerRe: Couple things Pin
Tim Corey9-Jun-12 13:47
memberTim Corey9-Jun-12 13:47 
GeneralRe: Couple things Pin
ManojKumar199-Jun-12 16:12
memberManojKumar199-Jun-12 16:12 
AnswerRe: Couple things Pin
Tim Corey9-Jun-12 16:20
memberTim Corey9-Jun-12 16:20 
GeneralRe: Couple things Pin
ManojKumar199-Jun-12 19:42
memberManojKumar199-Jun-12 19:42 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.170622.1 | Last Updated 11 Jun 2012
Article Copyright 2012 by ManojKumar19
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid