Jun 1, 2008

EventHandling in WF

In WF, CallExternalMethod is used to transfer data from workflow instance to host, and HandleExternalEvent is used to transfer data from host to workflow instance. The pattern of HandleExternalEvent is similar to the one of CallExternalMethod. Basically, you specify the interface the HandleExternalEvent can support, then you need to specify the event that HandleExternalEvent can handle. The event is from the interface supported. To use HandleExternalEvent in our workflow, we have more supporting activities need to be introduced into the workflow. Firstly, you need a listener activity to support event listening. In the listening activity, you define one or more EventDrivenActivity. In side of EventDrivenActivity, the first child activity should be HandleExternalEvent, which is define what event it is listening. To achieve the purpose of transferring information from external host to workflow instance, we can embed the information in the event argument. At the workflow client side, we need to add the external service to workflow host. The external service inplement the interface that the HandleExternalEvent want to handle. Then client call external service's fire event method, magically, the workflow instance will intercept the event.

ExternalDataExchangeService exSvc = new ExternalDataExchangeService(); workflowRuntime.AddService(exSvc); CustomerService custSvc = new CustomerService(); exSvc.AddService(custSvc); public void SendCustomerInfo(Guid id) { if (this.GotCustomerInfo != null) { CustomerEventArgs args = new CustomerEventArgs(id); args.CustomerName = "fred"; GotCustomerInfo(null, args); } }