Introduction
While working on Windows Workflow Foundation (WF) on Windows Communication Foundation (WCF), I could not find any “practical” documentation/sample explaining, step by step, what needs to be done along with explaining “why”. This article describes how to create a sequential WF as a WCF service.
Well...so, what are we going to learn here?
- Create a sequential workflow service library on Windows Communication Foundation.
- Work with “RecieveActivity” – Create contracts, bind properties, etc.
- Create a website to expose the library to the outside world (web configuration, etc.).
- Create a custom workflow runtime service and configure it on the service model section.
- Create a custom activity along with custom events and properties using DependencyProperty.
- Create a custom activity to invoke a workflow from another workflow. Also, how to pass parameters from/through.
- Basic idea of rules, how to create a ruleset using the rule editor. Apply rule during runtime.
Background
As I mentioned, WCF, WF, Rules are very interesting technologies, but at the same time, it's a bit time consuming task to gather all the required basic information from MSDN and other sites. I just wanted to capture all the necessary functionalities around this area.
Using the code
A simple business scenario will make this more interesting. So, let’s define it:
- Pre-order details (or raw order details) come from an external source.
- Client invokes “Order Processor” to process data.
- “Order Processor” applies business rules on raw data and decides which ones to accept and reject.
- It also applies a specific business rule (shipping rate discount for existing customers) on each process (order) and saves the order and shipping details into the database.
- Business user can change a rule anytime they want.
- Optionally, the invoker also can define whether the input file is to be archived or not. [Just to make our main workflow a bit interesting.]
In technical terms:
- Order processor is our main workflow on WCF. The workflow will be active (alive) till we switch it off explicitly, so that it can process our request on demand.
- The main workflow applies rules on file level (on all records). In this example, if an order amount is found as zero, reject the order (Data Quality Rule).
- After processing file level rules, it invokes another child workflow (which will be a custom activity) to process the workflow by passing the complete ‘order’ object. [Here we will see how we can define properties using “DependencyProperty” and how to use it.]
- ‘Worker’ workflow applies rules on order level (ShipmentCostRule) and shipping rates get calculated. Finally, it saves the order into the DB using LinQ.
- Both workflows will use a custom workflow runtime service, ExternalRuleSetService, which will be used to retrieve rules from the database.
- We will also create two custom activities, “InvokeWorkflow” - to invoke another workflow, and “PolicyFromService” – to apply a rule, which internally is going to use the workflow runtime service to pull rules from the database. This will also have a custom activity event which will get fired after applying the policy.
- We will use the rule editor to create rules.
References
I have used a few samples from Microsoft samples which has the rule editor, PolicyFromService, sample usage, etc.
Getting started
Alright! Let us first decide our folder structure.
- Lib – will contain all the binaries which will be used by the various apps during runtime.
- Src/Libraries – will contain all the libraries (Custom Activities, Custom workflow runtime service, etc.).
- Src/RuleEditor – rule editor from Microsoft sample, I have not done anything special on this.
- Src/Services – our main workflow on WCF service, also a website to host the service.
- Src/TestClient – test client to invoke the service.
Create the main workflow
- Create New Project-->WCF-->Sequential Workflow Service Library.
- A sequential workflow will get created with a “RecieveActivity”, also an interface “
IWorkflow1
”. Rename workflow “Workflow1” to “ProcessManager” and the interface “IWorkflow1
” to “IProcessManagerServices
”, because this will be our service which we are going to expose to the external world. - Open
IProcessManagerServices
. Rename the existing operation contract to string Start(bool fileToBeArchived)
. - Open “ProcessManager”, you will notice the recieveActivity with a red exclamation mark. Don’t worry, this is because we have renamed the contract “getData”.
- Correct the contract operation name, properties --> ServiceOperationInfo --> select “Start” operation.
- We need to bind the input parameter “fileToBeArchived”.
- ProcessManager --> view code.
- Define a variable
public bool FileToBeArchived;
- Come back to Design mode.
- Click on “Promote Bindable Properties”.
- You should see the “
FaultMessage
”, “fileToBeArchived
”, and “OperationValidation
” properties filled. - Delete the “
FaultMessage
” and “OperationValidation
” properties back to blank. We are going to learn about these in the next section. Go to the code view. Delete all the related code VS IDE has added into your code. You should see something like this.
- Click on ‘
fileToBeArchived
” and bind with our defined variable “FileToBeArchived
”.
- We want our service (main workflow) to be active (always alive). For that, we will use a “While” activity. Before dropping it, let us first create another variable
public bool SwitchedOn
. - Create a “Code” activity just above our receive activity and name it as “SwitchOn”. Drop a “While” activity just below it, and then drag Start “Receive” activity inside “While” activity. Name it as “WhileSwitchedOn”.
- On the execute code method, write
this.SwitchedOn = true;
. - Let's drop another “Receive” activity, and name it “SwitchOff”. Create another operation contract “SwitchOff” and bind it accordingly.
- So, our workflow will be alive till we don’t switch it off explicitly.
- The client will invoke the workflow “Start” operation with a parameter whether the input file needs to be archived or not, just to show you a few “ifelse” activities to perform some tasks at the main workflow level.
Create libraries
Alright…now, let’s take a break from here and create some other interesting stuff! Let’s create our workflow library with the required custom activities “InvokeWorkflow
” and “PolicyFromService
”.
A custom activity needs to be inherited from System.Workflow.ComponentModel.Activity
, and override its Execute
method, that’s it! So simple!
PolicyFromService
– I just took the Microsoft sample, not done anything special on this, but just added a custom event which will get fired after applying the policy (ruleset).InvokeWorkflow
– While overriding the Execute
method, you just need to do following:
- Get service as
IStartWorkflow IStartWorkflow startWorkflow = executionContext.GetService(typeof(IStartWorkflow)) as IStartWorkflow;
- Start the workflow
this.InstanceId = startWorkflow.StartWorkflow(this.TargetWorkflow, this.Parameters)
. Targetflow
and Parameters
will be filled by “calling workflow” or “parent workflow”.
Custom property using DependencyProperty:
- Define a static
DepedencyProperty
, public static readonly DependencyProperty InstanceIdProperty = DependencyProperty.Register("InstanceId", typeof(Guid), typeof(InvokeWorkflow));
. - Create a public property to ‘
get
’ and ‘set
’ the (base
) values
[Category("Activity")]
[Description("InstanceId of of the invoked workflow")]
public Guid InstanceId
{
get
{
return (Guid)base.GetValue(InstanceIdProperty);
}
set
{
base.SetValue(InstanceIdProperty, value);
}
}
- Define a static
DependencyProperty
:
public static DependencyProperty AfterPolicyAppliedEvent =
DependencyProperty.Register("AfterPolicyApplied",
typeof(EventHandler), typeof(PolicyFromService));
- Create a public event to ‘
add
’ and ‘remove
’ the event handler:
public event EventHandler AfterPolicyApplied
{
add { base.AddHandler(AfterPolicyAppliedEvent, value); }
remove { base.RemoveHandler(AfterPolicyAppliedEvent, value); }
}
- Raise the event in the required place(s).
base.RaiseEvent(PolicyFromService.AfterPolicyAppliedEvent,this, EventArgs.Empty);
Custom workflow runtime service
ExternalRuleSetService – I have not done anything special on this, but just used the Microsoft sample as it is. You guys just try to understand what the code does. We are going to configure this workflow runtime service, so that it can be used at run time (by the “PolicyFromService” activity).
Finally, compile the solution “Library.sln” and copy the binaries into the root\Lib folder, which will be referred by our main solution.
Back to the main workflow
To use our custom libraries, go to Toolbox --> right click --> Choose Items. Select our libraries from the Lib folder. You should see our components on the toolbar like this:
As for our business scenario, after processing the initial tasks on the file (archive, etc.), we need to apply the business rules on the completed input records, accept, and reject records accordingly.
Drop the “PolicyFromService” activity, name it “DataQuality”, and also set RuleSetName as “DataQuality”. Remind me to create this rule with the rule editor :)
Child Workflow
Okie Tokie! We have filtered the records (pre order details) and we need to process each order by applying the business rules and saving them into the DB. For that, we will create another sequential workflow “Worker” and place the “Code” activity to initialize the worker, then apply the rule, a “PolicyFromService” activity, and finally a “Code” activity to save the processed order into the DB. Name these accordingly and set the “PolicyFromService” activity RuleSetName to “ShipmentCostRule” …Remind me to create this rule as well :)
We are going to invoke this child “Worker” workflow from our main “ProcessManager” workflow. And, this will keep continuing until all orders are processed. So, let's drop a “While” activity, select the Condition as "Declarative Rule Condition" with an expression as this.OrderQueue.Count != 0
. Drop an “InvokeWorflow” activity inside the "While" activity. Set TargetWorkflow to “Worker” (remember we are now using a DependencyProperty
) by browsing type.
Finally…our final main workflow looks like…
We need to pass the required parameters from the main workflow to the child workflow. In this case, we are going to pass a complete ‘order’ object to “Worker”. And, we do it in the “BeforeInvoke
” event of the “worker” workflow.
Dequeue the order from the queue and pass it as a parameter.
OrderDetail thisOrder = this.OrderQueue.Dequeue();
Type targetWorkflow = typeof(Worker);
(sender as InvokeWorkflow).Parameters.Add("Order", thisOrder);
[Note - I have used the .NET Queue
to queue all records, and dequeue them one by one to process.]
Creating Rules
Okaayy... :) We are pretty close with our application, just need to create two rule sets using the rule editor. Hope you have been with me till now.
- DataQuality – Ignore all orders with a $0 amount. I have created an extension method
FilterZeroAmountOrders
to filter all such records, which we are going to use in this rule. Condition – this.RawOrders.FilterZeroAmountOrders().Count > 0
.
Then Action – this.Orders = this.RawOrders.FilterZeroAmountOrders()
.
- ShipmentCostRule – If a member has already bought items of more than $1000 value, we give 50% discount to him. Another extension method
TotalAmountOrderedTillDate
will help us on this. Condition – this.Order.TotalAmountOrderedTillDate() > 1000
.
Then Action – this.Order.ShippingCost = this.Order.ShippingCost * 0.5
.
We don’t care about else parts, we can keep those as blank. Save the rule.
Hosting
Let’s host our service by adding a blank website into our solution. You can delete all except web.config. I will also suggest deleting all contents of web.config so that we know what exactly is needed to run this solution. This is important… Let’s divide our tasks into two parts..
- Configure or expose our service contract on the WCF service.
- To do that, we will add a new item “WCF Service” into the website and name it as ProcessManagerService.svc. Delete all other items, and folders if VS IDE creates any.
- In ProcessManagerService.svc, we need to tell which service we are going to expose. Our service is actually a workflow named “ProcessManager”. Mention this along with its Factory which takes care of the internal “things” to expose the workflow as a service.
Factory="System.ServiceModel.Activation.WorkflowServiceHostFactory"
Service="BigDs.WF.Services.ProcessManager"
- Define its endpoint as per the old WCF “ABC” rule. Look at the web.config for details.
- Configure our custom
WorkflowRuntime
service under the service model, because WCF is the host for our workflow. This config section is at <system.serviceModel
> --> <serviceBehaviors
> --> <behavior
> --> <workflowRuntime
>
- Add the workflow runtime service as follows.
<workflowRuntime>
<services>
<add type="BigDs.WF.WorkFlow.ExternalRuleSetService, BigDs.WF.WorkFlow" />
<services>
<commonParameters>
<add name="ConnectionStringName" value="cs" />
</commonParameters>
</workflowRuntime>
Set the website as the startup project and run it. Click on ProcessManagerService.svc to verify that our service is configured properly.
We are nearly ready, this time I am serious :), just need to create client to invoke.
Client
Create a simple console application, add a service reference, and invoke the WCF method as follows…
ProcessManagerServicesClient psc = new ProcessManagerServicesClient();
psc.Start(true);
That's all folks...Run your website, execute the client to test the application. Please let me know what you think about this article, or how can we improve/enhance it.
Let’s quickly summarize what all things we learnt today
- WCF Sequential workflow (WF) service library.
- Exposing it as a WCF service by hosting it on a website.
- Configuring it along with a workflow runtime service.
- Creating a custom workflow runtime service.
- Creating custom activities.
- Creating custom properties using
DependencyProperty
. - Creating custom events using
DependencyProperty
. - A basic idea of the rule set.
- How to use a rule editor to create rule sets.
- Getting rules from the DB and apply it during runtime using a workflow runtime service.
- Creating a custom activity to invoke a child workflow.
- How to pass parameters to a child workflow.
- How to use extension methods and use it in the rule editor to create rules.
Points of interest
We can define <commonParameters>
under <workflowRuntime>
. I am still trying to find a way to read this from the config. Huuuh! It's really annoying to search for these small things. May be some other time.
Running the code
- Unzip the file.
- Execute the scripts under Script folder. This will create two tables with some existing data (two rules and three existing order details).
- Update the
AssemblyPath
of the RuleSet table according to your path. This is important. - Update
InputFileLocation
in web.config to your path.
History
- 1st version, released on 16th July 2009.
Client partner/Technical Architect/Multiple Project Management with over 19+ years of experience in all phases of the software development full & agile life cycle, including technical design, system analysis & programming.
Expertise:
✔ Experienced with large agile onshore/offshore model project(170+ team members).
✔ Innovative ideas and solution to improve development process, execution, test automation, project meta data reporting, etc.
✔ Client relationship, value added service, software development, right process to mitigate risk and ultimately deliver the project on time.
✔ 18+ years of technical "in-depth" knowledge on various programming technologies right from C, C++, Oracle, VB.Net, C#, Windows Workflow 4.0, Windows Communication Foundation, MS SQL Server. Touch of Java, TIBCO BW & BC. The new exciting thing in past few months is 'R' language.
✔ Handling multiple projects, working as client partner to a major bank in Charlotte, North Carolina
✔ Developer of intellectual property of Synechron Inc - Symplus Rule Engine - A rule engine developed using Windows Workflow Foundation 4.0 and Windows Communication Foundation.
Latest/Top stories:
✔ Started a few financial projects around AI(Artificial Intelligence). My new favorite language is R!!!
✔ Executing a few major projects in counter party credit & market risk platform & reg reporting area.
✔ Designed & developed a rule engine named Symplus Rule Engine using WF4.0, WCF, SQL CE 4.0