简体   繁体   中英

Publishing events from a workflow activity

I'm building a WPF application using prism and I'd like to use workflow foundation (both locally and from a service).

Does anyone have insight on building workflow activities invoked using the EventAggretator and then have workflow publish events in response? I'm considering building an implementation that would allow an activity to publish one to many events, is this a good scenario for workflow?

One way you can implement using an event aggregator is to require it as an extension to your workflow activities like the following. In your WorkflowApplication / WorkflowInvoker , you can then register an instance of the event aggregator so that your activities can raise the events. I haven't used the event aggregator (yet) in my apps, so there might be some quirks.

Custom Activity that requires an event aggregator and uses it in its Execute method:

namespace SampleWorkflowAppOne
{
    using System.Activities;
    using Microsoft.Practices.Prism.Events;

    public class PurchaseOrderInventoryCheckActivity : NativeActivity
    {
        protected override void CacheMetadata(NativeActivityMetadata metadata)
        {
            metadata.RequireExtension<IEventAggregator>();
        }

        protected override void Execute(NativeActivityContext context)
        {
            var eventAggregator = context.GetExtension<IEventAggregator>();
            var somethingHappenedEvent = eventAggregator.GetEvent<MyActivityEvent>();
            var myEventInfo = new MyEventInfo() { SomeNumber = 5 };
            somethingHappenedEvent.Publish(myEventInfo);
        }
    }

    public class MyActivityEvent : CompositePresentationEvent<MyEventInfo>
    {
    }

    public class MyEventInfo
    {
        public int SomeNumber { get; set; }
    }
}

Registering the event aggregator instance for use in your activities:

using System;
using System.Linq;
using System.Activities;
using System.Activities.Statements;

namespace SampleWorkflowAppOne
{
    using Microsoft.Practices.Prism.Events;

    class Program
    {
        static void Main(string[] args)
        {
            var purchaseOrderValidationWorkflow = new PurchaseOrderValidationWorkflow();
            var eventAggregator = new EventAggregator();
            var wfInvoker = new WorkflowInvoker(purchaseOrderValidationWorkflow);
            wfInvoker.Extensions.Add(eventAggregator);
            wfInvoker.Invoke();
        }
    }
}

Hope that helps.

EDIT: I found this video that shows building an event driven +long running workflow that might be of some help as well. I haven't watched it yet though: http://channel9.msdn.com/Events/Build/BUILD2011/TOOL-801T

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM