How To: Control Persistence After Idling

When hosting a workflow service that persists data, you may want to control what occurs after a period of idleness. For example, if you have a web farm with two servers, and a “dumb” router that routes messages to either server randomly, you may want to unload the service often. Or, if you have a “smart” router that routes messages to a particular server, you may not want to unload the service as frequently.

The WorkflowIdleBehavior lets you specify the time between when a service instance goes idle and the time a service instance is persisted and unloaded. Two properties set these time periods. TimeToUnload specifies the time period between when a workflow service instance goes idle and when the workflow service is unloaded. Unloading implicitly implies persistence. TimeToPersist specifies the time period between when a workflow service instance goes idle and when the workflow service instance is persisted. The TimeToPersist value only takes effect when it is less than TimeToUnload. If TimeToPersist elapses before TimeToUnload, persistence must complete before the unloading occurs. Therefore, the unloading may occur after TimeToUnload elapses. Both of the time spans specified for these properties do not start elapsing until the workflow service instance becomes idle. A workflow service instance becomes idle when the following are true:

  • ActivityRuntime.Idle = true.

  • ActivityRuntime.CanPersist = true.

  • There are no pending requests for the workflow service instance.

  • A persistence provider must be configured.

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

To change idle behavior in configuration

  • The following example changes the time to wait before persisting and unloading through a configuration file.

    <system.serviceModel>
      <services >
        <service name="HelloWorkFlow">        
        </service>
      </services>
      <behaviors>
        <serviceBehaviors>
          <behavior name="OnIdle">
            <workflowIdle timeToPersist="00:04:00" timeToUnload="00:05:00" />
          </behavior>
        </serviceBehaviors>
      </behaviors>
    </system.serviceModel>
    

See Also

Reference

WorkflowIdleBehavior

Other Resources

<workflowIdle>

Last Published: 2010-04-25