外部事件响应

事件

工作流也可以在继续之前等待外部事件。在下面的示例中,工作流将等待一个键为0的名为“MyEvent”的事件。一旦外部源触发了此事件,工作流将唤醒并继续处理,将事件生成的数据传递到下一个步骤。

public class EventSampleWorkflow : IWorkflow<MyDataClass>
{
    public void Build(IWorkflowBuilder<MyDataClass> builder)
    {
        builder
            .StartWith(context => ExecutionResult.Next())
            .WaitFor("MyEvent", data => "0")
                .Output(data => data.Value, step => step.EventData)
            .Then<CustomMessage>()
                .Input(step => step.Message, data => "The data from the event is " + data.Value);
    }
}
...
//External events are published via the host
//All workflows that have subscribed to MyEvent 0, will be passed "hello"
host.PublishEvent("MyEvent", "0", "hello");

生效日期

您还可以在等待事件时指定一个有效日期,这允许您响应过去可能已经发生的事件,或者只响应在有效日期之后发生的事件。