WorkflowInstance.EnqueueItem Metodo

Definizione

Inserisce un messaggio in modo sincrono nella coda del flusso di lavoro specificata.

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)

Parametri

queueName
IComparable

Nome di WorkflowQueue.

item
Object

Oggetto da accodare.

pendingWork
IPendingWork

Interfaccia IPendingWork che consente al mittente di ricevere una notifica quando item viene recapitato.

workItem
Object

Oggetto da passare ai metodi IPendingWork.

Eccezioni

queueName è un riferimento null (Nothing in Visual Basic).

Il motore di runtime del flusso di lavoro non è in esecuzione.

-oppure-

La classe WorkflowQueue specificata da queueName non esiste.

-oppure-

La classe WorkflowQueue specificata da queueName non è abilitata.

Esempio

Nell'esempio di codice seguente viene illustrato come utilizzare EnqueueItem. Quando si verifica l'evento WorkflowIdled viene chiamato il metodo OnWorkflowIdled definito in questo esempio. Determina quale flusso di lavoro venga reso inattivo mediante la proprietà WorkflowInstance e quindi ottiene una raccolta di elementi in coda per l'istanza del flusso di lavoro chiamando il metodo GetWorkflowQueueData. Il codice scorre la raccolta per determinare quale attività è in attesa dell'evento che ha sospeso il flusso di lavoro. Invia quindi un'eccezione alla coda del flusso di lavoro mediante il metodo EnqueueItem insieme al nome dell'elemento in coda dell'evento.

Questo esempio di codice fa parte dell'esempio SDK Canceling a Workflow nel file Program.cs. Per altre informazioni, vedere Annullamento di un flusso di lavoro.

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

Commenti

Invia l'oggetto item alla classe WorkflowQueue specificata. Se si desidera ricevere una notifica quando il messaggio viene recapitato, è possibile implementare IPendingWork nel servizio e passare un workItem e un oggetto IPendingWork a EnqueueItem. Se non si desidera tale notifica, è possibile passare un riferimento null (Nothing in Visual Basic) per pendingWork e workItem.

Quando si usa questo metodo con un flusso di lavoro del computer di stato, potrebbe essere visualizzata un'eccezione con il messaggio "Coda '{0}' non è abilitato". Ciò accade quando lo stato corrente della macchina di stato non sa come gestire un evento specifico. Ad esempio, quando uno stato diverso da quello corrente contiene la classe EventDrivenActivity che contiene l'oggetto HandleExternalEventActivity rappresentato dalla coda "{0}".

Nota

Non è garantito che i messaggi vengano ricevuti dall'istanza del flusso di lavoro nell'ordine in cui sono inviati. Ad esempio, se la ricezione di un messaggio in una coda esistente (Coda A) fa sì che un flusso di lavoro crei un'altra coda (Coda B), che quindi è in ascolto per un altro messaggio inviato dopo il primo messaggio, è possibile che il secondo messaggio arrivi per primo e non venga ricevuto poiché la coda non è ancora stata creata. Per evitare questo problema, il secondo messaggio non deve essere inviato fino a quando la presenza della seconda coda viene verificata (mediante il metodo GetWorkflowQueueData).

Si applica a

Vedi anche