通过AEC解读WF的核心原理(二)通过AEC调用添加到引擎中的服务对像

 

说明

TrackingService,PersistenceService,ExternalDataExchange等服务都是通过引擎的AddService方法添加的

 

可以在Activity中用ActivityExecutionContext的GetService方法得到WorkflowRuntime的AddService方法加载的外部对象

 

本例是一个将自定义对象通过引擎的AddService方法添加,自定义Activity在内部使用演示

 

例子下载:https://files.cnblogs.com/wxwinter/aec2.rar

例子

自定义功能类

public class wxdTest

{

public string Text

{ set; get; }

 

public void myTest(string v)

{

System.Console.WriteLine("一个外部实现:" + v);

}

}

 

自定义Activity

public class Activity1 : Activity

{

public string Text

{ set; get; }

 

protected override ActivityExecutionStatus Execute(ActivityExecutionContext executionContext)

{

wxdTest obj = executionContext.GetService<wxdTest>();

obj.myTest(Text);

this.Text = obj.Text;

return base.Execute(executionContext);

}

}

 

测试用工作流

public class Workflow1 : SequentialWorkflowActivity

{

private CodeActivity codeActivity1;

private Activity1 activity11;

 

[System.Diagnostics.DebuggerNonUserCode]

private void InitializeComponent()

{

this.CanModifyActivities = true;

this.codeActivity1 = new System.Workflow.Activities.CodeActivity();

this.activity11 = new wxwinterAecTest.Activity1();

//

// codeActivity1

//

this.codeActivity1.Name = "codeActivity1";

this.codeActivity1.ExecuteCode += new System.EventHandler(this.codeActivity1_ExecuteCode);

//

// activity11

//

this.activity11.Name = "activity11";

this.activity11.Text = "wxd";

//

// Workflow1

//

this.Activities.Add(this.activity11);

this.Activities.Add(this.codeActivity1);

this.Name = "Workflow1";

this.CanModifyActivities = false;

}

 

public Workflow1()

{

InitializeComponent();

}

 

private void codeActivity1_ExecuteCode(object sender, EventArgs e)

{

System.Console.WriteLine("内部打印:" + activity11.Text);

}

}

 

宿主

class Program

{

static void Main()

{

WorkflowRuntime workflowRuntime = new WorkflowRuntime();

wxdTest obj = new wxdTest();

obj.Text = "lzm";

 

workflowRuntime.AddService(obj);

 

workflowRuntime.WorkflowCompleted +=new EventHandler<WorkflowCompletedEventArgs>(workflowRuntime_WorkflowCompleted);

workflowRuntime.WorkflowTerminated +=new EventHandler<WorkflowTerminatedEventArgs>(workflowRuntime_WorkflowTerminated);

workflowRuntime.WorkflowIdled+=new EventHandler<WorkflowEventArgs>(workflowRuntime_WorkflowIdled);

WorkflowInstance instance = workflowRuntime.CreateWorkflow(typeof(Workflow1));

instance.Start();

 

System.Console.Read();

}

 

static void workflowRuntime_WorkflowIdled(object sender, WorkflowEventArgs e)

{

System.Console.WriteLine("WorkflowIdled");

}

 

static void workflowRuntime_WorkflowTerminated(object sender, WorkflowTerminatedEventArgs e)

{

System.Console.WriteLine("Terminated" + e.Exception.Message);

}

 

static void workflowRuntime_WorkflowCompleted(object sender, WorkflowCompletedEventArgs e)

{

System.Console.WriteLine("WorkflowCompleted");

}

 

}

 

运行结果

 

 

总结:

AEC为Activity访问加载到引擎中的服务提供了接口

posted @ 2008-06-05 23:36  WXWinter(冬)  阅读(1994)  评论(2编辑  收藏  举报