WorkflowInstance.EnqueueItemOnIdle 方法

定义

当工作流处于空闲状态时,将消息发送到指定的工作流队列。 在确认工作流计划程序处于空闲状态(即没有执行任何有效操作)之后,EnqueueItemOnIdle(IComparable, Object, IPendingWork, Object) 将一直等待,直至工作流达到空闲点并编排队列。

public:
 void EnqueueItemOnIdle(IComparable ^ queueName, System::Object ^ item, System::Workflow::Runtime::IPendingWork ^ pendingWork, System::Object ^ workItem);
public void EnqueueItemOnIdle (IComparable queueName, object item, System.Workflow.Runtime.IPendingWork pendingWork, object workItem);
member this.EnqueueItemOnIdle : IComparable * obj * System.Workflow.Runtime.IPendingWork * obj -> unit
Public Sub EnqueueItemOnIdle (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)。

工作流运行时引擎未运行。

  • 或 - 工作流实例处于挂起状态。

  • 或 - WorkflowQueue 指定的 queueName 不存在。

  • 或 - 由 WorkflowQueue 指定的 queueName 未启用。

示例

下面的示例演示如何使用 EnqueueItemOnIdle 方法将信息发送到工作流实例。 首先,创建并启动 WorkflowInstance 对象;接着,创建一个实现 IPendingWork 接口的对象。 EnqueueItemOnIdle 然后,调用队列名称、排队项和要传递给 pendingWork 对象的方法的工作项的字符串值。

// Create a workflow runtime environment
WorkflowRuntime workflowRuntime = new WorkflowRuntime();
// Create a new instance of the out-of-box SqlWorkflowPersistenceService.
// Use the non-locking constructor, since we're only creating a single Workflow Runtime.
NameValueCollection parameters = new NameValueCollection();
parameters.Add("ConnectionString",
    "Initial Catalog=SqlPersistenceService;Data Source=localhost;Integrated Security=SSPI;");
//Set UnloadOnIdle to true, so that the service will persist the workflow
parameters.Add("UnloadOnIdle", "true");
SqlWorkflowPersistenceService persistenceService =
   new SqlWorkflowPersistenceService(parameters);

// Add the service to the runtime
workflowRuntime.AddService(persistenceService);
// Create a WorkflowInstance object
WorkflowInstance workflowInstance = workflowRuntime.CreateWorkflow(typeof(Workflow1));
// Start the instance
workflowInstance.Start();
// Create an instance of a class that implements IPendingWork for notification
PendingService pendingWork = new PendingService();
// Send the workflow the message
workflowInstance.EnqueueItemOnIdle("ActionQueue", "StartWork", pendingWork, "ActionItem");
' Create a workflow runtime environment
Dim workflowRuntime As New WorkflowRuntime()
' Create a new instance of the out-of-box SqlWorkflowPersistenceService.
' Use the non-locking constructor, since we're only creating a single Workflow Runtime.
Dim parameters As New NameValueCollection()
parameters.Add("ConnectionString", _
    "Initial Catalog=SqlPersistenceServiceData Source=localhostIntegrated Security=SSPI")
'Set UnloadOnIdle to true, so that the service will persist the workflow
parameters.Add("UnloadOnIdle", "true")
Dim persistenceService As _
   New SqlWorkflowPersistenceService(parameters)

' Add the service to the runtime
workflowRuntime.AddService(persistenceService)
' Create a WorkflowInstance object
Dim workflowInstance As WorkflowInstance = workflowRuntime.CreateWorkflow(GetType(Workflow1))
' Start the instance
workflowInstance.Start()
' Create an instance of a class that implements IPendingWork for notification
Dim pendingWork As New PendingService()
' Send the workflow the message
workflowInstance.EnqueueItemOnIdle("ActionQueue", "StartWork", pendingWork, "ActionItem")

注解

等待工作流实例变为空闲状态,然后将 item 发送到指定的 WorkflowQueue。 如果在工作流实例处于挂起状态时调用 EnqueueItemOnIdle,工作流运行时引擎将引发 InvalidOperationException。 若要在传递消息之后获得通知,您可以在您的服务中实现 IPendingWork,并将一个 workItem 和一个 IPendingWork 对象传送至 EnqueueItem。 如果不想要此类通知,则可以为 NothingpendingWork 传送空引用(在 Visual Basic 中为 workItem)。

在将此方法和状态机工作流一起使用时,您可能会收到异常,并收到消息“未启用队列‘{0}’”。 当状态机的当前状态不知道如何处理特定事件时,则会发生此异常。 例如,当前状态以外的其他状态包含 EventDrivenActivity,而后者包含由队列“{0}”表示的 HandleExternalEventActivity 时。

适用于