WorkflowServiceHost:获取当前状态以及状态机的可能转换

时间:2019-05-28 14:21:25

标签: .net workflow workflow-foundation-4

如何获取StateMachine的当前状态和将来状态?我尝试使用StateMachineStateTracker,但它不能与WorkflowServiceHost一起使用,尽管它可以与WorkflowApplication一起正常工作(但是我无法弄清楚如何在时间到期时使用SQL Persistence运行Delay活动)。 我的代码:

public static System.ServiceModel.Activities.WorkflowServiceHost Host { get; set; }

public static BookmarkEndpoint Endpoint { get; set; }

public static string ConnectionString { get; private set; }

private static SqlWorkflowInstanceStore instanceStore;

static WorkflowService()
{
    Host = new System.ServiceModel.Activities.WorkflowServiceHost(new Activity1(), new Uri("http://localhost:50574"));
    Endpoint = new BookmarkEndpoint(new BasicHttpBinding(), new EndpointAddress("http://localhost:50574/workflow"));
    Host.AddServiceEndpoint(Endpoint);

    ConnectionString = ConfigurationManager.ConnectionStrings["WFStorage"].ConnectionString;

    SqlConnectionStringBuilder builder = new SqlConnectionStringBuilder(ConnectionString);

    if (!SqlWorkflowInstanceStoreManager.InstanceStoreExists(builder.InitialCatalog, ConnectionString))
        SqlWorkflowInstanceStoreManager.CreateInstanceStore(builder.InitialCatalog, ConnectionString);

    instanceStore = new SqlWorkflowInstanceStore(ConnectionString)
    {
        InstanceEncodingOption = InstanceEncodingOption.GZip,
        InstanceCompletionAction = InstanceCompletionAction.DeleteAll,
        InstanceLockedExceptionAction = InstanceLockedExceptionAction.AggressiveRetry,
        HostLockRenewalPeriod = new TimeSpan(0, 0, 30),
        RunnableInstancesDetectionPeriod = new TimeSpan(0, 0, 5)
    };

    //StateMachineStateTracker.Promote(instanceStore);

    var createWorkflowOwnerCommand = new CreateWorkflowOwnerCommand();
    var handle = instanceStore.CreateInstanceHandle();

    try
    {
        instanceStore.BeginExecute(handle, createWorkflowOwnerCommand, TimeSpan.FromSeconds(30), OnInstanceStoreEndExecute, null);
    }
    catch
    {
        throw new FaultException<WorkflowFaultException>(new WorkflowFaultException(""), new FaultReason("An error has occured setting up the InstanceStore"));
    }

    Host.DurableInstancingOptions.InstanceStore = instanceStore;

    var stateTracker = StateMachineStateTracker.Attach(Host);
    //Host.WorkflowExtensions.Add(stateTracker);
    //Host.WorkflowExtensions.Add(new StateTrackerPersistenceProvider(stateTracker));

    Host.Open();
}

0 个答案:

没有答案