在WF状态机的State活动中,都是从事件活动开始,通过callExternalMethod活动调用方法来执行需要执行的业务.
在工作流创建时候,我们可以通过参数的方式将工作流需要的共有数据传递给工作流,这样工作流可以任何时候访问这些数据(通过工作流属性实现了数据的get和set)。
工作流的CreateWorkflow方法:
namedArgumentValues.Add("UserId", userId);
namedArgumentValues.Add("Xml", xml);
namedArgumentValues.Add("OrderId", orderId);
通过传参的方式只能在工作流创建的时候将数据写入,在工作流重新被load后,没有办法给工作流实例(WorkflowInstance)添加参数或者写入数据。但是在实际业务中每个业务操作都可能需要传入一些数据,比如订单审批人,审批金额等,这样就出现了我在开发中遇到的问题,如何给工作流实例传递值?
大概的解决思路在关于wf中传参的问题:如何在workflow实例钝化后重新load时传参! 做了说明。
第一步:创建自定义事件类,比如OrderDataEventArgs,创建自定义事件类的时候需要注意两点:首先要继承ExternalDataEventArgs,另外就是要添加序列化声明
第二步:在状态机中添加OrderDataEventArgs类型的属性:
设置过程图片:
e参数绑定到OrderDataEventArgs属性的代码如下,这是通过属性设置生成的代码:
图片:
代码:
下面是关于工作流接口和实现的代码:
在工作流创建时候,我们可以通过参数的方式将工作流需要的共有数据传递给工作流,这样工作流可以任何时候访问这些数据(通过工作流属性实现了数据的get和set)。
工作流的CreateWorkflow方法:
public WorkflowInstance CreateWorkflow (Type workflowType,Dictionary<string, Object> namedArgumentValues)Dictionary<string, object> namedArgumentValues= new Dictionary<string, object>();=
参数namedArgumentValues就是我们需要的参数,可以这样组装namedArgumentValues参数:
namedArgumentValues.Add("UserId", userId);
namedArgumentValues.Add("Xml", xml);
namedArgumentValues.Add("OrderId", orderId);
通过传参的方式只能在工作流创建的时候将数据写入,在工作流重新被load后,没有办法给工作流实例(WorkflowInstance)添加参数或者写入数据。但是在实际业务中每个业务操作都可能需要传入一些数据,比如订单审批人,审批金额等,这样就出现了我在开发中遇到的问题,如何给工作流实例传递值?
大概的解决思路在关于wf中传参的问题:如何在workflow实例钝化后重新load时传参! 做了说明。
第一步:创建自定义事件类,比如OrderDataEventArgs,创建自定义事件类的时候需要注意两点:首先要继承ExternalDataEventArgs,另外就是要添加序列化声明
[Serializable]
public class OrderDataEventArgs : ExternalDataEventArgs
{
private string corpIdValue;
private string userIdValue;
private int stampDateValue;
private string orderIdValue;
private string xmlValue;
public OrderDataEventArgs(Guid instanceId, string corpId, string userId, int stampDate)
: base(instanceId)
{
orderIdValue = instanceId.ToString();
corpIdValue = corpId;
userIdValue = userId;
stampDateValue = stampDate;
//xmlValue = xml;
WaitForIdle = true;
}
public OrderDataEventArgs(Guid instanceId, string corpId, string userId, string xml)
: base(instanceId)
{
orderIdValue = instanceId.ToString();
corpIdValue = corpId;
userIdValue = userId;
xmlValue = xml;
WaitForIdle = true;
}
public string OrderId
{
get { return orderIdValue; }
set { orderIdValue = value; }
}
public string CorpId
{
get { return corpIdValue; }
set { corpIdValue = value; }
}
public string UserId
{
get { return userIdValue; }
set { userIdValue = value; }
}
public int StampDate
{
get { return stampDateValue; }
set { stampDateValue = value; }
}
public string Xml
{
get { return xmlValue; }
set { xmlValue = value; }
}
}
public class OrderDataEventArgs : ExternalDataEventArgs
{
private string corpIdValue;
private string userIdValue;
private int stampDateValue;
private string orderIdValue;
private string xmlValue;
public OrderDataEventArgs(Guid instanceId, string corpId, string userId, int stampDate)
: base(instanceId)
{
orderIdValue = instanceId.ToString();
corpIdValue = corpId;
userIdValue = userId;
stampDateValue = stampDate;
//xmlValue = xml;
WaitForIdle = true;
}
public OrderDataEventArgs(Guid instanceId, string corpId, string userId, string xml)
: base(instanceId)
{
orderIdValue = instanceId.ToString();
corpIdValue = corpId;
userIdValue = userId;
xmlValue = xml;
WaitForIdle = true;
}
public string OrderId
{
get { return orderIdValue; }
set { orderIdValue = value; }
}
public string CorpId
{
get { return corpIdValue; }
set { corpIdValue = value; }
}
public string UserId
{
get { return userIdValue; }
set { userIdValue = value; }
}
public int StampDate
{
get { return stampDateValue; }
set { stampDateValue = value; }
}
public string Xml
{
get { return xmlValue; }
set { xmlValue = value; }
}
}
第二步:在状态机中添加OrderDataEventArgs类型的属性:
public static DependencyProperty OrderEventArgsProperty = DependencyProperty.Register("OrderEventArgs", typeof(OrderDataEventArgs), typeof(Workflow1));
[DescriptionAttribute("OrderEventArgs")]
[CategoryAttribute("OrderEventArgs Category")]
[BrowsableAttribute(true)]
[DesignerSerializationVisibilityAttribute(DesignerSerializationVisibility.Visible)]
public OrderDataEventArgs OrderEventArgs
{
get
{
return ((OrderDataEventArgs)(base.GetValue(Workflow1.OrderEventArgsProperty)));
}
set
{
base.SetValue(Workflow1.OrderEventArgsProperty, value);
}
}
然后将HandleExternalEventActivity活动事件e参数绑定到上面定义的OrderDataEventArgs属性,这样就能通过事件将需要的数据传递给工作流[DescriptionAttribute("OrderEventArgs")]
[CategoryAttribute("OrderEventArgs Category")]
[BrowsableAttribute(true)]
[DesignerSerializationVisibilityAttribute(DesignerSerializationVisibility.Visible)]
public OrderDataEventArgs OrderEventArgs
{
get
{
return ((OrderDataEventArgs)(base.GetValue(Workflow1.OrderEventArgsProperty)));
}
set
{
base.SetValue(Workflow1.OrderEventArgsProperty, value);
}
}
设置过程图片:
e参数绑定到OrderDataEventArgs属性的代码如下,这是通过属性设置生成的代码:
//
// handleExternalEventActivity1
//
this.handleExternalEventActivity1.EventName = "Create";
this.handleExternalEventActivity1.InterfaceType = typeof(WorkflowInterface.IOrder);
this.handleExternalEventActivity1.Name = "handleExternalEventActivity1";
activitybind1.Name = "Workflow1";
activitybind1.Path = "OrderEventArgs";
workflowparameterbinding1.ParameterName = "e";
workflowparameterbinding1.SetBinding(System.Workflow.ComponentModel.WorkflowParameterBinding.ValueProperty, ((System.Workflow.ComponentModel.ActivityBind)(activitybind1)));
this.handleExternalEventActivity1.ParameterBindings.Add(workflowparameterbinding1);
this.handleExternalEventActivity1.Invoked += new System.EventHandler<System.Workflow.Activities.ExternalDataEventArgs>(this.handleExternalEventActivity1_Invoked);
第三步:为callExternalMethodActivity活动的方法绑定参数,在IOrder接口中,SaveOrder方法需要两个参数:OrderId,UserId,这两个参数的值来自OrderEventArgsProperty 属性。// handleExternalEventActivity1
//
this.handleExternalEventActivity1.EventName = "Create";
this.handleExternalEventActivity1.InterfaceType = typeof(WorkflowInterface.IOrder);
this.handleExternalEventActivity1.Name = "handleExternalEventActivity1";
activitybind1.Name = "Workflow1";
activitybind1.Path = "OrderEventArgs";
workflowparameterbinding1.ParameterName = "e";
workflowparameterbinding1.SetBinding(System.Workflow.ComponentModel.WorkflowParameterBinding.ValueProperty, ((System.Workflow.ComponentModel.ActivityBind)(activitybind1)));
this.handleExternalEventActivity1.ParameterBindings.Add(workflowparameterbinding1);
this.handleExternalEventActivity1.Invoked += new System.EventHandler<System.Workflow.Activities.ExternalDataEventArgs>(this.handleExternalEventActivity1_Invoked);
图片:
代码:
//
// callExternalMethodActivity1
//
this.callExternalMethodActivity1.InterfaceType = typeof(WorkflowInterface.IOrder);
this.callExternalMethodActivity1.MethodName = "SaveOrder";
this.callExternalMethodActivity1.Name = "callExternalMethodActivity1";
activitybind1.Name = "Workflow1";
activitybind1.Path = "OrderEventArgs.OrderId";
workflowparameterbinding1.ParameterName = "orderId";
workflowparameterbinding1.SetBinding(System.Workflow.ComponentModel.WorkflowParameterBinding.ValueProperty, ((System.Workflow.ComponentModel.ActivityBind)(activitybind1)));
activitybind2.Name = "Workflow1";
activitybind2.Path = "OrderEventArgs.UserId";
workflowparameterbinding2.ParameterName = "userId";
workflowparameterbinding2.SetBinding(System.Workflow.ComponentModel.WorkflowParameterBinding.ValueProperty, ((System.Workflow.ComponentModel.ActivityBind)(activitybind2)));
this.callExternalMethodActivity1.ParameterBindings.Add(workflowparameterbinding1);
this.callExternalMethodActivity1.ParameterBindings.Add(workflowparameterbinding2);
OrderEventArgsProperty 属性数据传递的媒介,客户端组装OrderDataEventArgs,通过事件发送给工作流,工作流执行CallExternalMethodActivity的方法。// callExternalMethodActivity1
//
this.callExternalMethodActivity1.InterfaceType = typeof(WorkflowInterface.IOrder);
this.callExternalMethodActivity1.MethodName = "SaveOrder";
this.callExternalMethodActivity1.Name = "callExternalMethodActivity1";
activitybind1.Name = "Workflow1";
activitybind1.Path = "OrderEventArgs.OrderId";
workflowparameterbinding1.ParameterName = "orderId";
workflowparameterbinding1.SetBinding(System.Workflow.ComponentModel.WorkflowParameterBinding.ValueProperty, ((System.Workflow.ComponentModel.ActivityBind)(activitybind1)));
activitybind2.Name = "Workflow1";
activitybind2.Path = "OrderEventArgs.UserId";
workflowparameterbinding2.ParameterName = "userId";
workflowparameterbinding2.SetBinding(System.Workflow.ComponentModel.WorkflowParameterBinding.ValueProperty, ((System.Workflow.ComponentModel.ActivityBind)(activitybind2)));
this.callExternalMethodActivity1.ParameterBindings.Add(workflowparameterbinding1);
this.callExternalMethodActivity1.ParameterBindings.Add(workflowparameterbinding2);
下面是关于工作流接口和实现的代码:
[ExternalDataExchange]
public interface IOrder
{
/// <summary>
/// 创建订单
/// </summary>
event EventHandler<OrderDataEventArgs> Create;
/// <summary>
/// 确认订单
/// </summary>
event EventHandler<OrderDataEventArgs> Confirm;
/// <summary>
/// 完成
/// </summary>
event EventHandler<OrderDataEventArgs> Complete;
void SaveOrder(string orderId, string userId);
}
public interface IOrder
{
/// <summary>
/// 创建订单
/// </summary>
event EventHandler<OrderDataEventArgs> Create;
/// <summary>
/// 确认订单
/// </summary>
event EventHandler<OrderDataEventArgs> Confirm;
/// <summary>
/// 完成
/// </summary>
event EventHandler<OrderDataEventArgs> Complete;
void SaveOrder(string orderId, string userId);
}
[Serializable]
public class Order : IOrder
{
public void RaiseCreateEvent(OrderDataEventArgs args)
{
if (Create != null)
{
Create(null, args);
}
}
public void RaisConfirmEvent(OrderDataEventArgs args)
{
if (Confirm != null)
{
Confirm(null, args);
}
}
public void RaisCompleteEvent(OrderDataEventArgs args)
{
if (Complete != null)
{
Complete(null, args);
}
}
#region IOrder Members
public event EventHandler<OrderDataEventArgs> Create;
public event EventHandler<OrderDataEventArgs> Confirm;
public event EventHandler<OrderDataEventArgs> Complete;
public void SaveOrder(string orderId, string userId)
{
// 得到通过OrderDataEventArgs传递的数据
Console.WriteLine("OrderId value {0}, UserId value {1}", orderId, userId);
}
#endregion
}
最后一步:调用代码:public class Order : IOrder
{
public void RaiseCreateEvent(OrderDataEventArgs args)
{
if (Create != null)
{
Create(null, args);
}
}
public void RaisConfirmEvent(OrderDataEventArgs args)
{
if (Confirm != null)
{
Confirm(null, args);
}
}
public void RaisCompleteEvent(OrderDataEventArgs args)
{
if (Complete != null)
{
Complete(null, args);
}
}
#region IOrder Members
public event EventHandler<OrderDataEventArgs> Create;
public event EventHandler<OrderDataEventArgs> Confirm;
public event EventHandler<OrderDataEventArgs> Complete;
public void SaveOrder(string orderId, string userId)
{
// 得到通过OrderDataEventArgs传递的数据
Console.WriteLine("OrderId value {0}, UserId value {1}", orderId, userId);
}
#endregion
}
WorkflowHelper workflowHelper;
Order OrderEx;
public Form1()
{
InitializeComponent();
OrderEx = new Order();
workflowHelper = new WorkflowHelper(OrderEx);
}
Guid InstanceId = new Guid("74E512AA-AC75-45a1-8818-0F77C8670AB4");
string userId = "F8E05185-3A4B-48c8-8C37-3B4EBD03B7AA";
string corpId = "b5d183ab-dc95-4fdd-a7ad-562822047ce6";
private void btnCreate_Click(object sender, EventArgs e)
{
workflowHelper.LoadWorkflow(InstanceId);
OrderDataEventArgs ea = new OrderDataEventArgs(InstanceId, corpId, userId, "");
ea.WaitForIdle = true;
OrderEx.RaiseCreateEvent(ea);
workflowHelper.LoadWorkflow(InstanceId);
}
private void btnConfirm_Click(object sender, EventArgs e)
{
//InstanceId = new Guid(textBox1.Text);
workflowHelper.LoadWorkflow(InstanceId);
OrderDataEventArgs ea = new OrderDataEventArgs(InstanceId, corpId, userId, 1);
ea.WaitForIdle = true;
OrderEx.RaisConfirmEvent(ea);
workflowHelper.LoadWorkflow(InstanceId);
}
Order OrderEx;
public Form1()
{
InitializeComponent();
OrderEx = new Order();
workflowHelper = new WorkflowHelper(OrderEx);
}
Guid InstanceId = new Guid("74E512AA-AC75-45a1-8818-0F77C8670AB4");
string userId = "F8E05185-3A4B-48c8-8C37-3B4EBD03B7AA";
string corpId = "b5d183ab-dc95-4fdd-a7ad-562822047ce6";
private void btnCreate_Click(object sender, EventArgs e)
{
workflowHelper.LoadWorkflow(InstanceId);
OrderDataEventArgs ea = new OrderDataEventArgs(InstanceId, corpId, userId, "");
ea.WaitForIdle = true;
OrderEx.RaiseCreateEvent(ea);
workflowHelper.LoadWorkflow(InstanceId);
}
private void btnConfirm_Click(object sender, EventArgs e)
{
//InstanceId = new Guid(textBox1.Text);
workflowHelper.LoadWorkflow(InstanceId);
OrderDataEventArgs ea = new OrderDataEventArgs(InstanceId, corpId, userId, 1);
ea.WaitForIdle = true;
OrderEx.RaisConfirmEvent(ea);
workflowHelper.LoadWorkflow(InstanceId);
}