我在.net 4.6.1中使用此article创建了一个简单的数字猜测工作流程,并启用了持久性。
运行示例时,我可以看到在下表中创建了工作流程实例
[System.Activities.DurableInstancing].[InstancesTable]
如果执行工作流(正确猜测数字后,如果我进入数据库并观察该表,它仍显示为]
ExecutionStatus : Executing & IsCompleted : 0
期望这些值将完成&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;
}
}
经过一段时间调试后,我发现控制台应用程序在工作流持续存在之前就已退出,如果我停止控制台退出,它将继续存在,并且由于sqlserver持久性库的行为,它会在成功完成执行后删除实例记录。