WorkflowInvoker,WorkflowApplication and WorkflowServiceHost

Workflow 4.0是个全新的框架,重写了3.0/3.5中的的Runtime和Activity。在Workflow 4.0的Runtime和3.0/3.5中的Runtime的最大区别在于没有了WorkflowRuntime对应的类。

在3.0/3.5中,我们需要先初始化WorkflowRuntime,并通过CreateInstance函数来建立工作流实例(WorkflowInstance)。4.0中的WorkflowApplication类和WorkflowInstance类对应,但是不需要通过WorkflowRuntime来实例化,可以通过new来实例化。

WorkflowInvoker,WorkflowApplication and WorkflowServiceHost 

Creating an instance of the WorkflowRuntime class and calling StartRuntime is all we need to spin up the workflow execution environment. WorkflowRuntime defines methods that allow customization of the execution environment. The class also defines events we can listen for during execution. The runtime will fire an event when workflows finish execution, abort, turn idle, and more.

Once we've created an instance of the runtime, we can create workflows with the CreateWorkflow method. The CreateWorkflow method returns an object of type WorkflowInstance. The WorkflowInstance class represents an individual workflow. The Start method on the workflow instance object will begin the execution of a workflow. If an exception occurs, the workflow will invoke the Terminate method (which leads to the runtime raising a WorkflowTerminated event). A typical sequence of calls is shown in the screenshot next. 

here comes a code example below which based on the .net 3.5:

class Program
{

static AutoResetEvent waitHandle = new AutoResetEvent(false); 
 

static void Main(string[] args)
{
WorkflowRuntime workflowRuntime = new WorkflowRuntime();
workflowRuntime.WorkflowCompleted +=
new EventHandler<WorkflowCompletedEventArgs>
(workflowRuntime_WorkflowCompleted);
workflowRuntime.WorkflowTerminated +=
new EventHandler<WorkflowTerminatedEventArgs>
(workflowRuntime_WorkflowTerminated);
WorkflowInstance instance= workflowRuntime.CreateWorkflow(typeof(Workflow1));
instance.Start();
waitHandle.WaitOne();
}
static void workflowRuntime_WorkflowTerminated(object sender,WorkflowTerminatedEventArgs e)
{
Console.WriteLine(e.Exception.Message);
waitHandle.Set();
}
static void workflowRuntime_WorkflowCompleted(object sender,WorkflowCompletedEventArgs e)
{
waitHandle.Set();
}

 

虽然WorkflowApplication和WorkflowInstance的对应,但是他们的构造方法也存在区别。3.0/3.5中我们通过传入一个System.Type类,由WorkflowRuntime来负责进行模板的实例化。而在4.0中我们是传入一个System.Activities.Activity实例来建立工作流实例。

the code example of how to use WorkflowApplication class to hosting a wf in .net 4.0 

Activity wf = new Sequence
{
    Activities =
     {
         new WriteLine
         {
             Text = "Starting the workflow."
         },
         new Delay
         {
             Duration = TimeSpan.FromSeconds(5)
         },
         new WriteLine
         {
             Text = "Ending the workflow."
         }
     }
};

// Create a WorkflowApplication instance.

WorkflowApplication wfApp = new WorkflowApplication(wf);

//...

// Run the workflow.  

wfApp.Run();

 

除此之外,4.0中还提供提供了一种调用Workflow的快速通道WorkflowInvoker。可以直接通过调用静态方法Invoke来执行一个工作流。

Workflow 4.0另一个重大的特点在于WCF和Workflow的密切结合。和3.5一样,4.0中存在一个用于管理Workflow服务的类System.ServiceModel.Activities.WorkflowServiceHost。需要注意的是这个类存在于System.ServiceModel.Activities.dll中。他和System.WorkflowService.dll (.NET Framework 3.5)中的WorkflowServiceHost是完全不同的。

由此可见4.0中提供了3中不同的方式来管理/托管工作流实例。

WorkflowInvoker

可以像执行一个函数一样运行一个工作流。

优点:简单

缺点:在工作流执行过程中,如果和工作流实例进行数据交换

代码:

            WorkflowInvoker.Invoke(new Workflow1());

WorkflowApplication

可以控制工作流实例长时间运行,可以在运行过程中和实例进行数据交换。但是只能执行一个的工作流实例。

优点:

支持持久化,可以对实例进行控制,可以通过Bookmark来控制实例或交换数据。

应用例子:

使用工作流辅助建立一个向导的UI程序。

代码:

            WorkflowApplication instance = new WorkflowApplication(new Workflow1());

            instance.Completed = delegate(WorkflowApplicationCompletedEventArgs e)

            {

                Console.WriteLine("workflow completed, Id = " + instance.Id);

            };

            instance.Run();

            //....

            instance.ResumeBookmark("Submit", data);

WorkflowServiceHost

WorkflowServiceHost是一个最主要的工作流主机类。可以同时管理多个工作流实例,同时控制实例的激活等操作。支持WCF,和3.5相比有更强大的消息关联功能。当然WorkflowServiceHost也支持持久化(Persistence)和跟踪(Tracking)等功能。

以我的理解,我认为,大多数情况下, WorkflowServiceHost管理的工作流都附带一个endpoint, 并用象Receive这样的活动接收消息。这是 WorkflowServiceHost常用的场景。

By default, WorkflowInvoker is used to invoke the WF. I changed this into WorkflowServiceHost, the reason is that WorkflowInvoker cannot be configured with PersistenceWorkflowServiceHost is used to host WF Services. The other method of calling (hosting) workflows is WorkflowApplication which - likeWorkflowServiceHost - can be used to host long running asynch workflows with extensions (Persistence, Tracking); however, only for workflows that are non services. 

so , when your bussiness need to expose the WorkFlow to a WCF service . you should use the  WorkflowServiceHost  complete your task.

// Create a service to handle incoming requests
WorkflowService service = new WorkflowService
{
Name = "LibraryReservation",
Body = new ProcessRequest(),
Endpoints =
{
new Endpoint
{
ServiceContractName="ILibraryReservation",
AddressUri = new Uri("http://localhost:" + adr +"/LibraryReservation"),
Binding = new BasicHttpBinding(),
}
}
};
// Create a WorkflowServiceHost that listens for incoming messages
System.ServiceModel.Activities.WorkflowServiceHost wsh =
new System.ServiceModel.Activities.WorkflowServiceHost(service);

wsh.Open(); 

 

 

你可能感兴趣的:(application)