0

嗨,我是 Windows 工作流的新手。这可能很容易,但我一直坚持这一点。我有一个状态机工作流,其中我有一个工作流主机类。持久性在此代码中不起作用。虽然调试指针永远不会转到 application.persistableIdle 事件。

我使用自定义输入参数,我已将其设置为可序列化。

以下是我的主机类代码:

 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 事件,它将进入该事件代码。

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

谢谢。

4

1 回答 1

-1

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

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

StateMachineStateTracker.Promote(this.instanceStore);
于 2014-05-07T10:16:26.263 回答