共用方式為


WorkflowInstance.EnqueueItem 方法

定義

同步公佈訊息至指定的工作流程佇列。

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 為 null 參考 (在 Visual Basic 中為 Nothing)。

工作流程執行階段引擎不在執行中。

-或-

WorkflowQueue 指定的 queueName 並不存在。

-或-

WorkflowQueue 指定的 queueName 未啟用。

範例

下列程式碼範例將示範如何使用 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,並且傳遞 workItemIPendingWork 物件至 EnqueueItem。 如果不想要這類通知,您可以傳遞 NothingpendingWork 的 null 參考 (在 Visual Basic 中為 workItem)。

搭配狀態機器工作流程使用此方法時,您可能會收到「佇列 ' {0} ' 未啟用」訊息的例外狀況。當狀態電腦的目前狀態不知道如何處理特定事件時,就會發生這種情況。 例如,當目前狀態以外的某種狀態包含 EventDrivenActivity,而且其中包含由佇列 '{0}' 所代表的 HandleExternalEventActivity 時。

注意

工作流程執行個體接收訊息的順序,不一定會與訊息的傳送順序相同。 例如,如果在現有佇列 (佇列 A) 中接收訊息導致工作流程建立另一個佇列 (佇列 B),後者接著接聽在第一個訊息後傳送的另一個訊息,第二個訊息可能會先到達,而且因為它的佇列尚未建立而無法被接收。 若要避免這個問題,應該先確認 (使用 GetWorkflowQueueData) 第二個佇列存在後,再傳送第二個訊息。

適用於

另請參閱