workflow中定义的规则集会以单独的文件保存,以.rules为后缀,名称与工作流名称一致。
假设有个名为SellItemWorkflow的workflow,并且在里面定义了一个名为CalculateItemTotals的规则集。
在其他工作流调用该规则集(SellItemWorkflow.rules):
Stream stream = typeof(SellItemWorkflow).Module.Assembly.GetManifestResourceStream(typeof(SellItemWorkflow), "SellItemWorkflow.rules"); //获取规则集
using (XmlReader xmlReader = XmlReader.Create(new StreamReader(stream)))
{
WorkflowMarkupSerializer mark = new WorkflowMarkupSerializer();
RuleDefinitions ruleDefinitions = mark.Deserialize(xmlReader) as RuleDefinitions; //反序列化
if (ruleDefinitions != null) //返回规则集的集合
{
if (ruleDefinitions.RuleSets.Contains("CalculateItemTotals")) //要找的规则集
{
RuleSet rs = ruleDefinitions.RuleSets["CalculateItemTotals"];
RuleValidation validation = new RuleValidation(typeof(Workflow1), null); //验证规则集对某种特定的对象类型是否有效
if (rs.Validate(validation)) //验证通过
{
RuleExecution execution = new RuleExecution(validation, this); //执行包装器
rs.Execute(execution); //对目标工作流执行该规则集
}
else
{
foreach (ValidationError error in validation.Errors)
{
Console.WriteLine(error.ErrorText);
}
}
}
}
}