using (WorkflowRuntime workflowRuntime = new WorkflowRuntime())
{
AutoResetEvent waitHandle = new AutoResetEvent(false);
workflowRuntime.WorkflowCompleted += delegate(object sender, WorkflowCompletedEventArgs e) { Console.WriteLine("WorkFlow Completed!"); };
workflowRuntime.WorkflowTerminated += delegate(object sender, WorkflowTerminatedEventArgs e)
{
Console.WriteLine(e.Exception.Message);
//waitHandle.Set();
};
//为工作流添加参数
Dictionary<string, object> parameters = new Dictionary<string, object>();
parameters.Add("Alias", "lxf");
//为工作流添加数据交换服务
ExternalDataExchangeService dataService = new ExternalDataExchangeService();
workflowRuntime.AddService(dataService);
//实例化服务内容
VotingServiceImpl votingservice = new VotingServiceImpl();
dataService.AddService(votingservice);
workflowRuntime.StartRuntime();
//调用工作流
WorkflowInstance instance = workflowRuntime.CreateWorkflow(typeof(WorkflowConsoleApplication1.Workflow1),parameters);
instance.Start();
waitHandle.WaitOne();
}
Console.ReadLine();
}
2、定义工作流服务接口
[ExternalDataExchange]
internal interface IVotingServices
{
event EventHandler<VotingServiceEventArgs> ApprovedProposal;
event EventHandler<VotingServiceEventArgs> RejectProposal;
void CreateBallot(string alias);
}
3、定义工作流服务事件参数
[Serializable]
internal class VotingServiceEventArgs : ExternalDataEventArgs
{
private string aliasvalue;
public VotingServiceEventArgs(Guid instanceID, string alias)
: base(instanceID)
{
this.aliasvalue = alias;
}
public string Alias
{
get
{
return this.aliasvalue;
}
}
}
4、定义工作流服务实现类
internal class VotingServiceImpl : IVotingServices
{
public event EventHandler<VotingServiceEventArgs> ApprovedProposal;
public event EventHandler<VotingServiceEventArgs> RejectProposal;
public void CreateBallot(string alias)
{
Console.WriteLine("Bollot created for {0}.", alias);
ShowVotingDialog(new VotingServiceEventArgs(WorkflowEnvironment.WorkflowInstanceId,alias));
}
public void ShowVotingDialog(VotingServiceEventArgs votingEventArgs)
{
DialogResult result;
string alias = votingEventArgs.Alias;
result = MessageBox.Show(string.Format("Approved Proposal,{0}?",alias),string.Format("{0} Bollot",alias),MessageBoxButtons.YesNo);
if (result == DialogResult.Yes)
{
EventHandler<VotingServiceEventArgs> approvedProposal = this.ApprovedProposal;
if (approvedProposal != null)
approvedProposal(null, votingEventArgs);
}
else
{
EventHandler<VotingServiceEventArgs> rejectProposal = this.RejectProposal;
if (rejectProposal != null)
rejectProposal(null, votingEventArgs);
}
}
}
5、开发工作流
1)添加callExternalMethodActivity,为其设置接口 IVotingServices,事件CreateBallot
2)添加listenActivity
3)为listenActivity添加handleExternalEventActivity1,handleExternalEventActivity2,分别定义
接口 IVotingServices,事件为ApprovedProposal,RejectProposal
至此,简单的例子已经完成。