zoukankan      html  css  js  c++  java
  • .net WCF WF4.5 状态机、书签与持久化

    想看源码请直接翻到最后,使用方式如下图

     使用方式

    如果同时需要多个书签可以直接在需要的位置创建书签,会认为是同一个实例。

    若需要实现的效果是同时需要好几个部门审核,那么只要在对应的位置同时创建多个书签即可。

    编写书签代码活动

    注意几个地方,创建书签需要继承NativeActivity,随之要修改的就是Execute的参数类型

    如下所有注释部分

    public sealed class BookmarkActivity : NativeActivity
        {
            // 定义一个字符串类型的活动输入参数
            public InArgument<string> Text { get; set; }
            public OutArgument<string> bookMark{get;set;}
    
            // 如果活动返回值,则从 CodeActivity<TResult>
            // 并从 Execute 方法返回该值。
    
            //2.修改上下文类型
            protected override void Execute(NativeActivityContext context)
            {
                // 获取 Text 输入参数的运行时值
                string text = context.GetValue(this.Text);
    
                //3.创建书签并设置书签恢复会的回调函数
                context.CreateBookmark("name", new BookmarkCallback(final));
            }
            //4.重写CanInduceIdle
            protected override bool CanInduceIdle
            {
                get
                {
                    return true;
                }
            }
            //5.通过恢复书签时自动调用此函数来将书签传递给外部变量
            protected  void final(NativeActivityContext context,Bookmark bookmark,object obj)
            {
                Dictionary<string, object> o = (Dictionary<string, object>)obj;
                context.SetValue(bookMark, context.ActivityInstanceId);
            }

    1.使用WorkflowApplication启动活动

    //与主线程同步
    AutoResetEvent syncEvent = new AutoResetEvent(false);
    
                var act = new Activity1();
                Dictionary<string, object> dictionary = new Dictionary<string, object>();
                dictionary.Add("pic","1");
                WorkflowApplication app = new WorkflowApplication(act, dictionary);
                //运行活动
                app.Run();
                //等待信号
                syncEvent.WaitOne();        

     2.持久化的sql路径

    C:WindowsMicrosoft.NETFrameworkv4.0.30319SQLen 
    
    

     3.添加程序集

    
    

     3.添加引用

    using System.Activities.DurableInstancing;

    4.在合适位置绑定数据持久化使用的数据库

    SqlWorkflowInstanceStore store =
        new SqlWorkflowInstanceStore(@"Server=DESKTOP-OIGV51OSQLEXPRESS;database=WFTest;uid=sa;pwd=123");
    //绑定数据库
                app.InstanceStore = store;

    到这一步数据库中便已经能够查看到数据了,接下来要在需要的位置获取我们的书签

     5.继续执行后续步骤

                //继续执行
    
                //此处使用数据库中工作流的ID,而不是使用书签名
                app.Load(Guid.Parse(textBox2.Text));
    
                app.ResumeBookmark(textBox1.Text,null);

    6.绑定生命周期事件

    #region 工作流生命周期事件
                app.Unloaded = delegate (WorkflowApplicationEventArgs er)
                {
                    Console.WriteLine("工作流 {0} 卸载.", er.InstanceId);
                };
                app.Completed = delegate (WorkflowApplicationCompletedEventArgs er)
                {
                    //textBox1.Text = er.Outputs["arg1"].ToString();
                    syncEvent.Set();
                };
                app.Aborted = delegate (WorkflowApplicationAbortedEventArgs er)
                {
                    Console.WriteLine("工作流 {0} 终止.", er.InstanceId);
                };
                app.Idle = delegate (WorkflowApplicationIdleEventArgs er)
                {
    
                    Console.WriteLine("工作流 {0} 空闲.", er.InstanceId);
                    syncEvent.Set(); //这里要唤醒,不让的话,当创建了一个书签之后,界面就卡死了。
                };
                app.PersistableIdle = delegate (WorkflowApplicationIdleEventArgs er)
                {
                    var bookmarks = er.Bookmarks;
                    var instanceID = er.InstanceId;
                    //textBox1.Text = er.["arg1"].ToString();
                    Console.WriteLine("持久化");
                    return PersistableIdleAction.Unload;
                };
                app.OnUnhandledException = delegate (WorkflowApplicationUnhandledExceptionEventArgs er)
                {
                    Console.WriteLine("OnUnhandledException in Workflow {0}
    {1}",
           er.InstanceId, er.UnhandledException.Message);
                    return UnhandledExceptionAction.Terminate;
                };
                #endregion

    代码:下载

    上一篇:.net WCF WF4.5    下一篇:WCF使用相关

  • 相关阅读:
    C语言-if语句
    C语言-表达式
    C语言-基础
    Java for LeetCode 187 Repeated DNA Sequences
    Java for LeetCode 179 Largest Number
    Java for LeetCode 174 Dungeon Game
    Java for LeetCode 173 Binary Search Tree Iterator
    Java for LeetCode 172 Factorial Trailing Zeroes
    Java for LeetCode 171 Excel Sheet Column Number
    Java for LeetCode 169 Majority Element
  • 原文地址:https://www.cnblogs.com/ives/p/WF_BOOKMARK.html
Copyright © 2011-2022 走看看