Tuesday, June 19, 2007

EventDeliveryFailedException in State Machine Workflow

I am currently diving into Windows Workflow Foundation. During my first steps (kind of hello world examples), some errors occurs ... Sometimes it's hard to resolve some errors...
Let's have a look at the EventDeliveryFailedException ...

Has this error ever occured?
An unhandled exception of type 'System.Workflow.Activities.EventDeliveryFailedException' occurred in System.Workflow.Activities.dll.
Additional information: Event "AskWorkflowValues" on interface type "App.GeneralLib.IMyService" for instance id "8385d402-19fb-4549-a765-c7e2b4c0a856" cannot be delivered.

When adding an eventDrivenActivity to a State Machine's worfklow State Activity, you wouldn't receive this exception when firing an event from the host application to the workflow.

This exception only occurs, in my case, when adding an eventDrivenActivity to the State Machine's workflow self... (and not to a state activity).


Solution

The host application fires an event through a local service method. In that method you must specify the ExternalDataEventArgs parameter. Now to resolve the problem you must set the WaitForIdle property of the ExternalDataEventArgs instance.

If this property is true then the workflow should go idle before raising the event by the host application through the local service. At default, this property is false and the event can only be catched by an EventDrivenActivity within a State Activity. So, in other words the workflow has to process it things first before this event can be deliverd. That's why the WaitForIdle must be true.


Local Service Interface

[ExternalDataExchange]
public interface IMyService
{
event EventHandler AskWorkflowValues;
}


Local Service implementation

[Serializable]
public class MyService:IMyService
{
public void OnAskWorkflowValues(ExternalDataEventArgs e)
{
if (AskWorkflowValues != null)
{
AskWorkflowValues(null, e);
}
}



Host application

...
//calling local service's method to fire an event to the state machine workflow

localService.OnAskWorkflowValues(GetEventArgs(instance.InstanceId));
...

ExternalDataEventArgs GetEventArgs(Guid instanceId)
{
ExternalDataEventArgs e = new ExternalDataEventArgs(instanceId);
e.WaitForIdle = true;
return e;
}

1 comment:

Kerda said...

Good for people to know.