How to: Configure Idle Behavior with WorkflowServiceHost

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

  1. 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. The timeToPersist attribute specifies the time period between when a workflow service instance goes idle and when the workflow service instance is persisted. The default value for timeToUnload is 1 minute. The default value for timeToPersist is MaxValue. If you want to keep idle instances in memory but persist them for robustness, set values so that timeToPersist < timeToUnload. If you want to prevent idle instances from being unloaded, set timeToUnload to MaxValue. For more information about WorkflowIdleBehavior, see Workflow Service Host Extensibility

    Note

    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 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);
    
    ' 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)
    

See also