外部事件触发和处理:
1. 创建一个HandleExternalEventActivity
View Code
1 public class HandleEventActivity:HandleExternalEventActivity 2 { 3 public HandleEventActivity() 4 : base() 5 { 6 this.EventName = "HandleEvent"; 7 this.InterfaceType = typeof(ITriggerEventService); 8 this.Name = "HandleEventActivity"; 9 } 10 11 }
2. 创建一些类:
View Code
1 [ExternalDataExchange] 2 public interface ITriggerEventService 3 { 4 void triggerEvent(Guid instanceId); 5 6 event EventHandlerHandleEvent; 7 } 8 9 [Serializable] 10 public class TriggerEventArgs : ExternalDataEventArgs 11 { 12 public TriggerEventArgs(Guid instanceId) 13 : base(instanceId) 14 { } 15 } 16 17 [Serializable] 18 public class TriggerEventService : ITriggerEventService 19 { 20 #region ITriggerEventService Member 21 22 public void triggerEvent(Guid instanceId) 23 { 24 TriggerEventArgs teArgs = new TriggerEventArgs(instanceId); 25 EventHandler evt = this.HandleEvent; 26 if (evt != null) 27 { 28 evt(this, teArgs); 29 } 30 } 31 32 public event EventHandler HandleEvent; 33 34 #endregion 35 }
3. 创建TriggerEventActivity:
创建一个custom activity. 里面放一个名叫callExternalMethodActivity的CallExternalMethodActivity。
View Code
1 [Designer(typeof(ActivityDesigner))] 2 public partial class TriggerEventActivity : SequenceActivity 3 { 4 public TriggerEventActivity() 5 { 6 InitializeComponent(); 7 } 8 9 protected override ActivityExecutionStatus Execute(ActivityExecutionContext executionContext) 10 { 11 ExternalDataExchangeService edeService = executionContext.GetService(typeof(ExternalDataExchangeService)) as ExternalDataExchangeService; 12 if (edeService.GetService(typeof(TriggerEventService)) == null) 13 { 14 TriggerEventService teService = new TriggerEventService(); 15 edeService.AddService(teService); 16 } 17 this.callExternalMethodActivity.ParameterBindings.GetItem("instanceId").Value = this.WorkflowInstanceId; 18 return base.Execute(executionContext); 19 } 20 21 public static DependencyProperty MethodInvokingEvent = DependencyProperty.Register("MethodInvoking", typeof(System.EventHandler), typeof(TriggerEventActivity)); 22 23 [DesignerSerializationVisibilityAttribute(DesignerSerializationVisibility.Visible)] 24 [BrowsableAttribute(true)] 25 [CategoryAttribute("Execute Method")] 26 public event EventHandler MethodInvoking 27 { 28 add 29 { 30 base.AddHandler(MethodInvokingEvent, value); 31 } 32 remove 33 { 34 base.RemoveHandler(MethodInvokingEvent, value); 35 } 36 } 37 }