Thursday, January 27, 2011

Create Windows Workflow 4.0 Rules By Custom String

In my previous post, I presented a way to create Windows Workflow rules by CodeDom expression. Sometimes it's hard to build a meaningful UI for authoring business rules. In such case we may need to use custom string to construct WF rules. The key part is how to parse the rule condition/action string. We could do similar things in WF RuleSetDialog. Can we re-use the parser used by RuleSetDialog? The answer is yes. Beau Crawford gave a solution for WF 3.5. Basically WF internally has a rule condition and action parser under System.Workflow.Activities.Rules.Parser inside System.Workflow.Activities assembly, but the parser is not exposed to public. In order to re-use the internal parser we need to use reflection.

Following code example demos how to archive this by slight modification of the code used in previous post:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.CodeDom;
using System.Reflection;
using System.Workflow.Activities.Rules;
using System.Workflow.ComponentModel.Compiler;

class Product
{
public string Name { get; set; }
public decimal Price { get; set; }
}

class ShoppingCart
{
public List<Product> SelectedProducts { get; set; }
public List<string> CouponCodes { get; set; }
public decimal TotalAmount { get { return SelectedProducts.Sum(prod => prod.Price); } }
internal decimal Discount { get; set; }

public ShoppingCart()
{
SelectedProducts = new List<Product>();
CouponCodes = new List<string>();
Discount = 0;
}
}

class CouponPromotion
{
public string Name { get; set; }
public string PromotionCode { get; set; }
public decimal Discount { get; set; }
public DateTime ExpiredOn { get; set; }
}

/// <summary>
/// WFRuleParser is helper class to parse Windows Workflow condition and action.
/// Invoke ParseCondition and ParseAction two internal methods
/// defined in System.Workflow.Activities.Rules.Parser by reflection
/// </summary>
class WFRuleParser
{
public static RuleExpressionCondition ParseCondition(Type targetObjectType, string expression)
{
RuleValidation ruleValidation = new RuleValidation(targetObjectType, null);
return ExecuteMethod("ParseCondition",
new object[] { ruleValidation }, new object[] { expression }) as RuleExpressionCondition;
}

public static List<RuleAction> ParseAction(Type targetObjectType, string expression)
{
RuleValidation ruleValidation = new RuleValidation(targetObjectType, null);
return ExecuteMethod("ParseStatementList",
new object[] { ruleValidation }, new object[] { expression }) as List<RuleAction>;
}

// Invoke private/internal method using reflection
private static object ExecuteMethod(string methodName, object[] ctorParameters, object[] methodParameters)
{
string ParserTypeName ="System.Workflow.Activities.Rules.Parser, System.Workflow.Activities,
Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
;
try
{
Type type = Type.GetType(ParserTypeName);
ConstructorInfo constructor = type.GetConstructor(BindingFlags.NonPublic | BindingFlags.Instance,
null, new Type[] { typeof(RuleValidation) }, null);
object instance = constructor.Invoke(ctorParameters);
MethodInfo method = instance.GetType().GetMethod(methodName,
BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance);
return method.Invoke(instance, methodParameters);
}
catch (Exception ex)
{
throw ex;
}
}
}

class WFRuleBuilder
{
// Create a Rule for a CouponPromotion
public static Rule GetCouponPromotionRule(CouponPromotion couponPromo)
{
Type targetObjectType = typeof(ShoppingCart); // Shopping Cart is the input object for Rules Engine

// condition => if coupone code is matched and promotion is not expired
var conditionString = string.Format(
"this.CouponCodes.Contains(\"{0}\") && DateTime.Today < DateTime.Parse(\"{1}\")",
couponPromo.PromotionCode, couponPromo.ExpiredOn.ToString("yyyy-MM-dd"));
var condition = WFRuleParser.ParseCondition(targetObjectType, conditionString);

// action => add discount
var actionString = string.Format("this.Discount = this.Discount + {0}", couponPromo.Discount);
var action = WFRuleParser.ParseAction(targetObjectType, actionString);

return new Rule(couponPromo.Name, condition, action, null);
}
}

class Program
{
static void Main(string[] args)
{
// Mock 5 products in shopping cart:
// [{"Product1":{Price:$100, Category:"Category1"}}, {"Product2":{Price: $200, Category:"Category1"}}, ...]
ShoppingCart shoppingCart = new ShoppingCart();
Enumerable.Range(1, 5).ToList().ForEach(i => shoppingCart.SelectedProducts.Add(
new Product() { Name = "Product" + i.ToString(), Price = 100 * i }));
shoppingCart.CouponCodes.Add("123456789");
shoppingCart.CouponCodes.Add("111111111");
Console.Write("\nShopping Cart before rules execution -- ");
Console.WriteLine("Total : ${0}\tDiscount : ${1}", shoppingCart.TotalAmount, shoppingCart.Discount);

// Mock 2 CouponPromotions
CouponPromotion promo1 = new CouponPromotion()
{
Name = "Promotion1", PromotionCode = "123456789", Discount = 100, ExpiredOn = new DateTime(2000, 1, 1)
};
CouponPromotion promo2 = new CouponPromotion()
{
Name = "Promotion2", PromotionCode = "111111111", Discount = 100, ExpiredOn = new DateTime(2100, 1, 1)
};


// Build a RuleSet with above two rules
RuleSet ruleSet = new RuleSet();
ruleSet.Rules.Add(WFRuleBuilder.GetCouponPromotionRule(promo1));
ruleSet.Rules.Add(WFRuleBuilder.GetCouponPromotionRule(promo2));

// Print Rule information
Console.WriteLine("\nRules defined in the RuleSet:");
ruleSet.Rules.ToList().ForEach(rule =>
{
Console.WriteLine("\"{0}\" RuleCondition: \n{1}", rule.Name, rule.Condition);
Console.WriteLine("\"{0}\" RuleAction: \n{1}", rule.Name, rule.ThenActions[0]);
});

// Execute Rules
RuleValidation validation = new RuleValidation(typeof(ShoppingCart), null);
if (ruleSet.Validate(validation))
{
RuleEngine engine = new RuleEngine(ruleSet, validation);
engine.Execute(shoppingCart);
}
else // Validation failed, print errors
{
StringBuilder errors = new StringBuilder();
foreach (ValidationError validationError in validation.Errors)
{
errors.Append(validationError.ErrorText);
}
Console.WriteLine("Validation errors:{0}", errors);
}

Console.Write("\nShopping Cart after rules execution -- ");
Console.WriteLine("Total : ${0}\tDiscount : ${1}", shoppingCart.TotalAmount, shoppingCart.Discount);

Console.Read();
}
}
A CouponPromotion has a coupon code and an expiration date property. At run-time, rules engine will check all coupon codes stored in a ShoppoingCart, then apply the discount if they are valid. The logic is:
if ShoppingCart.Contains(couponPromotion.CouponCode) && couponPromotion is not expired
then add CurrentCouponPromotion.Discount to ShoppingCart
Remember the target object the rule engine runs against on is a ShoppingCart instance. The rule action would be update ShoppingCart's Discount property. The exact strings to be parsed for two promotions defined in above example code are:
Promotion1 condition string:
this.CouponCodes.Contains("123456789") && System.DateTime.Today < System.DateTime.Parse("2000-01-01")
Promotion1 action string:
this.Discount = this.Discount + 100
Promotion2 condition string:
this.CouponCodes.Contains("111111111") && System.DateTime.Today < System.DateTime.Parse("2100-01-01")
Promotion2 action string:
this.Discount = this.Discount + 100
Obviously Promotion1 has expired and only Promotion1 is valid and applied. The console app result:

Tuesday, January 25, 2011

Business Rules Implementation Using Windows Workflow 4.0

Recently I have been working on a project for a back-end service providing product information for a telecom company. The pricing of a product or products is relying on a number of business rules. e.g. a cell-phone's price and its monthly charge can vary based on the data/voice plan as well as those value-added features. A discount or multiple discounts could be applied if certain criteria are matched. In addition, complicated validation rules are applied for those products.

Windows Workflow Rules Engine, a free out-of-box feature from Windows Workflow Foundation, plays a center role in our business rules implementation. We construct our own business entities and relationship. On top of those business objects we build our customized rule authoring UI. Those rule-related entities are converted to CodeDom expression at run-time, and then a set of Workflow RuleSets are created based on those CodeDom expression. For each service call, products will be passed to the Workflow Rule Engine as an input, and rules execution would update their metadata or properties if a rule or some rules are satisfied.

Following code sample demos how all this works:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.CodeDom;
using System.Workflow.Activities.Rules;
using System.Workflow.ComponentModel.Compiler;

#region Business Object Models
class Product
{
public string Name { get; set; }
public string Category { get; set; }
public decimal Price { get; set; }
}

class ShoppingCart
{
public List<Product> SelectedProducts { get; set; }
public string CouponCode { get; set; }
public decimal TotalAmount { get { return SelectedProducts.Sum(prod => prod.Price); } }
internal decimal Discount { get; set; }

public ShoppingCart()
{
SelectedProducts = new List<Product>();
Discount = 0;
}

// Check total amount for a given category
public bool TotalCategoryAmountGreaterThan(decimal price, string category)
{
decimal totalPriceForCategory = 0;
foreach (var prod in SelectedProducts)
{
if (prod.Category == category)
totalPriceForCategory += prod.Price;
}

return totalPriceForCategory >= price;
}
}
#endregion

#region Rule-related Object Models
/// <summary>
/// Apply a discount if having the promotion code
/// </summary>
class CouponPromotion
{
public string Name { get; set; }
public string PromotionCode { get; set; }
public decimal Discount { get; set; }
}

/// <summary>
/// Apply a discount when total amount for a given category greater than a value
/// </summary>
class BundlePromotion
{
public string Name { get; set; }
public string ProductCategory { get; set; }
public decimal TotalAmountThreshold { get; set; }
public decimal Discount { get; set; }
}
#endregion

#region Helper class to create rules based on Rule-related objects
class WFRuleBuilder
{
// Create a Rule for a CouponPromotion
public static Rule GetCouponPromotionRule(CouponPromotion couponPromo)
{
CodePrimitiveExpression couponCode = new CodePrimitiveExpression(couponPromo.PromotionCode);
CodePrimitiveExpression couponDiscount = new CodePrimitiveExpression(couponPromo.Discount);

CodePropertyReferenceExpression couponPropertyRef =
new CodePropertyReferenceExpression(new CodeThisReferenceExpression(), "CouponCode");
CodePropertyReferenceExpression discountPropertyRef =
new CodePropertyReferenceExpression(new CodeThisReferenceExpression(), "Discount");

// Create a binary equality expression to compare the property value
CodeBinaryOperatorExpression couponConditionExpression =
new CodeBinaryOperatorExpression(couponCode, CodeBinaryOperatorType.ValueEquality, couponPropertyRef);

// Create a binary addition expression to increase the discount
CodeBinaryOperatorExpression discountExpression =
new CodeBinaryOperatorExpression(discountPropertyRef, CodeBinaryOperatorType.Add, couponDiscount);
CodeAssignStatement discountActionExpression =
new CodeAssignStatement(discountPropertyRef, discountExpression);

Rule couponRule = new Rule(couponPromo.Name);
couponRule.Condition = new RuleExpressionCondition(couponConditionExpression);
couponRule.ThenActions.Add(new RuleStatementAction(discountActionExpression));

return couponRule;
}

// Create a Rule for a BundlePromotion
public static Rule BuildBundlePromotionRule(BundlePromotion bundlePromo)
{
CodePrimitiveExpression priceThreshold = new CodePrimitiveExpression(bundlePromo.TotalAmountThreshold);
CodePrimitiveExpression productCategory = new CodePrimitiveExpression(bundlePromo.ProductCategory);
CodePrimitiveExpression discount = new CodePrimitiveExpression(bundlePromo.Discount);
CodePrimitiveExpression[] parameters = new CodePrimitiveExpression[] { priceThreshold, productCategory };

CodePropertyReferenceExpression discountPropertyRef =
new CodePropertyReferenceExpression(new CodeThisReferenceExpression(), "Discount");

// Create a method invocation expression to check the totalValue at runtime
CodeMethodInvokeExpression methodEvaluationExpression =
new CodeMethodInvokeExpression(new CodeThisReferenceExpression(), "TotalCategoryAmountGreaterThan", parameters);

// Create a binary addition expression to increase the discount
CodeBinaryOperatorExpression discountExpression =
new CodeBinaryOperatorExpression(discountPropertyRef, CodeBinaryOperatorType.Add, discount);
CodeAssignStatement discountActionExpression = new CodeAssignStatement(discountPropertyRef, discountExpression);

Rule priceThresholdRule = new Rule(bundlePromo.Name);
priceThresholdRule.Condition = new RuleExpressionCondition(methodEvaluationExpression);
priceThresholdRule.ThenActions.Add(new RuleStatementAction(discountActionExpression));

return priceThresholdRule;
}
}
#endregion

class Program
{
static void Main(string[] args)
{
// Mock 5 products in shopping cart:
// [{"Product1":{Price:$100, Category:"Category1"}}, {"Product2":{Price: $200, Category:"Category1"}}, ...]
ShoppingCart mockShoppingCart = new ShoppingCart();
Enumerable.Range(1, 5).ToList().ForEach(i => mockShoppingCart.SelectedProducts.Add(
new Product() { Name = "Product" + i.ToString(), Price = 100 * i, Category = "Category1" }));
mockShoppingCart.CouponCode = "123456789";
Console.Write("\nShopping Cart before rules execution -- ");
Console.WriteLine("Total : ${0}\tDiscount : ${1}", mockShoppingCart.TotalAmount, mockShoppingCart.Discount);

// Mock some rules
RuleSet rules = MockRules();

// Execute Rules
RuleValidation validation = new RuleValidation(typeof(ShoppingCart), null);
if (rules.Validate(validation))
{
RuleEngine engine = new RuleEngine(rules, validation);
engine.Execute(mockShoppingCart);
}
else // Validation failed, print errors
{
StringBuilder errors = new StringBuilder();
foreach (ValidationError validationError in validation.Errors)
{
errors.Append(validationError.ErrorText);
}
Console.WriteLine("Validation errors:{0}", errors);
}

Console.Write("\nShopping Cart after rules execution -- ");
Console.WriteLine("Total : ${0}\tDiscount : ${1}", mockShoppingCart.TotalAmount, mockShoppingCart.Discount);

Console.Read();
}

// Mock one CouponPromotion rule and PackageTotalPricePromotion rule for testing
private static RuleSet MockRules()
{
// Mock coupon rule: $10 discount if the user has a coupon code of "123456789"
CouponPromotion couponPromo = new CouponPromotion()
{
Name = "2011 new year coupon",
PromotionCode = "123456789",
Discount = 10
};

// Mock total price rule: $100 discount if purchased more than $1000 for ProductCategory of "Category1"
BundlePromotion bundlePromo1 = new BundlePromotion()
{
Name = "Bundle Promotion for Category1",
ProductCategory = "Category1",
TotalAmountThreshold = 1000,
Discount = 100
};

// Mock total price rule: $200 discount if purchased more than $1000 for ProductCategory of "Category2"
BundlePromotion bundlePromo2 = new BundlePromotion()
{
Name = "Bundle Promotion for Category2",
ProductCategory = "Category2",
TotalAmountThreshold = 1000,
Discount = 200
};

// Create a RuleSet to contain above two rules
RuleSet promotionRules = new RuleSet("Promotion Rules");
promotionRules.Rules.Add(WFRuleBuilder.GetCouponPromotionRule(couponPromo));
promotionRules.Rules.Add(WFRuleBuilder.BuildBundlePromotionRule(bundlePromo1));
promotionRules.Rules.Add(WFRuleBuilder.BuildBundlePromotionRule(bundlePromo2));

// Print Rule information
Console.WriteLine("\nRules defined in the RuleSet:");
promotionRules.Rules.ToList().ForEach(rule =>
{
Console.WriteLine("\"{0}\" RuleCondition: \n{1}", rule.Name, rule.Condition);
Console.WriteLine("\"{0}\" RuleAction: \n{1}", rule.Name, rule.ThenActions[0]);
});

return promotionRules;
}

}
The console app shows that a $10 coupon discount and a $100 bundle discount has been applied:



We have a simple Product class with Name, Category and Price properties. A ShoppingCart sent to a rule engine contains a list of products and an optional coupon code. Rule engine will update ShoppingCart's discount property if applicable.

Two promotion types have been abstracted in the demo code. CouponPromotion simply applies a discount if the promotion code is matched. BundlePromotion is setup the way that a discount is applied if the total amount for certain category is greater than a number. For such rules we are easily build a nice authoring UI without using the out-of-box RuleSetDialog. RuleSetDialog is powerful but not that user friendly.

GetCouponPromotionRule and BuildBundlePromotionRule are two helper methods to build the Windows Workflow Rule objects based on the defined CouponPromotion and BundlePromotion. CodeDom expression trees are constructed to setup the WF condition and actions. CodeDom expression is an other topic and I am not going to expand it in detail here. Basically you need to build an expression for each statement but join them as an expression tree.

For CouponPromotion the logical condition is comparing the ShoppingCart's CouponCode property with the PromotionCode property defined in CouponPromotion by using CodeBinaryOperatorType.ValueEquality. The CodeBinaryOperatorExpression returns true if the values are the same. The action for a satisfied CouponPromotion condition is a property assignment using CodeAssignStatement to increase the ShoppingCart's Discount value. For BundlePromotion, the condition expression is a little different. The TotalCategoryAmountGreaterThan method defined in ShoppingCart class is used to determine the Workflow Rule's condition. This is achieved by CodeMethodInvokeExpression.

In our test code we create a shoppoing cart with five mock products and three promotions:
ShoppingCart1:
Product1: Name: Product1, Price: $100, Category: "Category1"
Product2: Name: Product2, Price: $200, Category: "Category1"
Product3: Name: Product3, Price: $300, Category: "Category1"
Product4: Name: Product4, Price: $400, Category: "Category1"
Product5: Name: Product5, Price: $500, Category: "Category1"
CouponCode: "123456789"
Promotion1: CouponPromotion $10 discount for coupon code "123456789"
Promotion2: BundlePromotion $100 discount if total amount for "Category1" is more than $1000
Promotion3: BundlePromotion $200 discount if total amount for "Category2" is more than $1000

We can see that both Promotion1 and Promotion2's conditions are matched, but Promotion3's condition is not. So the result shows that total $110 discount has been applied.

How about the performance? Let's do a simple test by increasing the ShoppoingCart's product number to 200, faking 200 CouponPromotion rules and 200 BundlePromotion rules:
    static void Main(string[] args)
{
ShoppingCart mockShoppingCart = new ShoppingCart();
Enumerable.Range(1, 200).ToList().ForEach(i => mockShoppingCart.SelectedProducts.Add(
new Product() { Name = "Product" + i.ToString(), Price = 100 * (i%10),
Category = "Category" + (i%10).ToString() }));
mockShoppingCart.CouponCode = "100";
Console.Write("\nShopping Cart before rules execution -- ");
Console.WriteLine("Total : ${0}\tDiscount : ${1}", mockShoppingCart.TotalAmount, mockShoppingCart.Discount);

RuleSet promotionRules = new RuleSet("Promotion Rules");
Enumerable.Range(1, 200).ToList().ForEach(i =>
{
string sequence = i.ToString(), category = "Category" + (i % 10).ToString();
CouponPromotion couponPromo = new CouponPromotion()
{ Name = "Coupon" + sequence, PromotionCode = sequence, Discount = 10 };
BundlePromotion bundlePromo = new BundlePromotion()
{ Name = "Bundle" + sequence, ProductCategory = category, TotalAmountThreshold = 1000, Discount = 100 };
promotionRules.Rules.Add(WFRuleBuilder.GetCouponPromotionRule(couponPromo));
promotionRules.Rules.Add(WFRuleBuilder.BuildBundlePromotionRule(bundlePromo));
});
Console.WriteLine("\nRules number: {0}", promotionRules.Rules.Count);

Stopwatch watch = new Stopwatch();
RuleValidation validation = new RuleValidation(typeof(ShoppingCart), null);
if (promotionRules.Validate(validation))
{
RuleEngine engine = new RuleEngine(promotionRules, validation);
watch.Start();
engine.Execute(mockShoppingCart);
watch.Stop();
}
else // Validation failed, print errors
{
StringBuilder errors = new StringBuilder();
foreach (ValidationError validationError in validation.Errors)
{
errors.Append(validationError.ErrorText);
}
Console.WriteLine("Validation errors:{0}", errors);
}

Console.WriteLine("Execution time in milliseconds: {0}", watch.ElapsedMilliseconds);

Console.Write("\nShopping Cart after rules execution -- ");
Console.WriteLine("Total : ${0}\tDiscount : ${1}", mockShoppingCart.TotalAmount, mockShoppingCart.Discount);
Console.Read();
}
The test result in an i5-CPU 8G memory Windows 2008 machine:



It shows the execution time for 400 rules is around 20-millisecond. In reality the rules would be more complex than our simple test case. But in general we can see that the speed of Windows Workflow Rules Engine is okay.

Wednesday, January 05, 2011

Good to be Lazy<T>?

Just read Dino Esposito's article Don’t Worry, Be Lazy about the new new Lazy<T> class in .NET Framework 4.0. For short it's a wrapper class to simplify your work for the "lazy loading" approach.

Sometimes we don't want to populate all related data for a complex object at one shot. Ideally, for the performance consideration, we should only retrieve data that are needed. A class is usually abstracted from business model, for instance a Customer object would have properties of ID, Name, Address, Purchased Orders, etc. Populating all these fields could be costly and may require a few expensive calls to backend system. It's possible that only a small portion of an object data is used when requesting a Customer object. Why those purchased orders data are filled when only Customer's name is in interest? Lazy<T> is designed to help for such scenarios: delay loading data until they are actually requested.

Following code example demos such usage. A User class has an Address property, and the Address data are only loaded once when Address property is accessed:

class Program
{
class Address
{
public string Location { get; set; }
public string GeoCode { get; set; }
public Address(string loc, string geo)
{
this.Location = loc;
this.GeoCode = geo;
}
}

class User // User has Address property which is lazily loaded
{
public string ID { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }

public User(string id, string fName, string lName)
{
this.ID = id;
this.FirstName = fName;
this.LastName = lName;
_repoAddress = new Lazy<Address>(GetUserAddress);
}

private Lazy<Address> _repoAddress = null; // Lazy container loaded from repository
private Address _setAddress = null; // Container to setter
public Address Address
{
get
{
if (_setAddress == null) // Only get repository data if not being set
return _repoAddress.Value;
else
return _setAddress;
}
set
{
_setAddress = value;
}
}
private Address GetUserAddress() // Method to load the address data from repository
{
Console.WriteLine("...... Loading address for user {0} ......", this.ID);
return UserService.GetUserAddress(this.ID);
}
}

static List<Lazy<User>> _repoUserList = new List<Lazy<User>>(); // Fake User repository
static List<Lazy<Address>> _repoAddressList = new List<Lazy<Address>>(); // Fake Address repository
static void MockData() // Mock some sample data
{
for (int i = 1; i <= 10; i++)
{
string fakeID = i.ToString();
_repoUserList.Add(new Lazy<User>(() =>
{
Console.WriteLine("... Mocking User {0} ...", fakeID);
return new User(fakeID, "FName" + fakeID, "LName" + fakeID);
}));
_repoAddressList.Add(new Lazy<Address>(() =>
{
Console.WriteLine("... Mocking Address {0}...", fakeID);
return new Address("Location" + fakeID, "GeoCode" + fakeID);
}));
}
}

class UserService // Fake Service
{
public static Address GetUserAddress(string ID)
{
var addr = _repoAddressList.FirstOrDefault(a => a.Value.GeoCode == "GeoCode" + ID);
return (addr == null) ? null : new Address(addr.Value.Location, addr.Value.GeoCode);
}
public static User GetUserByID(string ID)
{
var user = _repoUserList.FirstOrDefault(u => u.Value.ID == ID);
return (user == null) ? null : new User(user.Value.ID, user.Value.FirstName, user.Value.LastName);
}
}

static void Main()
{
MockData();
User user = UserService.GetUserByID("5");
Console.WriteLine("The user's name: {0} {1}", user.FirstName, user.LastName);
Console.WriteLine("The user's address: {0} {1}", user.Address.Location, user.Address.GeoCode);
user.Address = new Address(user.Address.Location + "(new)", user.Address.GeoCode + "(new)");
Console.WriteLine("The user's updated address: {0} {1}", user.Address.Location, user.Address.GeoCode);
Console.Read();
}
}
Result:
... Mocking User 1 ...
... Mocking User 2 ...
... Mocking User 3 ...
... Mocking User 4 ...
... Mocking User 5 ...
The user's name: FName5 LName5
...... Loading address for user 5 ......
... Mocking Address 1 ...
... Mocking Address 2 ...
... Mocking Address 3 ...
... Mocking Address 4 ...
... Mocking Address 5 ...
The user's address: Location5 GeoCode5
The user's updated address: Location5(new) GeoCode5(new)
Above demo code searches a DAO User object from the mock data list, whose Address property is not loaded until its value gets accessed. Notice the private lazy<Address> property is only used in the getter method, and would be overridden by setter value so the custom value can be saved and passed back to service layer for further process.

A LINQ expression can be used to initiate the Lazy<T> as shown in the mock data population. We can see only the first 5 mocked Users were created because the fifth User was returned and the rest 5 mocked Users were not being accessed at all.

The last note is that all Lazy collections in above demo code are not thread-safe. To make Lazy<T> thread-safe, you need to pass a LazyExecutionMode parameter to its constructor, for detail refer to MSDN.

Monday, November 29, 2010

Powershell Scripts For WCF Site Deployment

A WCF service needs to be deployed to IIS in a web farm environment with 8 physical servers. To simplify the deployment work, I created Powershell scripts to do following job:
1. Check if powershell is installed or not. Prompt error and exit if powershell is not installed.
2. Check if IIS 7 powershell snap-in is installed or not. Install Powershell snap-in silently
if it's not installed.
3. Create application pool with desired identity
4. Create IIS Site using appPool created in step 3
5. Copy supporting files to related folders
6. Set permission for related folders
There are three scripts in total. The one to run is called Deploy.cmd:
@REM file: Deploy.cmd
@echo off
set PowershellExe="C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe"
set IISSnapin="C:\Program Files\IIS\PowerShellSnapin\IIsConsole.psc1"

echo.
if not exist %PowershellExe% (
echo "Powershell 2.0 is not installed. Please install it and try again."
endlocal
)

%PowershellExe% -command "& .\InstallPowershellSnapin.ps1"

if not exist %IISSnapin% (
echo "IIS 7.0 Powershell Snap-in is not installed. Please install it and try again."
endlocal
)

%PowershellExe% -psconsolefile %IISSnapin% -command "& .\InstallWcfService.ps1"
The powershell script to install the IIS 7 Snap-in:
# file: InstallPowershellSnapin.ps1

Set-ExecutionPolicy Unrestricted

$LogFile = "DeploymentLog.log"
$SnapinInstallLog = "IIS7PowerShellSnappin.log"
$SnapinFile = "C:\Program Files\IIS\PowerShellSnapin\IIsConsole.psc1"
$SnapinInstaller = "iis7psprov_x64.msi"

Start-Transcript $LogFile -Append -Force

write-host "Checking IIS 7 Powershell Snapin..."

if (! (Test-Path $SnapinFile))
{
write-host "Install IIS 7 Powershell Snap-in..."
if ( !(Test-Path $SnapinInstaller))
{
write-error "`nCan not find IIS 7 Powershell Snap-in."
exit
}

trap {
write-error "`nInstallation of IIS 7 Powershell Snap-in failed."
}
msiexec.exe /i iis7psprov_x64.msi -qn /l* $SnapinInstallLog
start-sleep -second 10
}

Stop-Transcript
The last Powershell script to setup IIS:
# file: InstallWcfService.ps1

$MachineName = "Machine123"
$LogFile = "DeploymentLog.log"
$SnappinFile = "C:\Program Files\IIS\PowerShellSnapin\IIsConsole.psc1"

$DeploymentSourceFolder = "DeployFiles"
$SitePath = "C:\inetpub\WCFService"

$SiteName = "WCF Service"
$PortNumber = 80
$AppPoolName = "WCF Service Pool"
$AppPoolAccount = "Domain1\User1"
$AppPoolPassword = "SecretPassword"
$AppPoolType = 3

Start-Transcript $LogFile -Append -Force

write-host "Checking Site Path..."
if ( !(Test-Path $SitePath))
{
write-host "Creating Site Path" $SitePath
trap {
write-error "Error in creating site path:" $_.Exception.Message
}
mkdir $SitePath > $null

$SitePathPermission = Get-Acl $SitePath
$AccessRight = New-Object system.security.accesscontrol.
filesystemaccessrule($AppPoolAccount,"FullControl","Allow")
$SitePathPermission.SetAccessRule($AccessRight)
Set-Acl $SitePath $SitePathPermission
}

write-host "Checking application pool..."
$pcAppPool = Get-Item IIS:\AppPools\$AppPoolName
if ( !$pcAppPool)
{
Write-Host "Creating application pool..."
trap {
write-error "Error in creating application pool:" $_.Exception.Message
}
new-webapppool -name $AppPoolName > $null

Write-Host "Configuring application pool..."
$pcAppPool = Get-Item IIS:\AppPools\$AppPoolName
$pcAppPool.processModel.userName = $AppPoolAccount
$pcAppPool.processModel.password = $AppPoolPassword
$pcAppPool.processModel.identityType = $AppPoolType
$pcAppPool.managedRuntimeVersion = "v4.0"
$pcAppPool | Set-Item
}

write-host "Checking IIS Site..."
if ( !(get-website | where {$_.Name -eq $SiteName}))
{
Write-Host "Creating Site..."
trap {
write-error "Error in creating IIS site:" $_.Exception.Message
}
New-Website -name $SiteName -ApplicationPool $AppPoolName -PhysicalPath $SitePath
-port $PortNumber -HostHeader $MachineName > $null
}

Write-Host "Copying deployment files.."
copy-item $DeploymentSourceFolder\* $SitePath -recurse -force

Write-Host "Deployment completed."
Stop-Transcript

Friday, October 29, 2010

WCF 4.0 CommunicatinException

Found an exception sometimes in WCF 4.0 communication with wsHttpBinding binding in a test environment:

CommunictionException: "An error occurred while receiving the HTTP response to ....svc. This could be due to the service endpoint binding not using the HTTP protocol. This could also be due to an HTTP request context being aborted by the server (possibly due to the service shutting down). See server logs for more details."

InnerExcpetion: "The underlying connection was closed: An unexpected error occurred on a receive."

InnerInnerException: "An existing connection was forcibly closed by the remote host"

NativeErrorCode: 10054

Turn on WCF logging in the server and found following error message using Microsoft Service Trace Viewer:

"There was an error while trying to serialize parameter http://tempuri.org/:Get...Result. The InnerException message was 'Maximum number of items that can be serialized or deserialized in an object graph is '65536'. Change the object graph or increase the MaxItemsInObjectGraph quota. '. Please see InnerException for more details."

Update server's web.config as suggested:
<system.serviceModel>
<behaviors>
<serviceBehaviors>
<behavior>
<!-- ... -->
<dataContractSerializer maxItemsInObjectGraph="2147483647" />
</behavior>
</serviceBehaviors>
</behaviors>
<!-- ... -->
</system.serviceModel>
Now get another exception in the client:
NetDispatcherFaultException: The formatter threw an exception while trying to deserialize the message: There was an error while trying to deserialize parameter http://tempuri.org/:Get...Result. The InnerException message was 'Maximum number of items that can be serialized or deserialized in an object graph is '65536'. Change the object graph or increase the MaxItemsInObjectGraph quota. '. Please see InnerException for more details.
Inner Exception: "Maximum number of items that can be serialized or deserialized in an object graph is '65536'. Change the object graph or increase the MaxItemsInObjectGraph quota. "

Update client's app.config:
<system.serviceModel>
<client>
<endpoint behaviorConfiguration="ServiceBehavior" _A_B_C_Setting... />
</client>
<behaviors>
<endpointBehaviors>
<behavior name="ServiceBehavior">
<dataContractSerializer maxItemsInObjectGraph="2147483647"/>
</behavior>
</endpointBehaviors>
</behaviors>
</system.serviceModel>
The exception is gone after configuration update.

Wednesday, September 29, 2010

.NET 4.0 Parallel IO Test

The new .NET 4.0 TPL simplifies the parallelizing programming. Would it speed up the I/O in reading and writing a large volumn of files? The answer is yes. Through a quick test in my desktop (Intel i5 760 CPU, 8G memory,Windows Server 2008 64-bit), I found that the parallel writing 10000 small files is about 4 times faster than that without TPL. Parallel loading all those 10000 files is about 6 times faster than that without using TPL. The test code is below:
using System;
using System.Collections.Generic;
using System.Text;
using System.Xml.Linq;
using System.IO;
using System.Diagnostics;
using System.Threading.Tasks;

class Program
{
static string path = @"c:\Test\";
static string fakeValue = GetFakeValue(1024);
static int fileNumber = 10000;
static void Main(string[] args)
{
CleanupFolder();
Stopwatch watch = new Stopwatch();

watch.Start();
SaveOneByOne();
watch.Stop();
Console.WriteLine("Saving {0} files serially takes {1} milli-seconds.",
fileNumber, watch.ElapsedMilliseconds);

watch.Restart();
ReadOneByOne();
watch.Stop();
Console.WriteLine("Reading {0} files serially takes {1} milli-seconds.",
fileNumber, watch.ElapsedMilliseconds);

CleanupFolder();
watch.Restart();
SaveInParallel();
watch.Stop();
Console.WriteLine("Saving {0} files in parallel takes {1} milli-seconds.",
fileNumber, watch.ElapsedMilliseconds);

watch.Restart();
ReadInParallel();
watch.Stop();
Console.WriteLine("Reading {0} files in parallel takes {1} milli-seconds.",
fileNumber, watch.ElapsedMilliseconds);

Console.Read();
}

static void SaveOneByOne()
{
for (int i = 0; i < fileNumber; i++)
{
XDocument xfile = GetXDocument(i.ToString(), fakeValue);
xfile.Save(path + i.ToString() + ".xml");
}
}

static void ReadOneByOne()
{
List<XDocument> files = new List<XDocument>();
for (int i = 0; i < fileNumber; i++)
{
XDocument xfile = XDocument.Load(path + i.ToString() + ".xml");
files.Add(xfile);
}
}

static void SaveInParallel()
{
Parallel.For(0, fileNumber, delegate(int i)
{
XDocument xfile = GetXDocument(i.ToString(), fakeValue);
xfile.Save(path + i.ToString() + ".xml");
});
}

static void ReadInParallel()
{
List<XDocument> files = new List<XDocument>();
Parallel.For(0, fileNumber, delegate(int i)
{
XDocument xfile = XDocument.Load(path + i.ToString() + ".xml");
files.Add(xfile);
});
}

static void CleanupFolder()
{
if (!Directory.Exists(path))
{
Directory.CreateDirectory(path);
}
else
{
//Array.ForEach(Directory.GetFiles(path), (string file) => File.Delete(file));
Directory.Delete(path, true);
Directory.CreateDirectory(path);
}
}

static XDocument GetXDocument(string key, string value)
{
return new XDocument(new XDeclaration("1.0", "utf-8", "yes"),
new XElement("Items",
new XElement("Item",
new XElement("Key", key),
new XElement("Value", value))));
}

static string GetFakeValue(int length)
{
StringBuilder sb = new StringBuilder();
string values = "abcdefghijklmnopqrstuvwxyz0123456789";
Random rand = new Random();
for (int i = 0; i < length; i++)
{
sb.Append(values[rand.Next(0, 35)]);
}
return sb.ToString();
}
}
Result:
Saving 10000 files serially takes 29528 milli-seconds.
Reading 10000 files serially takes 2389 milli-seconds.
Saving 10000 files in parallel takes 8072 milli-seconds.
Reading 10000 files in parallel takes 386 milli-seconds.
The results vary slightly in different tests and they are consistent overall.

Monday, August 30, 2010

SharePoint 2010 And SharePoint 2007 Content Database

SharePoint 2007 content database can be used by SharePoint 2010 directly without major problems. You can created a site collection in SharePoint 2007, copy its content database to the SharePoint 2010 environment, configure your SharePoint 2010 site collection content database to it, and you are done! Andrew Connell gave detailed instructions on how to do this with WCM sites.

That implies there're no big difference between SharePoint 2010 and SharePoint 2007 in terms of back-end database schema.

I checked briefly and found following tables existing in SharePoint 2007 but not in SharePoint 2010:
  1. Categories
  2. Image0x
  3. WebCat
  4. WebParts
More than 10 tables are added to SharePoint 2010 content database:
  1. AllfileFragments
  2. AllListsAux
  3. AllListsPlus
  4. AllListsUniqueFields
  5. AllLookupRelateionships
  6. AllWebParts
  7. CustomActions
  8. FeatureTracking
  9. Resources
  10. SharedAccessRequests
  11. SolutionResourceUsageDaily
  12. SolutionResourceUsageDailyOrdinal
  13. SolutionResourceUsageLog
  14. SolutionResourceUsageWindowed
  15. Solutions
  16. WebsPlus

Tuesday, August 03, 2010

RadEditor for MOSS 2007 - Assembly Binding

Just a quick note that it's possible to upgrade Telerik RadEditor for MOSS to a new version without changing registry on Page Layout and reference on VS project. The magic is the assembly binding. For example, if you want to upgrade the RadeEditor from version 5.6.2.0 to 5.8.0.0, first you need to install and deploy the new RadEditor solution package, then add following to the web.config file:
<configuration>
...
<runtime>
...
<dependentAssembly>
<assemblyIdentity name="RadEditorSharePoint" publicKeyToken="1f131a624888eeed" />
<bindingRedirect oldVersion="5.6.2.0" newVersion="5.8.0.0" />
</dependentAssembly>
<dependentAssembly>
<assemblyIdentity name="Telerik.Web.UI" publicKeyToken="121fae78165ba3d4" />
<bindingRedirect oldVersion="2009.3.1314.20" newVersion="2010.2.713.20" />
</dependentAssembly>
</assemblyBinding>
</runtime>
...
</configuration>

RadEditor for MOSS 2007 - Spell-check Multi-language Support

We use Telerik RadEditor for MOSS (5.6.2.0) as a cross-browser rich-text editor in one MOSS WCM publishing site to replace out-of-box Rich HTML field control. One great feature of the RadEditor is its AJAX spell checking function. By default, it would search against all three default dictionaries, English (en-US), French(fr-FR) and German(de-DE) that are included in solution package.

Is it possible to spell check based on the context that is selected by the end user? The answer is yes. Following are the setup steps:

1. Install and deploy RadEditorMoss solution package.

2. Open up related Page Layout and replace the Rich HTML Field Controls (or text fields) by the RadEditor Control:
  • Register Telerik assembly in Page Layout

    <%@ Register TagPrefix="telerik" Namespace="Telerik.SharePoint.FieldEditor" Assembly="RadEditorSharePoint, Version=5.6.2.0, culture=neutral, PublicKeyToken=1f131a624888eeed" %>

  • Replace the tag of the default editor with the RadeEditor control:

    <telerik:RadHtmlField id="PageContent" FieldName="PageContent" runat="server">telerik:RadHtmlField>

3. (Optional) Download dictionaries from web site http://www.telerik.com/community/forums/aspnet-ajax/spell/147971-radspell-dictionaries.aspx, and save dictionaries with tdf extension to RadEditor dictionary folder (C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\wpresources\RadEditorSharePoint\5.6.2.0__1f131a624888eeed\Resources\App_Data\RadSpell).

4. Inside RadEditor global resource folder (C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\wpresources\RadEditorSharePoint\5.6.2.0__1f131a624888eeed\Resources), update ToolsFile.XML and add Languages element just before </root> tag (assume that both en-Us.tdf and fr-FR.tdf are installed):
<root>

...
<tools name="MossTools1">
<tool name="AjaxSpellCheck" />
</tools>
...
<languages>
<language code="en-US" title="English"></language>
<language code="fr-FR" title="French"></language>
</languages>
</root>

5. Now you have option to select English or French for spell-check on the toolbar on each RadEditor instance, for example, you want to check English on PageContent text, and check French on PageContentFrench text:

RadEditor for MOSS 2007 - Re-package

Telerik’s RadEditor for SharePoint includes two versions, a free Lite version that is similar to the default rich-text editor with addition of providing cross-browser support, and a full version with more features such as AJAX spell-checking. Here RadEditor for MOSS 5.6.2.0 full version is used for our discussion.

After installation, many RadEditor settings, such as toolbar size, can be configured in ConfigFile.xml, ToolsFile.xml, ListConfigFile.xml, and ListConfigFile.xml under RadEditor's global resource folder; (C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\wpresources\RadEditorSharePoint\5.6.2.0__1f131a624888eeed\Resources). The details of settings can be found in http://www.telerik.com/help/aspnet-ajax/moss-introduction.html.

Manually changing the configuration files is cumbersome. Consider an environment of a QA farm, a staging farm, an authoring farm and a production farm, you have to update each machine one by one. The better way is to change the configuration and then re-package to a new wsp for deployment.

Steps to re-package RadEditorMOSS.wsp:

1. Rename the original RadEditorMOSS.wsp to RadEditorMOSS.cab.

2. Extract all files from RadEditorMOSS.cab to a RadEditorMOSS folder using any unzip tool such as 7-zip; remain the original folder structure in extraction. Notice all configuration files we mentioned above are located in RadEditorMOSS/Resources folder:


3.Update configuration files as required.

4.Create a repack.ddf file in the RadEditorMOSS extraction folder. The ddf diamond file has following instruction:
.OPTION Explicit
.Set DiskDirectoryTemplate=CDROM
.Set MaxDiskSize=CDROM
.Set CompressionType=MSZIP
.Set UniqueFiles=On
.Set Cabinet=On
;.Set DiskDirectory1=Package
.Set CabinetNameTemplate="RadMOSSEditorRepack.wsp"

;******************************

manifest.xml
RadEditorSharePoint.dll
RadEditorSharePoint.dwp
Telerik.Web.UI.dll

.Set DestinationDir=CONTROLTEMPLATES
CONTROLTEMPLATES\TelerikRadEditorHtml.ascx
CONTROLTEMPLATES\TelerikRadEditorText.ascx

.Set DestinationDir=Images
Images\RadEditor.gif

.Set DestinationDir=RadEditorFeatureRichHtml
RadEditorFeatureRichHtml\Feature.xml

.Set DestinationDir=RadEditorFeatureRichText
RadEditorFeatureRichText\Feature.xml

.Set DestinationDir=Resources
Resources\ConfigFile.xml
Resources\EditorToolPart.js
Resources\ListConfigFile.xml
Resources\ListToolsFile.xml
Resources\MOSSEditorTools.js
Resources\Telerik.Web.UI.DialogHandler.aspx
Resources\Telerik.Web.UI.SpellCheckHandler.ashx
Resources\ToolsFile.xml
Resources\web.config

.Set DestinationDir=Resources\App_Data\RadSpell
Resources\App_Data\RadSpell\de-DE.tdf
Resources\App_Data\RadSpell\en-US.tdf
Resources\App_Data\RadSpell\fr-FR.tdf

.Set DestinationDir=Resources\App_GlobalResources
Resources\App_GlobalResources\RadEditor.Dialogs.de-DE.resx
Resources\App_GlobalResources\RadEditor.Dialogs.fr-FR.resx
Resources\App_GlobalResources\RadEditor.Dialogs.resx
Resources\App_GlobalResources\RadEditor.Main.de-DE.resx
Resources\App_GlobalResources\RadEditor.Main.fr-FR.resx
Resources\App_GlobalResources\RadEditor.Main.resx
Resources\App_GlobalResources\RadEditor.Modules.de-DE.resx
Resources\App_GlobalResources\RadEditor.Modules.fr-FR.resx
Resources\App_GlobalResources\RadEditor.Modules.resx
Resources\App_GlobalResources\RadEditor.Tools.de-DE.resx
Resources\App_GlobalResources\RadEditor.Tools.fr-FR.resx
Resources\App_GlobalResources\RadEditor.Tools.resx
Resources\App_GlobalResources\RadListBox.resx
Resources\App_GlobalResources\RadProgressArea.resx
Resources\App_GlobalResources\RadScheduler.Main.resx
Resources\App_GlobalResources\RadSpell.Dialog.de-DE.resx
Resources\App_GlobalResources\RadSpell.Dialog.fr-FR.resx
Resources\App_GlobalResources\RadSpell.Dialog.resx
Resources\App_GlobalResources\RadUpload.resx

.Set DestinationDir=Resources\ToolImages
Resources\ToolImages\MOSSDeleteTableElement.gif
Resources\ToolImages\MOSSHelp.gif
Resources\ToolImages\MOSSImageManager.gif
Resources\ToolImages\MOSSInsertTable.GIF
Resources\ToolImages\MOSSInsertTableElement.gif
Resources\ToolImages\MOSSLeftToRight.GIF
Resources\ToolImages\MOSSLinkManager.gif
Resources\ToolImages\MOSSRightToLeft.GIF
Resources\ToolImages\MOSSTemplateManager.gif
Resources\ToolImages\RadEditorStyles_Black.css
Resources\ToolImages\RadEditorStyles_Default.css
Resources\ToolImages\RadEditorStyles_Forest.css
Resources\ToolImages\RadEditorStyles_Hay.css
Resources\ToolImages\RadEditorStyles_Office2007.css
Resources\ToolImages\RadEditorStyles_Outlook.css
Resources\ToolImages\RadEditorStyles_Sunset.css
Resources\ToolImages\RadEditorStyles_Telerik.css
Resources\ToolImages\RadEditorStyles_Vista.css
Resources\ToolImages\RadEditorStyles_Web20.css
Resources\ToolImages\RadEditorStyles_WebBlue.css
Resources\ToolImages\ToggleSource.GIF
4.In command line mode go to RadEditorMOSS extraction folder, type:
makecab /f repack.ddf
5.A new solution package with name of "RadMOSSEditorRepack.wsp" will be created. All installation with this RadMOSSEditorRepack.wsp will have the same custom configuration.