Redigera

Dela via


WorkflowPersistenceService.SaveCompletedContextActivity(Activity) Method

Definition

When implemented in a derived class, saves the specified completed scope to a data store.

protected public:
 abstract void SaveCompletedContextActivity(System::Workflow::ComponentModel::Activity ^ activity);
protected internal abstract void SaveCompletedContextActivity (System.Workflow.ComponentModel.Activity activity);
abstract member SaveCompletedContextActivity : System.Workflow.ComponentModel.Activity -> unit
Protected Friend MustOverride Sub SaveCompletedContextActivity (activity As Activity)

Parameters

activity
Activity

An Activity that represents the completed scope.

Examples

The following example demonstrates an implementation of the SaveCompletedContextActivity method. This example is from the Custom Persistence Service sample, from the FilePersistenceService.cs file. For more information, see Custom Persistence Service Sample.

// Save the completed activity state.
protected override void SaveCompletedContextActivity(Activity activity)
{
    Guid contextGuid = (Guid)activity.GetValue(Activity.ActivityContextGuidProperty);
    Console.WriteLine("Saving completed activity context: {0}", contextGuid);
    SerializeToFile(
        WorkflowPersistenceService.GetDefaultSerializedForm(activity), contextGuid);
}
' Save completed activity state
Protected Overrides Sub SaveCompletedContextActivity(ByVal activity As System.Workflow.ComponentModel.Activity)
    Dim contextGuid As Guid = CType(activity.GetValue(activity.ActivityContextGuidProperty), Guid)
    Console.WriteLine("Saving completed activity context: 0}", contextGuid)
    SerializeToFile( _
        WorkflowPersistenceService.GetDefaultSerializedForm(activity), contextGuid)
End Sub

Remarks

The workflow runtime engine saves the state of completed scope activities in order to implement compensation. You must call one of the overloaded Save methods to serialize activity into a Stream; you may then choose to additionally process the Stream before writing it to your data store. However, when the workflow runtime engine calls LoadCompletedContextActivity, you must restore an identical copy of the activity.

You must be able to associate the completed scope with its enclosing workflow instance to mark the scope as unneeded in your data store when the workflow instance finishes or is terminated. Therefore, you should also save the Guid of the workflow instance that is associated with the completed scope; this can be obtained from the InstanceId property of the WorkflowInstance associated with activity.

LoadCompletedContextActivity takes the Guid of the completed scope as a parameter. Therefore, you must also save the ContextGuid property associated with activity. This property can be referenced through the ActivityContextGuidProperty field of activity.

If you are implementing a persistence service that uses a durable store, to maintain consistency with the internal state of the workflow runtime engine, you should participate in workflow transaction batching to defer the actual write to your durable store until a workflow commit point. To participate in batching, add a work item to the WorkBatch property that represents the pending changes to the database, and implement the IPendingWork interface in your persistence service.

If you cannot save the completed scope to your data store, you should throw a PersistenceException with an appropriate error message.

Applies to