C# – Windows Workflow Foundation NullReferenceException

cnetworkflowworkflow-foundation

I got a NullReference problem using WWF and externaly raised events. The WWF state machine works together with a service instance raising events in the state machine to provide data and – of course – to change the state. While the "normal" operation works fine using events I got a strange problem.

To handle timeout scenarios I let the state initializer use a external timeout mechanism to register a callback in the menioned service. After the given time the callback function runs and shall raise the timeout event in the state machine. The events are defined like this:

event EventHandler<ExternalDataEventArgs> DeviceSysmapBrdcstTimeoutEvent;

and as mentioned work properly if not called from the timeout machanism. The ExternalDataEventArgs are created using valid Guids. The Excetion is raised accessing the delegate within the event:

System.Workflow.Activities.EventDeliveryFailedException was unhandled by user code
    Message="Event \"DeviceSysmapBrdcstTimeoutEvent\" on interface type \"...\" for instance id \"efa3da3d-8546-4fcf-bc56-bbec04df6d69\" cannot be delivered."
Source="System.Workflow.Activities"
    StackTrace:
        at System.Workflow.Activities.WorkflowMessageEventHandler.EventHandler(Object sender, ExternalDataEventArgs eventArgs)

Anyone an idea? Thanks.

Best Answer

You should check the inner exception for more details. Check if you event service classes are marked [Serializable] and also set the WaitForIdle property to true in the event args.

CustomEventDataArgs eventArgs = new CustomEventDataArgs();
//
eventArgs.WaitForIdle = true;

Check if this fixes the problem.

Thanks.