[WF4实践一] 使用 TryCatch 在 Flowchart 活动中进行错误处理

在此示例中,将促销代码和孩子数量作为变量传递到 Flowchart 活动,该活动将根据与促销代码相对应的公式计算折扣。此示例分为命令性代码版本和工作流设计器版本。

下表详细描述了 CreateFlowchartWithFaults 活动的变量。

参数说明

promoCode

促销代码。类型:String

可能的值,括号中带有说明:

  • Single(单身)

  • MNK(已婚但没有孩子。)

  • MWK(已婚并且有孩子。)

numKids

孩子数量。类型:int

CreateFlowchartWithFaults 活动使用 FlowSwitch 活动,后者根据 promoCode 参数进行切换并使用以下公式计算折扣。

promoCode 的值折扣 (%)

Single

10

MNK

15

MWK

15 + (1 – 1/numberOfKids)*10

Dd807383.note(zh-cn,VS.100).gif注意:
此计算可能会引发 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="[&quot;DivideByZeroException: Promo code is MWK - but number of kids = 0&quot;]" />
                      <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="[&quot;Discount calculated = &quot; &amp; 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>

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值