Skip to content

Feature Waiting on External Events (Human Interaction)

Yashwanth Anantharaju edited this page Sep 25, 2019 · 3 revisions

Often orchestrations need to wait for external events like a human being entering some input or some other external trigger. The framework provides a mechanism for the orchestration to asynchronously wait for an external event.

public class GetQuoteOrchestration : TaskOrchestration<string, string>
{
    TaskCompletionSource<object> getPermission = new TaskCompletionSource<object>(); 

    public override async Task<string> RunTask(OrchestrationContext context, string input)
    {
        await getPermission.Task;
        await context.ScheduleTask<object>(typeof (GetQuote), null);
        return null;
    }

    public override void OnEvent(OrchestrationContext context, string name, string input)
    {
        getPermission.SetResult(null);
    }
}

To trigger the event from the outside, the user can call the TaskHubClient.RaiseEvent method.

TaskHubClient client = new TaskHubClient("test", serviceBusConnString);
OrchestrationInstance instance = client.CreateOrchestrationInstance(typeof (GetQuoteOrchestration), 
                        "quote")
client.RaiseEvent(instance, "dummyEvent", "dummyData");