我使用本文在 .net 4.6.1 中创建了一个示例 numberguess 工作流程,并启用了持久性。
运行示例时,我可以看到在下表中创建了一个工作流实例
[System.Activities.DurableInstancing].[InstancesTable]
执行工作流后(数字猜对了),如果我去数据库并观察表格,它仍然显示为
ExecutionStatus : Executing & IsCompleted : 0
期望这些值将是 Completed & 1
该示例在控制台应用程序中运行,Program.cs 代码如下,请帮助我如何正确配置它以相应地更新状态
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;
}
}