博客园  :: 首页  :: 联系 :: 订阅 订阅  :: 管理

WF4 ViewStateService和补偿CompensableActivity

Posted on 2009-12-07 21:24  生鱼片  阅读(1623)  评论(0编辑  收藏  举报

标题的两者并没有什么关系,只是内容都比较短就放到一起了。

1.在工作流中存储ModelItems的视图状态。

例子,   我们创建一个自定义活动的活动设计器ActivityDesigner1.XAML,如下:

<sap:ActivityDesigner x:Class="CaryActivityLibrary1.ActivityDesigner1"

    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"

    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"

    xmlns:sap="clr-namespace:System.Activities.Presentation;assembly=System.Activities.Presentation"

    xmlns:sapv="clr-namespace:System.Activities.Presentation.View;assembly=System.Activities.Presentation">

    <Grid>

        <StackPanel Name="stackPanel1" VerticalAlignment="Top" >

            <TextBox  Name="commentBlock"   />

            <Button Content="Load View State" Name="loadViewState" Click="loadViewState_Click" />

            <Button Content="Commit View State"  Name="button1" Click="button1_Click" />

        </StackPanel>

    </Grid>

</sap:ActivityDesigner>

 

对应的代码如下:

private void loadViewState_Click(object sender, RoutedEventArgs e)

        {

            ViewStateService vss = this.Context.Services.GetService<ViewStateService>();

            commentBlock.Text = vss.RetrieveViewState(this.ModelItem, "comment") as string;

        }

 

        private void button1_Click(object sender, RoutedEventArgs e)

        {

            ViewStateService vss = this.Context.Services.GetService<ViewStateService>();

            vss.StoreViewStateWithUndo(this.ModelItem, "comment", commentBlock.Text);

        }

我们在工作流中使用该活动,输入一些信息后提交ViewState,你会看到工作流中增加了如下部分:

<sap:WorkflowViewStateService.ViewState>

      <scg3:Dictionary x:TypeArguments="x:String, x:Object">

        <x:String x:Key="comment">123456</x:String>

      </scg3:Dictionary>

    </sap:WorkflowViewStateService.ViewState>

2.补偿

补偿是提供给前面已经成功的操作一个补偿的机会,WF4提供了下面活动支持补偿:

image
1. CompensableActivity活动,该活动调用后会返回一个CompensationToken类型的Result属性,
2. Compensate活动和Confirm活动用于显示的调用CompensableActivity活动的confirmation handler 和 compensation handler部分。这两个活动都有一个Target属性用于指定CompensableActivity活动的Result属性。
3. CompensableActivity活动的CompenationHandler当工作流有异常时调用,ConfirmationHandler为工作流正常执行完成后调用不是CompensableActivity活动执行完成就立即调用,CancellationHandler为活动取消时调用。

参考资源:

1.http://blogs.msdn.com/mwinkle/archive/2009/12/06/wf4-viewstateservice.aspx
2.http://msdn.microsoft.com/en-us/library/system.activities.presentation.view.viewstateservice_members(VS.100).aspx
3.http://msdn.microsoft.com/en-us/library/dd489415(VS.100).aspx