在此示例中,将促销代码和孩子数量作为变量传递到 Flowchart 活动,该活动将根据与促销代码相对应的公式计算折扣。此示例分为命令性代码版本和工作流设计器版本。
下表详细描述了 CreateFlowchartWithFaults
活动的变量。
参数 | 说明 |
---|---|
promoCode | 促销代码。类型:String 可能的值,括号中带有说明:
|
numKids | 孩子数量。类型:int |
CreateFlowchartWithFaults
活动使用 FlowSwitch 活动,后者根据 promoCode
参数进行切换并使用以下公式计算折扣。
promoCode 的值 | 折扣 (%) |
---|---|
Single | 10 |
MNK | 15 |
MWK | 15 + (1 – 1/ ![]()
此计算可能会引发 DivideByZeroException。因此,将折扣计算包装在 TryCatch 活动中,该活动可捕获 DivideByZeroException 异常并将折扣设置为零。
|
class Program
{
static void Main(string[] args)
{
// no fault expected
Console.WriteLine("Invoke with Promo Code {0}, number of kids {1}", "Single", 0);
IDictionary<string, object> inputs = new Dictionary<string, object>() { { "promoCode", "Single" }, { "numberOfKids", 0 } };
WorkflowInvoker.Invoke(new PromotionalDiscountWorkflow(), inputs);
// no fault expected
Console.WriteLine("Invoke with Promo Code {0}, number of kids {1}", "MNK", 0);
inputs = new Dictionary<string, object>() { { "promoCode", "MNK" }, { "numberOfKids", 0 } };
WorkflowInvoker.Invoke(new PromotionalDiscountWorkflow(), inputs);
// no fault expected
Console.WriteLine("Invoke with Promo Code {0}, number of kids {1}", "MWK", 2);
inputs = new Dictionary<string, object>() { { "promoCode", "MWK" }, { "numberOfKids", 2 } };
WorkflowInvoker.Invoke(new PromotionalDiscountWorkflow(), inputs);
//fault expected
Console.WriteLine("Invoke with Promo Code {0}, number of kids {1}", "MWK", 0);
inputs = new Dictionary<string, object>() { { "promoCode", "MWK" }, { "numberOfKids", 0 } };
WorkflowInvoker.Invoke(new PromotionalDiscountWorkflow(), inputs);
// wait for user input
Console.WriteLine("\nPress enter to exit");
Console.ReadLine();
}
}
<ClipboardData Metadata="{x:Null}" Version="1.0" xmlns="http://schemas.microsoft.com/netfx/2009/xaml/activities/presentation" xmlns:av="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:mva="clr-namespace:Microsoft.VisualBasic.Activities;assembly=System.Activities" xmlns:p="http://schemas.microsoft.com/netfx/2009/xaml/activities" xmlns:s="clr-namespace:System;assembly=mscorlib" xmlns:s1="clr-namespace:System;assembly=mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" xmlns:sa="clr-namespace:System.Activities;assembly=System.Activities, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" xmlns:sad="clr-namespace:System.Activities.Debugger;assembly=System.Activities" xmlns:scg="clr-namespace:System.Collections.Generic;assembly=mscorlib" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<ClipboardData.Data>
<scg:List x:TypeArguments="x:Object" Capacity="7">
<p:Flowchart VirtualizedContainerService.HintSize="1101,636" mva:VisualBasic.Settings="Assembly references and imported namespaces serialized as XML namespaces">
<WorkflowViewStateService.ViewState>
<scg:Dictionary x:TypeArguments="x:String, x:Object">
<x:Boolean x:Key="IsExpanded">False</x:Boolean>
<av:Point x:Key="ShapeLocation">270,7.5</av:Point>
<av:Size x:Key="ShapeSize">60,75</av:Size>
<av:PointCollection x:Key="ConnectorLocation">330,45 540,45 540,107.5</av:PointCollection>
<x:Double x:Key="Width">1086.998333333333</x:Double>
</scg:Dictionary>
</WorkflowViewStateService.ViewState>
<p:Flowchart.StartNode>
<x:Reference>__ReferenceID5</x:Reference>
</p:Flowchart.StartNode>
<p:FlowSwitch x:TypeArguments="x:String" x:Name="__ReferenceID5" Expression="[promoCode]" VirtualizedContainerService.HintSize="60,75">
<p:FlowSwitch.Default>
<p:FlowStep x:Name="__ReferenceID1">
<WorkflowViewStateService.ViewState>
<scg:Dictionary x:TypeArguments="x:String, x:Object">
<av:Point x:Key="ShapeLocation">843.998333333333,286.5</av:Point>
<av:Size x:Key="ShapeSize">243,59</av:Size>
<av:PointCollection x:Key="ConnectorLocation">965.498333333333,345.5 965.498333333333,511.861666666667 657.83,511.861666666667</av:PointCollection>
</scg:Dictionary>
</WorkflowViewStateService.ViewState>
<p:Assign VirtualizedContainerService.HintSize="243,59">
<p:Assign.To>
<p:OutArgument x:TypeArguments="x:Double">[discount]</p:OutArgument>
</p:Assign.To>
<p:Assign.Value>
<p:InArgument x:TypeArguments="x:Double">[0.0R]</p:InArgument>
</p:Assign.Value>
</p:Assign>
<p:FlowStep.Next>
<x:Reference>__ReferenceID0</x:Reference>
</p:FlowStep.Next>
</p:FlowStep>
</p:FlowSwitch.Default>
<WorkflowViewStateService.ViewState>
<scg:Dictionary x:TypeArguments="x:String, x:Object">
<av:Point x:Key="ShapeLocation">510,107.5</av:Point>
<av:Size x:Key="ShapeSize">60,75</av:Size>
<av:PointCollection x:Key="Default">570,145 965,145 965,286.5</av:PointCollection>
<av:PointCollection x:Key="SingleConnector">570,163.75 685,163.75 685,286.5</av:PointCollection>
<av:PointCollection x:Key="MNKConnector">510,163.75 410,163.75 410,286.5</av:PointCollection>
<av:PointCollection x:Key="MWKConnector">510,145 155,145 155,285.361666666667</av:PointCollection>
<av:PointCollection x:Key="Case1Connector">570,163.75 685,163.75 685,286.5</av:PointCollection>
</scg:Dictionary>
</WorkflowViewStateService.ViewState>
<p:FlowStep x:Key="MWK" x:Name="__ReferenceID4">
<WorkflowViewStateService.ViewState>
<scg:Dictionary x:TypeArguments="x:String, x:Object">
<av:Point x:Key="ShapeLocation">55,285.361666666667</av:Point>
<av:Size x:Key="ShapeSize">200,53</av:Size>
<av:PointCollection x:Key="ConnectorLocation">155,338.361666666667 155,511.861666666667 444.83,511.861666666667</av:PointCollection>
</scg:Dictionary>
</WorkflowViewStateService.ViewState>
<p:TryCatch VirtualizedContainerService.HintSize="200,53">
<p:TryCatch.Try>
<p:If Condition="[numberOfKids = 0]">
<p:If.Then>
<p:Throw Exception="[New System.DivideByZeroException ()]" />
</p:If.Then>
<p:If.Else>
<p:Assign>
<p:Assign.To>
<p:OutArgument x:TypeArguments="x:Double">[discount]</p:OutArgument>
</p:Assign.To>
<p:Assign.Value>
<p:InArgument x:TypeArguments="x:Double">[15 + (1 - 1 / numberOfKids) * 10]</p:InArgument>
</p:Assign.Value>
</p:Assign>
</p:If.Else>
</p:If>
</p:TryCatch.Try>
<p:TryCatch.Catches>
<p:Catch x:TypeArguments="s:DivideByZeroException">
<p:ActivityAction x:TypeArguments="s:DivideByZeroException">
<p:Sequence>
<p:WriteLine Text="["DivideByZeroException: Promo code is MWK - but number of kids = 0"]" />
<p:Assign>
<p:Assign.To>
<p:OutArgument x:TypeArguments="x:Double">[discount]</p:OutArgument>
</p:Assign.To>
<p:Assign.Value>
<p:InArgument x:TypeArguments="x:Double">[0.0R]</p:InArgument>
</p:Assign.Value>
</p:Assign>
</p:Sequence>
</p:ActivityAction>
</p:Catch>
</p:TryCatch.Catches>
</p:TryCatch>
<p:FlowStep.Next>
<p:FlowStep x:Name="__ReferenceID0">
<WorkflowViewStateService.ViewState>
<scg:Dictionary x:TypeArguments="x:String, x:Object">
<av:Point x:Key="ShapeLocation">444.83,480.361666666667</av:Point>
<av:Size x:Key="ShapeSize">213,63</av:Size>
</scg:Dictionary>
</WorkflowViewStateService.ViewState>
<p:WriteLine VirtualizedContainerService.HintSize="213,63" Text="["Discount calculated = " & discount]" />
</p:FlowStep>
</p:FlowStep.Next>
</p:FlowStep>
<p:FlowStep x:Key="MNK" x:Name="__ReferenceID3">
<WorkflowViewStateService.ViewState>
<scg:Dictionary x:TypeArguments="x:String, x:Object">
<av:Point x:Key="ShapeLocation">288.998333333333,286.5</av:Point>
<av:Size x:Key="ShapeSize">243,59</av:Size>
<av:PointCollection x:Key="ConnectorLocation">410.498333333333,345.5 410.498333333333,375.5 551.33,375.5 551.33,480.361666666667</av:PointCollection>
</scg:Dictionary>
</WorkflowViewStateService.ViewState>
<p:Assign VirtualizedContainerService.HintSize="243,59">
<p:Assign.To>
<p:OutArgument x:TypeArguments="x:Double">[discount]</p:OutArgument>
</p:Assign.To>
<p:Assign.Value>
<p:InArgument x:TypeArguments="x:Double">[15.0R]</p:InArgument>
</p:Assign.Value>
</p:Assign>
<p:FlowStep.Next>
<x:Reference>__ReferenceID0</x:Reference>
</p:FlowStep.Next>
</p:FlowStep>
<p:FlowStep x:Key="Single" x:Name="__ReferenceID2">
<WorkflowViewStateService.ViewState>
<scg:Dictionary x:TypeArguments="x:String, x:Object">
<av:Point x:Key="ShapeLocation">563.998333333333,286.5</av:Point>
<av:Size x:Key="ShapeSize">243,59</av:Size>
<av:PointCollection x:Key="ConnectorLocation">685.498333333333,345.5 685.498333333333,375.5 551.33,375.5 551.33,480.361666666667</av:PointCollection>
</scg:Dictionary>
</WorkflowViewStateService.ViewState>
<p:Assign VirtualizedContainerService.HintSize="243,59">
<p:Assign.To>
<p:OutArgument x:TypeArguments="x:Double">[discount]</p:OutArgument>
</p:Assign.To>
<p:Assign.Value>
<p:InArgument x:TypeArguments="x:Double">[10.0R]</p:InArgument>
</p:Assign.Value>
</p:Assign>
<p:FlowStep.Next>
<x:Reference>__ReferenceID0</x:Reference>
</p:FlowStep.Next>
</p:FlowStep>
</p:FlowSwitch>
<x:Reference>__ReferenceID1</x:Reference>
<x:Reference>__ReferenceID2</x:Reference>
<x:Reference>__ReferenceID3</x:Reference>
<x:Reference>__ReferenceID4</x:Reference>
<x:Reference>__ReferenceID0</x:Reference>
</p:Flowchart>
</scg:List>
</ClipboardData.Data>
</ClipboardData>