Take the 2-minute tour ×
SharePoint Stack Exchange is a question and answer site for SharePoint enthusiasts. It's 100% free, no registration required.

I'm writing a workflow that uses pluggable workflow services to await an external event from an external program. My problem is that, no matter how I re-write it, the external event is raised twice, even though I'm calling the RaiseEvent method once. I'm at the end of my wits here - how do I solve this issue?

I've shared my code here.

Here's the structure of my workflow. Both event handler activities are bound to the same method, HandleEvent1_Invoked:

enter image description here

Here's how I'm raising the events:

        // 1: Raising the events: console program
        using (var site = new SPSite(siteGuid))
        {
            using (var web = site.OpenWeb(webGuid))
            {
                SPWorkflowExternalDataExchangeService.RaiseEvent(web, site.SystemAccount.ID, workflowGuid, typeof(IWfExternalEvents), 
                    "RandomEventType", new object[] { "alpha" }, SPWorkflowRunOptions.SynchronousAllowPostpone);

                SPWorkflowExternalDataExchangeService.RaiseEvent(web, site.SystemAccount.ID, workflowGuid, typeof(IWfExternalEvents),
                    "RandomEventType", new object[] { "beta" }, SPWorkflowRunOptions.SynchronousAllowPostpone);
            }
        }

    // 2: External data service
    public override void CallEventHandler(Type eventType, string eventName, object[] eventData, SPWorkflow workflow, 
        string identity, System.Workflow.Runtime.IPendingWork workHandler, object workItem)
    {
        var args = new ExternalEventArgs(workflow.InstanceId)
        {
            EventData = (string)eventData[0]
        };

        if (Event != null)
        {
            Event(null, args);
        }
    }

    // 3: Event handling method: workflow
    private void HandleEvent1_Invoked(object sender, ExternalDataEventArgs e)
    {
        var args = (ExternalEventArgs)e;

        workflowProperties.Workflow.CreateHistoryEvent(1, 1, workflowProperties.OriginatorUser, "Event received", args.EventData, null);
    }

Here's what I'm getting in the workflow history log:

enter image description here

And the trace log.

It looks like it delivers the first event ("alpha") twice, and when it tries to deliver the second one, the workflow is already complete, so it raises the exception.

Any help is appreciated.

share|improve this question
    
How long is your delay? –  Mike Dec 7 '12 at 18:18
    
Zero, right now. I just added it to ensure that the workflow is dehydrated after receiving the first event. But the same thing happens when I increase the delay to a minute or two, or when I remove it altogether. –  Mihai Dec 7 '12 at 19:13
    
what exception are you getting? –  Mike Dec 7 '12 at 22:37
    
It's in the trace log that I've attached. The "alpha" event is delivered twice, and for the "beta" event, I get System.Workflow.Activities.EventDeliveryFailedException: Event "Event" on interface type "EventTest.IWfExternalEvents" for instance id "50ebe158-99ef-4d1e-83d1-85ee9807cc94" cannot be delivered. - also two times. –  Mihai Dec 8 '12 at 21:35
add comment

1 Answer 1

up vote 2 down vote accepted
+25

There is a KB for this.

http://support.microsoft.com/kb/2694420

When the OnWorkflowActivated event fires, SharePoint delivers one event for each OnWorkflowItemChanged activity inside the state machine workflow. For example, if there are 3 OnWorkflowItemChanged activites inside the state machine workflow, then there will be 3 subscriptions for the workflow. So when an item changed event occurs, all 3 subscriptions are triggered.

Your issue isn't exactly identical but it is the same thing, but the "Workaround" applies. Pretty much, you have to manually prevent the workflow from firing again.

share|improve this answer
    
Hmm. I'm not sure how to create a manual subscription for a random external event, as the workaround seems to suggest, but at least it gives me a direction to dig in. –  Mihai Dec 13 '12 at 18:17
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.