How to persist and resume workflow in workflow-foundation?

0 votes
asked Jul 11, 2013 in Workflow Foundation by Miller

What is the functionality behind workflow persistence and resume in workflow foundation? What are the steps I need follow to properly implement persistence service? Please post me some sample if possible.


1 Answer

+1 vote
answered Jul 12, 2013 by Aadhira (1,213 points)
selected Jul 12, 2013 by administrator
Best answer

Workflow supports built in persistence service to store the intermediate state of the workflow. So that, whenever we want to resume the workflow from the same place we left, we can do. The persistence service can be called automatically by the workflow runtime or it can be called manually using WorkflowApplication instance.Persist().

Workflow runtime will automatically call the persistence service whenever the workflow goes to idle status. But unless you setup, InstanceStore on the Workflow Application instance, the persistence will not happen. Workflow framework comes with a built in Instance store which is SQLWorkflowInstanceStore. Either you can use this service or you may implement your own.

When the runtime persist the workflow, it will not only store detail about the workflow, but also will store the data which is being used. So, to do that, persistence service will serialize the data before persist. If you have used any of the data which cannot be serialized automatically, or if you are using your own Persistence Service, then you may want to take care of this serialization process as well.

Workflow goes into idle status when you are waiting for some data or waiting for some external input. You can use “Delay Activity”, if you know the exact time, how long you want to wait.

If the waiting time is unknown, then “Bookmark” would be the right option. Once you create bookmark, the workflow runtime will put the workflow to idle status and call the persistence service.

I have done some sample application using WPF and WF, in that I have used SQLWorkflowInstance Store. From that I did borrow some code and given here. Bookmark is the same idea as bookmark we do in real-time while reading book. When there is a bookmark then we can resume reading from that place. Same like that, we can resume the workflow from the place we left.

Bookmarks can only be created by the “Native Activities” and cannot be done with “Code Activity”.

One other thing to know is, even if we persist the workflow in to DB, whatever the workflow loaded into memory will be in the memory itself. If you do not want the workflow for now, then you can unload the workflow from memory using PersistebleIdleAction.

When the workflow goes to idle, it runtime will raise application.PersistableIdle event. In that events handle, you can return “PersistableIdleAction.Unload” to inform the runtime to unload the workflow from memory.

Steps to use SQLWorkflowInstance Service:
If you are using .NET Framework 4, then use the below files to create tables and stored procedures required for persistence service.

Once you do that, you can use the below code to inform the runtime to use the persistence service.

WorkflowApplication application = new WorkflowApplication(
      new SoftwareRequestWorkflow(),
      new Dictionary<string, object>()
    { "SoftwareRequestWFContextArg", workflowContext}
    this.SoftwareRequest.WorkflowInstance = application.Id;
    application.InstanceStore = (InstanceStore)new SqlWorkflowInstanceStore(connectionString);

I have used the below code to create the bookmark in my customer activity which is derived from “NativeActivity”.To bookmark and persist workflow, you need to override a property (CanInduceIdle) of “NativeActivity” and must return true on your customer activity.

public sealed class ManagerResponseActivity : NativeActivity
 public OutArgument<string> RequestStatusArg { get; set; }
 protected override bool CanInduceIdle
  return true;
 protected override void Execute(NativeActivityContext context)
  context.CreateBookmark("ManagerResponse", new BookmarkCallback(OnManagerResponseReceived));
 public void OnManagerResponseReceived(NativeActivityContext context, Bookmark bookmark, Object managerResponse)
  RequestStatus status = (RequestStatus)managerResponse;
  RequestStatusArg.Set(context, status.ToString());

Whenever you want to resume the persisted workflow, you can use below code. To resume the workflow, you need both workflow instance unique id and bookmark name.

WorkflowApplication application = new WorkflowApplication(new SoftwareRequestWorkflow());
application.InstanceStore = (InstanceStore)new SqlWorkflowInstanceStore(connectionString);
application.ResumeBookmark("ManagerResponse", ResponseObject);
commented Nov 11, 2015 by raj
thank you for this info, very need full
commented Sep 25, 2016 by anonymous
Thanks, i have few questions:

1) what is workflowContext in your code?
2) Where in your code you are persisting workflow state to database?
3) where you configuring "PersistableIdleAction.Persist"?

Your answer


Your name to display (optional):
Privacy: Your email address will only be used for sending these notifications.
Anti-spam verification:
To avoid this verification in future, please log in or register.
site design / logo / content © 2013 - 2015