外部触发的事件和工作流在出发之后使用调用的函数是两码事。可以这样认为所谓的接口只是提供了一种的对应关系。
1)首先是接口提供了能干什么?
2)接口的实现类来直接调调用接口中公开的方法,其实这里调用的就是在工作流中invoke中的方法。
所以将所谓的接口只是提供了一种的对应的方法。
例如下面的代码:
Test.cs
namespace HandleExternalEventActivityTest { // Args [Serializable] public class ArgsTest : ExternalDataEventArgs { public ArgsTest(Guid id) : base(id) { } } [Serializable] public class TestImpl : ITest { public event EventHandler<ArgsTest> Agree; public void OnAgree (ArgsTest arg) { if (Agree != null) { Agree(null, arg); } } } }
ITest.cs
namespace HandleExternalEventActivityTest { [ExternalDataExchange] public interface ITest { event EventHandler<ArgsTest> Agree; } }
Program.cs
namespace HandleExternalEventActivityTest { class Program { static void Main(string[] args) { using(WorkflowRuntime workflowRuntime = new WorkflowRuntime()) { ExternalDataExchangeService dataExchangeService = new ExternalDataExchangeService(); workflowRuntime.AddService(dataExchangeService); TestImpl service = new TestImpl(); dataExchangeService.AddService(service); AutoResetEvent waitHandle = new AutoResetEvent(false); workflowRuntime.WorkflowCompleted += delegate(object sender, WorkflowCompletedEventArgs e) {waitHandle.Set();}; workflowRuntime.WorkflowTerminated += delegate(object sender, WorkflowTerminatedEventArgs e) { Console.WriteLine(e.Exception.Message); waitHandle.Set(); }; WorkflowInstance instance = workflowRuntime.CreateWorkflow(typeof(HandleExternalEventActivityTest.Workflow1)); instance.Start(); ////////////////////////////////////////////////////////////////////////// // 这里时测试 Console.WriteLine("Press any key !"); Console.Read(); service.OnAgree(new ArgsTest(instance.InstanceId)); waitHandle.WaitOne(); } } } }
按照上面的思路其实就是定义了一种对应关系(其实这也是c#中delegate和event关键字的作用)如下:
Agree(null, arg);
实际上就是调用的是:
// workflow1.cs private void OnAgree(object sender, ExternalDataEventArgs e) { Console.WriteLine("Agree !"); }
这里只是为了理解使用‘对应’讲话理解,实际应该参加:
http://msdn.microsoft.com/en-us/library/8627sbea(v=VS.80).aspx