DCSIMG
Execute Windows Workflow Rules without Workflow - Guy Burstein's Blog

Guy Burstein's Blog

Developer Evangelist @ Microsoft

News

Guy Burstein The Bu

Disclaimer
Postings are provided 'As Is' with no warranties and confer no rights.

Guy Burstein LinkedIn Profile

TwitterCounter for @bursteg

Links

Articles

Blogs I Read

Execute Windows Workflow Rules without Workflow

Embedding business rules is very powerful and useful. If you are using Windows Workflow Foundation you can model you business process and use a PolicyActivity. But If you don't want to model your business process with a workflow, but still want to execute rules in it?

The Windows Workflow Foundation Rules Engine has the API for it. I'll take this post to explore it a little bit and give some examples.

 

You can run a RuleSet against any object…

Typically you would execute your RuleSets against you workflow, to change the workflow state according to some properties. But, you can also execute rules againt any other .net object.

 

Create a RuleSet

The RuleSet Editor Dialog is exposed as part of the API, so you can use it to create RuleSets as part of your program:

 

// Create a RuleSet that waorks with Orders (just another .net Object)

RuleSetDialog ruleSetDialog = new RuleSetDialog(typeof(Order), null, null);

 

// Show the RuleSet Editor

ruleSetDialog.ShowDialog();

 

// Get the RuleSet after editing

RuleSet ruleSet = ruleSetDialog.RuleSet;

 

Serialize and Deserialize rules

Windows Workflow Foundation uses the WorkflowMarkupSerializer to Serialize and Deserialize rules.

 

// Serialize to a .rules file

WorkflowMarkupSerializer serializer = new WorkflowMarkupSerializer(); 

XmlWriter rulesWriter = XmlWriter.Create(fileName);

serializer.Serialize(rulesWriter, ruleSet);

rulesWriter.Close();

 

// Deserialize from a .rules file.

XmlTextReader rulesReader = new XmlTextReader(fileName);

WorkflowMarkupSerializer serializer = new WorkflowMarkupSerializer();

ruleSet = (RuleSet)serializer.Deserialize(rulesReader);

rulesReader.Close();

 

Execute RuleSet againt an object

Execute your RuleSet after editing, serializing and deserialising it, but first validate that the rules can be executed against the input object.

 

// Execute the rules and print the entity's properties

Order myOrder = new Order(...);

RuleValidation validation = new RuleValidation(typeof(Order), null);

RuleExecution execution = new RuleExecution(validation, myOrder);

ruleSet.Execute(execution);

 

Download a sample project that demonstrates creation, editing, serialization and execution of RuleSet against a custom object.

 

Enjoy!

Comments

rotu said:

Good day.

Is there a way to determine which of the rules inside the ruleset have been satisfied during ruleSet.Execute?

ex.

Rule1 - satisfied

Rule2 - not satisfied

etc.

Thanks

# January 10, 2007 8:04 AM

גיא בורשטיין said:

You can use the RuleAnalysis class and the RuleExpressionWalker classes to determine that. They both work at the level of a single rule and not the whil ruleset, but a simple iteration should do it.

Good luck!

Guy

# January 10, 2007 11:07 AM

Guy Burstein's Blog said:

This is my #200 post, and it is a good time to look back at what I've posted over the last few months.

# March 9, 2007 8:51 PM

Guy Burstein's Blog said:

This post talks about the posibility of using the WF Rules Engine not inside a business process developed by Windows Workflow Foundation

# August 9, 2007 6:54 PM

Chakshu Shah said:

How can I use this Rules Engine in my ASP.Net 2.0 application? The only option I have to expose it as a web-service and then use? Can you think of so other way of accomplishing this?

# August 22, 2007 7:51 AM

גיא בורשטיין said:

You can use it in ASP.Net exactly in the same way. You can handle any postback event and call the Rules Engine API.

# August 22, 2007 9:32 AM

Chakshu Shah said:

Please note that I want to use it in a ASP.Net 2.0 application. I dont think I shall be able to use it in the same way. The option I was thinking was to wrap up the Rules Engine with a Web-Service and use it through ASP.Net 2.0. If I follow this approach,

1) Will I be able to modify the rules from my front-end GUI?

2) Is the WF Rules Engine advanced enough to support complex rules?

This is kind of urgent requirement. Your prompt help would be highly appreciated.

# August 23, 2007 8:52 AM

גיא בורשטיין said:

Hi

I see no reason for not being able to use the Rules Engine in an ASP.Net Application just as you would do in any other application.

You have no GUI for editing rules for web applications. Only a windows forms dialog ships with WF and it is targeted mostly for developers.

The WF Rules Enging support complex conditions and rules, and you can read all about its capabilities here:

http://msdn2.microsoft.com/en-us/library/aa480193.aspx

Guy

# August 23, 2007 5:58 PM

Chakshu Shah said:

Thanks for all the prompt replies but I still have doubts :)

As I understand, WF is part of .Net 3.0 and so is the Rules Engine that is part of the Workflow Framework. I have a requirement of using this Rules Engine from ASP.Net 2.0. Since Rules Engine is a part of .Net 3.0, I dont think I will be able to use the Rules API from 3.0 directly in 2.0. So, I was thinking of using a web-service.

Do you think the solution is workable? or are you saying that I will be able to use the APIs directly in 2.0?

# August 24, 2007 1:03 PM

גיא בורשטיין said:

Of course! The .net framework 3.0 contains only extensions for the .net framework 2.0, but the CLR remains the same. This means that you can easily reference the .net 3.0 assemblies and work directly with them.

Guy

# August 24, 2007 5:22 PM

Jon Kruger’s Blog » Filtering Intellisense lists in the WF RuleSetDialog said:

Pingback from  Jon Kruger’s Blog   » Filtering Intellisense lists in the WF RuleSetDialog

# February 3, 2008 11:35 PM

zxevil163 said:

qdejCE Hi from Russia!

# March 16, 2008 11:15 AM

poonamb said:

hi,

in my application, i require to save this .rules file in SQL server 2005. i should be able to retrieve this file from database whenever rule applies, make changes if needed and save it back to database.

please help.

# May 16, 2008 3:35 PM

Josh said:

Hi Guy, first of all thanks for great articale.

its gives me lots of (new) ideas of using wf rules instead of biztalk.

the problem with biztalk bre composer is that users need to install or use it from trminal place. but with wf (as your example) we can launch the rule set dialog from any client application.

my question is what the best way to call the wf rule from biztalk orch(we use biztalk2006) , of course the simple option is to call to webservice from BTS that host the wf rules.

Is there any option to call the wf rule directly from the BTS orch shape? is any problem to call from .net2.0(bts 2006) to .net3.5?

Thanks,

Josh

# July 8, 2008 11:17 AM

גיא בורשטיין said:

I guess you could do this (never tried though).

Keep in mind that you cannot reference a .Net 3.5 assembly from a .Net 2.0 project, so you may want to do this via reflection.

# July 8, 2008 2:43 PM

Marc said:

I would like to be able to audit the execution of my rules.  I prefer not to use Trace because I want more control over the output.  I found RuleActionTrackingEvent, but it appears to be only available for rules executed within a workflow.  Are there any other alternatives?  In an earlier post, you mentioned RuleAnalysis and RuleExpressionWalker.  However, it's not clear from the MSDN documentation how to really use them.  Any suggestions?  Thanks.

# August 7, 2008 9:56 PM

re: Execute Windows Workflow Rules without Workflow said:

re: Execute Windows Workflow Rules without Workflow

# October 28, 2008 5:13 AM

steve said:

Is there a reference for the rules scripting language?

Good stuff!

# October 28, 2008 8:32 PM

Windows Workflow Foundation Rules Engine « Alberts Tech Log said:

Pingback from  Windows Workflow Foundation Rules Engine « Alberts Tech Log

# November 13, 2008 7:17 AM

jakew said:

  How To Use the Windows Workflow Rules Engine By Itself Execute Windows Workflow Rules without

# December 4, 2008 10:10 AM

Guy said:

If any one know about rule engine working. please contact me. I need to create a rule engine using php and sql. contact me at stylishboy_18@hotmail.com

# March 10, 2009 8:28 PM

SK said:

Great post.!

I have a doubt.

How can I edit the rules once I created?

Or how do I display the "Select Rule Set" dialog?

# March 18, 2009 10:56 PM

Vagif Abilov's blog on .NET said:

I’ve been searching for a rule engine for our projects, and the one that suits our needs is a rule engine

# April 5, 2009 8:23 PM

CSR said:

I need check some fields in a form for trigger some events, and I need to do so without entering code in the form.

Could I use Rule Set Toolkit in a winform for this one?

# April 14, 2009 1:42 PM

Saman said:

Thnx Guy. Your solution was a great help and prevent me to use other unknown rules engines.

# May 5, 2009 2:01 PM

Barry said:

Is it possible to invoke the rules engine within a CLR procedure in SQL Server?

# May 19, 2009 10:57 AM

Vibhuti said:

Hello,

Thanks for Good Article !!

I have been go through it but i have one question in my mind that can we give Parenthesis in Rule to give high precedence to operator .

Like i have a rule :  rating=10 AND Channel="A" OR time=8 AM

In this rule first expression with AND operator will execute and then it will execute with OR.like : (rating=10 AND Channel="A") OR time=8 AM  because of high precedence of AND.

But suppose i want to execute rule like:  rating=10 AND (Channel="A" OR time=8 AM ) ,so can we give Parenthesis to make high precedence of OR operator ,so first expression with OR will execute and then it will execute with AND.

# June 16, 2009 8:46 AM

brutal said:

Can this be used for C++?

# July 2, 2009 5:40 PM

brutal said:

Can this be used in C++ CRL project?

# July 2, 2009 5:56 PM

Noel said:

Hi can we have rules applied on list on objects which will use linq to give some conditions. In short can we apply rules on Linq to SQL results.

# July 22, 2009 11:46 AM

DevDays 2010 Presentation: Using the Workflow Foundation Rule Engine without using Workflow Foundation « Daniel van Wyk's Blog said:

Pingback from  DevDays 2010 Presentation:  Using the Workflow Foundation Rule Engine without using Workflow Foundation « Daniel van Wyk's Blog

# March 30, 2010 5:40 PM

Motorhome Auto Chevy P30, P30 Series Part Generic said:

Pingback from  Motorhome Auto Chevy P30, P30 Series Part Generic

# May 20, 2010 10:47 PM

G3500 Part Fan Motor, Express 3500 Sale Chevrolet G3500 Duramax Diesel said:

Pingback from  G3500 Part Fan Motor, Express 3500 Sale Chevrolet G3500 Duramax Diesel

# May 21, 2010 2:27 AM

Bmw 850i Problems Auto Parts, Volvo 850 Wheel Bearings said:

Pingback from  Bmw 850i Problems Auto Parts, Volvo 850 Wheel Bearings

# May 21, 2010 10:02 AM

Eagle Vision Tsi Parts Car, Eagle Vision Part Brake Pad Set said:

Pingback from  Eagle Vision Tsi Parts Car, Eagle Vision Part Brake Pad Set

# May 22, 2010 12:46 PM

Lx470 Discount Sc400 Lexus Gs400, Discount Gs400 Body Tail Light Assembly said:

Pingback from  Lx470 Discount Sc400 Lexus Gs400, Discount Gs400 Body Tail Light Assembly

# May 23, 2010 3:08 AM

1984 Volkswagen Vanagon Used Engine, Gmc Vanagon For Sale - 495.jeepsunlimted.com said:

Pingback from  1984 Volkswagen Vanagon Used Engine, Gmc Vanagon For Sale - 495.jeepsunlimted.com

# May 23, 2010 9:08 PM

Acura Nsx Coupon Air Intake, 1998 Acura Nsx T - 171.cmanager.org said:

Pingback from  Acura Nsx Coupon Air Intake, 1998 Acura Nsx T - 171.cmanager.org

# May 24, 2010 7:44 PM

Mercedes Benz 280s Boiler Hot, 280e Price Wine Storage - 236.binggreen.com said:

Pingback from  Mercedes Benz 280s Boiler Hot, 280e Price Wine Storage - 236.binggreen.com

# May 25, 2010 11:05 AM

Dodge Spirit Replacement Headlight Assembly Fender Flares, Dodge Spirit Light Bulb Turn Signal - 122.tgrconversions.com said:

Pingback from  Dodge Spirit Replacement Headlight Assembly Fender Flares, Dodge Spirit Light Bulb Turn Signal - 122.tgrconversions.com

# May 25, 2010 10:18 PM

Cr V Malfunction Indicator Lamp Honda Accord, Gas Station Lax - 256.myipgirl.com said:

Pingback from  Cr V Malfunction Indicator Lamp Honda Accord, Gas Station Lax - 256.myipgirl.com

# May 26, 2010 3:34 AM

1984 - 2003 @ 2008 Chrysler Pacifica Odyssey, 2007 Chrysler Pacifica Bumper - 279.codebluehacks.org said:

Pingback from  1984 - 2003 @ 2008 Chrysler Pacifica Odyssey, 2007 Chrysler Pacifica Bumper - 279.codebluehacks.org

# May 31, 2010 4:39 AM

1981 - 2007 @ 1992 Honda Accord Lx Mpg, 1989 Honda Accord Engine Specs - 372.ja3ra.com said:

Pingback from  1981 - 2007 @ 1992 Honda Accord Lx Mpg, 1989 Honda Accord Engine Specs - 372.ja3ra.com

# May 31, 2010 5:53 AM

1994 - 2004 @ Concorde Substitute Japan Aerospace Exploration Agency, Chrysler Concorde Part Brake Pads - 276.jeepsunlimted.com said:

Pingback from  1994 - 2004 @ Concorde Substitute Japan Aerospace Exploration Agency, Chrysler Concorde Part Brake Pads - 276.jeepsunlimted.com

# June 1, 2010 12:04 AM
Leave a Comment

(required) 

(required) 

(optional)

(required) 


Enter the numbers above: