通過AEC解讀WF的核心原理(二)通過AEC調用添加到引擎中的服務對像

來源:互聯網
上載者:User

 

說明

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

 

可以在Activity中用ActivityExecutionContext的GetService方法得到WorkflowRuntime的AddService方法載入的外部對象

 

本例是一個將自訂對象通過引擎的AddService方法添加,自訂Activity在內部使用示範

 

例子下載:http://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訪問載入到引擎中的服務提供了介面

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.