Suspended Instance Management
The SuspendedInstanceManagement sample demonstrates how to manage workflow instances that have been suspended. The default action for WorkflowUnhandledExceptionBehavior is AbandonAndSuspend
. This means that by default, unhandled exceptions thrown from a workflow instance hosted in the WorkflowServiceHost will cause the instance to be disposed from memory (abandoned) and the durable/persisted version of the instance to be marked as suspended. A suspended workflow instance will not be able to run until it has been unsuspended.
The sample shows how a command-line utility can be implemented to query for suspended instances, and how to give the user the option to resume or terminate the instance. In this sample, a workflow service intentionally throws an exception, causing it to become suspended. The command-line utility can then be used to query for the instance and subsequently resume or terminate the instance.
Demonstrates
WorkflowServiceHost with WorkflowUnhandledExceptionBehavior and WorkflowControlEndpoint in Windows Workflow Foundation (WF).
Discussion
The command-line utility implemented in this sample is specific to the SQL instance store implementation that ships in .NET Framework 4.6.1. If you have a custom implementation of the instance store, then you can adapt this utility by replacing the WorkflowInstanceCommand
implementations in the sample with implementations that are specific to your instance store.
The provided implementation runs SQL commands against the SQL instance store directly to list suspended instances, and it relies on a WorkflowControlEndpoint added to the WorkflowServiceHost in order to resume or terminate the instances.
To set up, build, and run the sample
This sample requires that the following Windows components are enabled:
Microsoft Message Queues (MSMQ) Server
SQL Server Express
Set up the SQL Server database.
From a Visual Studio command prompt, run "setup.cmd" from the SuspendedInstanceManagement sample directory, which does the following:
Creates a persistence database using SQL Server Express. If the persistence database already exists, then it is dropped and re-created
Sets up the database for persistence.
Adds IIS APPPOOL\DefaultAppPool and NT AUTHORITY\Network Service to the InstanceStoreUsers role that was defined when setting up the database for persistence.
Set up the service queue.
In Visual Studio, right-click the SampleWorkflowApp project and click Set as Startup Project.
Compile and run the SampleWorkflowApp by pressing F5. This will create the required queue.
Press Enter to stop the SampleWorkflowApp.
Open the Computer Management console by running Compmgmt.msc from a command prompt.
Expand Service and Applications, Message Queuing, Private Queues.
Right click the ReceiveTx queue and select Properties.
Select the Security tab and allow Everyone to have permissions to Receive Message, Peek Message, and Send Message.
Now, run the sample.
In Visual Studio, run the SampleWorkflowApp project again without debugging by pressing Ctrl+F5. Two endpoint addresses will be printed in the console window: one for the application endpoint and then other from the WorkflowControlEndpoint. A workflow instance is then created, and tracking records for that instance will appear in the console window. The workflow instance will throw an exception causing the instance to be suspended and aborted.
The command-line utility can then be used to take further action on any of these instances. The syntax for command line arguments is as follows::
SuspendedInstanceManagement -Command:[CommandName] -Server:[ServerName] -Database:[DatabaseName] -InstanceId:[InstanceId]
The supported commands are:
Query
,Resume
, andTerminate
. The InstanceId switch is only required forResume
andTerminate
operations.
To cleanup (Optional)
Open the Computer Management console by running Compmgmt.msc.
Expand Service and Applications, Message Queuing, Private Queues.
Delete the ReceiveTx queue.
To remove the persistence database, run cleanup.cmd.