[ExternalDataExchange]
internal interface ILocalService {
event EventHandler InvokedWorkflowComplete;
void WorkComplete(Guid HostWFGuid);
}
internal class LocalService : ILocalService {
public event EventHandler InvokedWorkflowComplete;
public void WorkComplete(Guid HostWFGuid) {
if (InvokedWorkflowComplete != null) {
InvokedWorkflowComplete(null, new ExternalDataEventArgs(HostWFGuid));
}
}
}
using System;
using System.Workflow.Runtime;
using System.Workflow.Runtime.Hosting;
using System.Workflow.Activities; // ExternalDataExchangeService
using System.Threading;
namespace InvokeWorkflowActivityDemo {
class Program {
static Guid HostWFGuid;
static LocalService ls;
static void Main(string[] args) {
using (WorkflowRuntime workflowRuntime = new WorkflowRuntime()) {
// 必須套路:載入本地資料交換服務
ExternalDataExchangeService dataService = new ExternalDataExchangeService();
workflowRuntime.AddService(dataService);
// 註冊自訂本地服務
ls = new LocalService();
dataService.AddService(ls);
AutoResetEvent waitHandle = new AutoResetEvent(false);
workflowRuntime.WorkflowCompleted += delegate(object sender, WorkflowCompletedEventArgs e) {
if (e.WorkflowInstance.InstanceId != HostWFGuid) {
// 子工作流完成 → 通知父工作流
ls.WorkComplete(HostWFGuid);
} else {
// 父工作流完成 → 釋放等待
waitHandle.Set();
}
};
workflowRuntime.WorkflowTerminated += delegate(object sender, WorkflowTerminatedEventArgs e) {
Console.WriteLine(e.Exception.Message);
waitHandle.Set();
};
WorkflowInstance instance = workflowRuntime.CreateWorkflow(typeof(Workflow1));
HostWFGuid = instance.InstanceId;
instance.Start();
waitHandle.WaitOne();
Console.ReadLine();
}
}
}
}
關鍵點:啟動工作流前必須加載 ExternalDataExchangeService 並註冊自訂服務 (MSDN 規定)。