How to: Configure Idle Behavior with WorkflowServiceHost
This topic applies to Windows Workflow Foundation 4 (WF4).
Workflows go idle when they encounter a bookmark that must be resumed by some external stimulus, for example when the workflow instance is waiting for a message to be delivered using a Receive activity. WorkflowIdleBehavior is a behavior that allows you to specify the time between when a service instance goes idle and when the instance is persisted or unloaded. It contains two properties that enable you to set these time spans. TimeToPersist specifies the time span between when a workflow service instance goes idle and when the workflow service instance is persisted. TimeToUnload specifies the time span between when a workflow service instance goes idle and when the workflow service instance is unloaded, where unload means persisting the instance to the instance store and removing it from memory. This topic explains how to configure the WorkflowIdleBehavior in a configuration file.
To configure WorkflowIdleBehavior
Add a <workflowIdle> element to the <behavior> element within the <serviceBehaviors> element as shown in the following example.
<behaviors> <serviceBehaviors> <behavior name=""> <workflowIdle timeToUnload="0:05:0" timeToPersist="0:04:0"/> </behavior> </serviceBehaviors> </behaviors>
The
timeToUnload
attribute specifies the time period between when a workflow service instance goes idle and when the workflow service is unloaded. ThetimeToPersist
attribute specifies the time period between when a workflow service instance goes idle and when the workflow service instance is persisted. The default value fortimeToUnload
is 1 minute. The default value fortimeToPersist
is MaxValue. If you want to keep idle instances in memory but persist them for robustness, set values so thattimeToPersist
<timeToUnload
. If you want to prevent idle instances from being unloaded, settimeToUnload
to MaxValue. For more information about WorkflowIdleBehavior, see Workflow Service Host ExtensibilityNote
The preceding configuration sample is using simplified configuration. For more information, see Simplified Configuration.
To change idle behavior in code
The following example changes the time to wait before persisting and unloading programmatically.
' Code to create a WorkflowServiceHost not shown here. ' Note that SqlWorkflowInstanceStore is in the System.Activities.DurableInstancing.dll host.DurableInstancingOptions.InstanceStore = New SqlWorkflowInstanceStore(connectionString) ' Create a new workflow behavior. Dim alteredBehavior As WorkflowIdleBehavior = New WorkflowIdleBehavior() ' Alter the time to persist and unload. alteredBehavior.TimeToPersist = New TimeSpan(0, 4, 0) alteredBehavior.TimeToUnload = New TimeSpan(0, 5, 0) ' Remove the existing behavior and add the new one. host.Description.Behaviors.Remove(Of WorkflowIdleBehavior)() host.Description.Behaviors.Add(alteredBehavior)
// Code to create a WorkFlowServiceHost is not shown here. // Note that SqlWorkflowInstanceStore is in the System.Activities.DurableInstancing.dll host.DurableInstancingOptions.InstanceStore = new SqlWorkflowInstanceStore(connectionString[]); WorkflowIdleBehavior alteredBehavior = new WorkflowIdleBehavior(); // Alter the time to persist and unload. alteredBehavior.TimeToPersist = new TimeSpan(0, 4, 0); alteredBehavior.TimeToUnload = new TimeSpan(0, 5, 0); //Remove the existing behavior and replace it with the new one. host.Description.Behaviors.Remove<WorkflowIdleBehavior>(); host.Description.Behaviors.Add(alteredBehavior);
See Also
Concepts
Workflow Service Host Extensibility