WorkflowInstance.EnqueueItem 方法
定义
重要
一些信息与预发行产品相关,相应产品在发行之前可能会进行重大修改。 对于此处提供的信息,Microsoft 不作任何明示或暗示的担保。
将消息同步发送到指定工作流队列。
public:
void EnqueueItem(IComparable ^ queueName, System::Object ^ item, System::Workflow::Runtime::IPendingWork ^ pendingWork, System::Object ^ workItem);
public void EnqueueItem (IComparable queueName, object item, System.Workflow.Runtime.IPendingWork pendingWork, object workItem);
member this.EnqueueItem : IComparable * obj * System.Workflow.Runtime.IPendingWork * obj -> unit
Public Sub EnqueueItem (queueName As IComparable, item As Object, pendingWork As IPendingWork, workItem As Object)
参数
- queueName
- IComparable
WorkflowQueue 的名称。
- item
- Object
要排入队列的对象。
- pendingWork
- IPendingWork
允许在传递 IPendingWork 时通知发送方的 item
。
- workItem
- Object
要传递给 IPendingWork 方法的对象。
例外
queueName
为空引用(在 Visual Basic 中为 Nothing
)。
示例
下面的代码示例演示如何使用 EnqueueItem。 当 WorkflowIdled 事件发生时,调用本示例中定义的 OnWorkflowIdled
方法。 该方法使用 WorkflowInstance 属性来确定哪个工作流处于空闲状态,然后通过调用 GetWorkflowQueueData 方法来获取该工作流实例的排队项的集合。 该代码会循环访问该集合以确定哪个活动正在等待使工作流处于空闲状态的事件。 然后,该代码将使用 EnqueueItem 方法以及事件队列项的名称向工作流队列发出一个异常。
此代码示例摘自 Program.cs 文件中的“取消工作流”SDK 示例。 有关详细信息,请参阅 取消工作流。
static void OnWorkflowIdled(object sender, WorkflowEventArgs e)
{
WorkflowInstance workflow = e.WorkflowInstance;
Console.WriteLine("\n...waiting for 3 seconds... \n");
Thread.Sleep(3000);
// what activity is blocking the workflow
ReadOnlyCollection<WorkflowQueueInfo> wqi = workflow.GetWorkflowQueueData();
foreach (WorkflowQueueInfo q in wqi)
{
EventQueueName eq = q.QueueName as EventQueueName;
if (eq != null)
{
// get activity that is waiting for event
ReadOnlyCollection<string> blockedActivity = q.SubscribedActivityNames;
Console.WriteLine("Host: Workflow is blocked on " + blockedActivity[0]);
// this event is never going to arrive eg. employee left the company
// lets send an exception to this queue
// it will either be handled by exception handler that was modeled in workflow
// or the runtime will unwind running compensation handlers and exit the workflow
Console.WriteLine("Host: This event is not going to arrive");
Console.WriteLine("Host: Cancel workflow with unhandled exception");
workflow.EnqueueItem(q.QueueName, new Exception("ExitWorkflowException"), null, null);
}
}
}
Shared Sub OnWorkflowIdled(ByVal sender As Object, ByVal e As WorkflowEventArgs)
Dim workflow As WorkflowInstance = e.WorkflowInstance
Console.WriteLine(vbCrLf + "...waiting for 3 seconds... " + vbCrLf)
Thread.Sleep(3000)
' what activity is blocking the workflow
Dim wqi As ReadOnlyCollection(Of WorkflowQueueInfo) = workflow.GetWorkflowQueueData()
For Each q As WorkflowQueueInfo In wqi
Dim eq As EventQueueName = TryCast(q.QueueName, EventQueueName)
If eq IsNot Nothing Then
' get activity that is waiting for event
Dim blockedActivity As ReadOnlyCollection(Of String) = q.SubscribedActivityNames
Console.WriteLine("Host: Workflow is blocked on " + blockedActivity(0))
' this event is never going to arrive eg. employee left the company
' lets send an exception to this queue
' it will either be handled by exception handler that was modeled in workflow
' or the runtime will unwind running compensation handlers and exit the workflow
Console.WriteLine("Host: This event is not going to arrive")
Console.WriteLine("Host: Cancel workflow with unhandled exception")
workflow.EnqueueItem(q.QueueName, New Exception("ExitWorkflowException"), Nothing, Nothing)
End If
Next
End Sub
注解
将 item
发送到指定的 WorkflowQueue。 若要在传递消息之后获得通知,您可以在您的服务中实现 IPendingWork,并将一个 workItem
和一个 IPendingWork 对象传送至 EnqueueItem。 如果不想要此类通知,则可以为 Nothing
和 pendingWork
传送空引用(在 Visual Basic 中为 workItem
)。
将此方法用于状态机工作流时,可能会收到异常,并显示消息“未启用队列'{0}”。当状态机的当前状态不知道如何处理特定事件时,就会发生这种情况。 例如,当前状态以外的其他状态包含 EventDrivenActivity,而后者包含由队列“{0}”表示的 HandleExternalEventActivity 时。
注意
不保证工作流实例按消息发送的顺序接收消息。 例如,如果在现有队列(队列 A)中接收消息会导致工作流创建另一个队列(队列 B),该队列随后会侦听在发送第一条消息之后发送的另一条消息,那么,有可能第二条消息会先到达,但由于尚未创建其队列而无法接收。 为了避免此问题,在发送第二条消息之前,必须先验证第二个队列是否存在(使用 GetWorkflowQueueData)。