Windows work flow persistance iscomplete is not to set to 1, is always 0

46 views Asked by At

I have created a sample numberguess work flow using this article, in .net 4.6.1 and enabled persistence.

When the sample is run, I can see a work flow instance is created in the below table

 [System.Activities.DurableInstancing].[InstancesTable] 

After the workflow is executed (number is guessed correctly) if I go to database and observe the table it is still shown as

ExecutionStatus : Executing & IsCompleted : 0 expecting these values would be Completed & 1

The sample is run in console app and the Program.cs code is as below, please help me how to configure it correct way to update the status accordingly

class Program
    {
        static InstanceStore instanceStore ;
        static void Main(string[] args)
        {

            SetupInstanceStore();
            var inputs = new Dictionary<string, object>() { { "MaxNumber", 100 } };
            AutoResetEvent syncEvent = new AutoResetEvent(false);
            AutoResetEvent idleEvent = new AutoResetEvent(false);            
            WorkflowApplication wfApp = new WorkflowApplication(new StateMachineNumberGuessWorkflow(), inputs);                   
            wfApp.InstanceStore = instanceStore;


            wfApp.PersistableIdle = delegate (WorkflowApplicationIdleEventArgs e)
            {
                return PersistableIdleAction.Persist;
            };
            wfApp.Completed = delegate (WorkflowApplicationCompletedEventArgs e)
            {
                int Turns = Convert.ToInt32(e.Outputs["Turns"]);
                Console.WriteLine(wfApp.Id);
                Console.WriteLine(e.CompletionState);
                Console.WriteLine("Congratulations, you guessed the number in {0} turns.", Turns);
                Console.ReadKey();
                syncEvent.Set();
            };

            wfApp.Aborted = delegate (WorkflowApplicationAbortedEventArgs e)
            {
                Console.WriteLine(e.Reason);
                syncEvent.Set();
            };

            wfApp.OnUnhandledException = delegate (WorkflowApplicationUnhandledExceptionEventArgs e)
            {
                Console.WriteLine(e.UnhandledException.ToString());
                return UnhandledExceptionAction.Terminate;
            };

            wfApp.Idle = delegate (WorkflowApplicationIdleEventArgs e)
            {
                idleEvent.Set();
            };

            wfApp.Run();

            //syncEvent.WaitOne();
            // Loop until the workflow completes.
            WaitHandle[] handles = new WaitHandle[] { syncEvent, idleEvent };
            while (WaitHandle.WaitAny(handles) != 0)
            {
                //Console.WriteLine("in while loop");
                // Gather the user input and resume the bookmark.
                bool validEntry = false;
                while (!validEntry)
                {
                    int Guess;
                    if (!Int32.TryParse(Console.ReadLine(), out Guess))
                    {
                        Console.WriteLine("Please enter an integer ---");
                    }
                    else
                    {
                        validEntry = true;
                        wfApp.ResumeBookmark("EnterGuess", Guess);
                    }
                }
            }
        }
        private static void SetupInstanceStore()
        {
            instanceStore =
               new SqlWorkflowInstanceStore(@"Data Source=localhost;Initial Catalog=WorkFlowInstanceDB;Integrated Security=True;Asynchronous Processing=True");

            InstanceHandle handle = instanceStore.CreateInstanceHandle();

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

            instanceStore.DefaultInstanceOwner = view.InstanceOwner;


        }
    }
1

There are 1 answers

0
Mahesh Gupta On

after debugging for some time, I found that console app is exiting before workflow persists, if I stop the console to exit, it gets persisted and due to the behavior of sqlserver persistance library it deletes the instance record on successful completion of execution.