WF持久性有两个书签。无法坚持第二个书签

时间:2016-09-27 12:49:13

标签: c# .net workflow workflow-foundation-4 workflow-activity

我正在创建工作流application with persistent behaviour and two bookmarks.

我无法为第二个书签保留工作流运行时。

工作流程声明

1- Start workflow
2- Ask user to enter name
3- Create bookmark
4- Resume bookmark on receiving input from user and show it on UI
5- Ask user again to enter number
6- Create bookmark and wait for user input // Unable to persist at this point
7- Resume bookmark on receiving input from user and show it on UI

自定义活动

public class WaitForInput<TResult> : NativeActivity<TResult>
{
    [RequiredArgument]
    public InArgument<string> BookmarkName { get; set; }

    // indicate to the runtime that this activity can go idle
    protected override bool CanInduceIdle
    {
        get { return true; }
    }

    protected override void Execute(NativeActivityContext context)
    {                        
        context.CreateBookmark(this.BookmarkName.Get(context), new BookmarkCallback(OnReadComplete));
    }

    void OnReadComplete(NativeActivityContext context, Bookmark bookmark, object state)
    {            
        this.Result.Set(context, (TResult)state);
    }
}

Program.cs的

class Program
{
    static AutoResetEvent syncEvent = new AutoResetEvent(false);
    static Guid id;

    static void Main(string[] args)
    {
        WorkflowApplication app = new WorkflowApplication(new Sequence1());
        InstanceStore store = new SqlWorkflowInstanceStore(@"Data Source=.\SQLEXPRESS;Initial Catalog=WF45GettingStartedTutorial;Integrated Security=True");
        InstanceHandle handle = store.CreateInstanceHandle();
        InstanceView view = store.Execute(handle, new CreateWorkflowOwnerCommand(), TimeSpan.FromSeconds(30));
        handle.Free();
        store.DefaultInstanceOwner = view.InstanceOwner;
        app.InstanceStore = store;


        app.PersistableIdle = delegate(WorkflowApplicationIdleEventArgs e)
        {
            return PersistableIdleAction.Unload;

        };

        app.Unloaded = delegate(WorkflowApplicationEventArgs e)
        {
            syncEvent.Set();
        };

        app.Completed = delegate(WorkflowApplicationCompletedEventArgs e)
        {
            Console.WriteLine("Workflow {0} Completed.", e.InstanceId);
        };

        id = app.Id;
        app.Run();
        syncEvent.WaitOne();

        string text = Console.ReadLine();
        app = new WorkflowApplication(new Sequence1());
        app.InstanceStore = store;

        app.Completed = (workflowApplicationCompletedEventArgs) =>
        {
            Console.WriteLine("WF Bookmark1 has Completed in the {0} state.",
                              workflowApplicationCompletedEventArgs.CompletionState);
        };
        app.Unloaded = (workflowApplicationEventArgs) =>
        {
            Console.WriteLine("WF Bookmark1 unloaded");
            syncEvent.Set();
        };

        app.Load(id);
        app.ResumeBookmark("readText", text);
        syncEvent.WaitOne();

        // resume bookmark 2
        int number = ReadNumberFromConsole();
        app = new WorkflowApplication(new Sequence1());
        app.InstanceStore = store;
        app.Completed = (workflowApplicationCompletedEventArgs) =>
        {
            Console.WriteLine("WF Bookmark2 has Completed in the {0} state.",
                              workflowApplicationCompletedEventArgs.CompletionState);
        };
        app.Unloaded = (workflowApplicationEventArgs) =>
        {
            Console.WriteLine("WF Bookmark1 unloaded");
            syncEvent.Set();
        };

        app.Load(id);
        app.ResumeBookmark("readNumber", number);
        syncEvent.WaitOne();
        Console.ReadLine();
    }    


} 

问题 工作流运行时要求用户输入名称并创建书签并调用PersistableIdleAction.Unload

我在控制台上输入用户名后,会重新加载工作流程实例并恢复书签。

下次活动不会调用PersistableIdleAction.Unload

请帮助

1 个答案:

答案 0 :(得分:0)

问题此处有multi threading concept

app.Run();

以上行启动新线程上的工作流运行时,因为我使用WorkflowApplication(而不是WorkflowInvoker)调用它。

创建第一个书签时,工作流程保持不变从这个帖子中卸载。

我在恢复上面的书签时创建了新的工作流运行时,因此它有不同的线程。

Solution: I should persist & unload workflow from this thread and not from the 1st first thread.

我正在学习它,所以可能在某个地方出错了,但说实话,这是我在花了很多钱后理解的。

Program.cs的正确版本

namespace Microsoft.Samples.Activities.Statements
{

    class Program
    {
        static AutoResetEvent syncEvent = new AutoResetEvent(false);
        static Guid id;

        static void Main(string[] args)
        {
            // create the workflow app and add handlers for the Idle and Completed actions
            WorkflowApplication app = new WorkflowApplication(new Sequence1());

            //setup persistence
            InstanceStore store = new SqlWorkflowInstanceStore(@"Data Source=.\SQLEXPRESS;Initial Catalog=WF45GettingStartedTutorial;Integrated Security=True");
            InstanceHandle handle = store.CreateInstanceHandle();
            InstanceView view = store.Execute(handle, new CreateWorkflowOwnerCommand(), TimeSpan.FromSeconds(30));
            handle.Free();
            store.DefaultInstanceOwner = view.InstanceOwner;
            app.InstanceStore = store;


            app.PersistableIdle = delegate(WorkflowApplicationIdleEventArgs e)
            {
                syncEvent.Set();
                return PersistableIdleAction.Unload;

            };

            app.Unloaded = delegate(WorkflowApplicationEventArgs e)
            {
                syncEvent.Set();
            };

            app.Completed = delegate(WorkflowApplicationCompletedEventArgs e)
            {
                Console.WriteLine("Workflow {0} Completed.", e.InstanceId);
                syncEvent.Set();
            };

            // start the application
            id = app.Id;
            app.Run();
            syncEvent.WaitOne();

            // resume bookmark 1
            string text = Console.ReadLine();
            app = new WorkflowApplication(new Sequence1());
            app.InstanceStore = store;

            app.PersistableIdle = delegate(WorkflowApplicationIdleEventArgs e)
            {
                syncEvent.Set();
                return PersistableIdleAction.Unload;

            };

            app.Completed = (workflowApplicationCompletedEventArgs) =>
            {
                Console.WriteLine("WF Bookmark1 has Completed in the {0} state.",
                                  workflowApplicationCompletedEventArgs.CompletionState);
                syncEvent.Set();
            };
            app.Unloaded = (workflowApplicationEventArgs) =>
            {
                Console.WriteLine("WF Bookmark1 unloaded");
                syncEvent.Set();
            };

            app.Load(id);
            app.ResumeBookmark("readText", text);
            syncEvent.WaitOne();

            // resume bookmark 2
            int number = ReadNumberFromConsole();
            app = new WorkflowApplication(new Sequence1());
            app.InstanceStore = store;
            app.Completed = (workflowApplicationCompletedEventArgs) =>
            {
                Console.WriteLine("WF Bookmark2 has Completed in the {0} state.",
                                  workflowApplicationCompletedEventArgs.CompletionState);
                syncEvent.Set();
            };
            app.Unloaded = (workflowApplicationEventArgs) =>
            {
                Console.WriteLine("WF Bookmark1 unloaded");
                syncEvent.Set();
            };

            app.Load(id);
            app.ResumeBookmark("readNumber", number);
            syncEvent.WaitOne();

            Console.WriteLine("");
            Console.WriteLine("Press [ENTER] to exit...");
            Console.ReadLine();
        }           

    } 
}

请在此处纠正我的观点。 感谢