工作流程应用.PersistableIdle事件未触发

时间:2013-09-25 16:52:23

标签: workflow-foundation-4

您好我是Windows Workflow的新手。这可能很容易,但我长期坚持这一点。 我有一个状态机工作流,我有一个工作流主机类。 持久性在此代码中不起作用。调试指针永远不会进入application.persistableIdle事件。

我使用自定义输入参数,我将其设置为Serializable。

下面是我的主机类代码:

 static InstanceStore instanceStore;
    static AutoResetEvent instanceUnloaded = new AutoResetEvent(false);
    static Activity activity = new Activity1();
    static Guid id = new Guid();
    static int intContractHeaderKey;
   static  Contract contract = new Contract();

 public ContractActivityHost(Guid wfid, Int32 contractHeaderID)
    {
        SetupInstanceStore();

        StartAndUnloadInstance(contractHeaderID);
        if (intContractHeaderKey > 0)
        {
            LoadAndCompleteInstance(id, intContractHeaderKey);
        }
    }

    static void StartAndUnloadInstance(Int32 contractHeaderID)
    {

        contract = new Contract();
        //var objContract = new object();

         var input = new Dictionary<string, object>
                            {
                                {"TheContract", contract}                                
                            };
         input.Add("ContractHeaderKey", contractHeaderID);


         WorkflowApplication application = new WorkflowApplication(activity, input);


        application.InstanceStore = instanceStore;


        //returning IdleAction.Unload instructs the WorkflowApplication to persists application state and remove it from memory  
        application.PersistableIdle = (e) =>
        {
            return PersistableIdleAction.Unload;
        };

        application.Unloaded = (e) =>
        {
            instanceUnloaded.Set();
        };

        //application.Idle = (e) =>
        //    {
        //        //application.Unload();
        //        instanceUnloaded.Set();

        //    };


        //This call is not required 
        //Calling persist here captures the application durably before it has been started
        application.Persist();
        id = application.Id;

        application.Run();

        instanceUnloaded.WaitOne();

        //application.Unload();
        //contract = (Contract)objContract;

        intContractHeaderKey = contract.ContractID;
    }

    static void LoadAndCompleteInstance(Guid wfid, Int32 contractHeaderID)
    {
        //string input = Console.ReadLine();
        while (!contract.ContractWFPause)
        {
            contract.FireContract(contract.ContractID);

            WorkflowApplication application = new WorkflowApplication(activity);
            application.InstanceStore = instanceStore;

            application.Completed = (workflowApplicationCompletedEventArgs) =>
            {
                //Console.WriteLine("\nWorkflowApplication has Completed in the {0} state.", workflowApplicationCompletedEventArgs.CompletionState);
                strWFStatus = "Completed";
            };

            application.Unloaded = (workflowApplicationEventArgs) =>
            {
                //Console.WriteLine("WorkflowApplication has Unloaded\n");
                strWFStatus = "Unloaded";
                instanceUnloaded.Set();
            };

            application.Load(wfid);


            instanceUnloaded.WaitOne();
        }
    }

    private static void SetupInstanceStore()
    {
        instanceStore =
            new SqlWorkflowInstanceStore(@"Data Source=.;Initial Catalog=WorkflowInstanceStore;Integrated Security=True;");

        InstanceHandle handle = instanceStore.CreateInstanceHandle();
        InstanceView view = instanceStore.Execute(handle, new CreateWorkflowOwnerCommand(), TimeSpan.FromSeconds(10));
        handle.Free();

        instanceStore.DefaultInstanceOwner = view.InstanceOwner;
    }

我一直试图解决这个问题很长时间,但不知道我错过了什么。我已经完成了几个示例应用程序并更改了我的代码以匹配流和逻辑,但它仍然无效。 在application.persist之后,将记录插入[System.Activities.DurableInstancing]。[InstancesTable]视图。 但是调试指针不会超出instanceUnloaded.WaitOne(); 它实际上进入了空闲状态。如果我取消注释application.idle事件,它会进入该事件代码。

任何解决此问题的帮助都会很棒。

感谢。

1 个答案:

答案 0 :(得分:-1)

请检查您是否添加了以下详细信息

instanceStore = new SqlWorkflowInstanceStore(ConfigurationManager.ConnectionStrings["WFPersistenceDb"].ConnectionString);

StateMachineStateTracker.Promote(this.instanceStore);