views:

211

answers:

2

This seems like an elementary question, but I can't find the answer anywhere:

I have several different types of long-running state machine workflows that I am working with across multiple host applications with a central database. I am using the SqlWorkflowPersistenceService to persist them. So say I have three workflow types WorkflowOne, WorkflowTwo, and WorkflowThree. An instance of each of those is started and persisted to the database by one user. Another user comes along and wants to make various changes, so they fire up the application. Now I can use the persistence service to give me a list of all the persisted instances but how do I know which is of type WorkflowOne and which is WorkflowTwo or WorkflowThree?

A: 

Not sure if this is the best way, but am simply hydrating each WF instance and then comparing it's Name property to see if that's the instance I am looking for.

Ben Rice
I'll leave this open for a couple days to see if anyone else has a better way. Also thought about creating my own persistence service and just adding another field to the InstanceState table to hold the type, but thought that was sort of overkill.
Ben Rice
+3  A: 

There are a few ways you can do this.

First option is to use the WorkflowPersistenceService itself and do something like:

var persistenceService = new SqlWorkflowPersistenceService("<<connection string>>");
var persistedWorkflows = persistenceService.GetAllWorkflows();
foreach (var persistedWorkflow in persistedWorkflows)
{
    var workflowInstance = workflowRuntime.GetWorkflow(persistedWorkflow.WorkflowInstanceId);
    var workflowDefinition = workflowInstance.GetWorkflowDefinition();
    Console.WriteLine(workflowDefinition.GetType().FullName);
}

Easy to do as you are already using the SqlWorkflowPersistenceService but it has the drawback of having to load all workflow instances into memory and you are responsible for removing them from memory.

The second option is to use workflow tracing:

    var trackingQuery = new SqlTrackingQuery("<<connection string>>");
    var queryOptions = new SqlTrackingQueryOptions()
    {
        WorkflowStatus = WorkflowStatus.Running
    };
    var runningWorkflows = trackingQuery.GetWorkflows(queryOptions);
    foreach (var runningWorkflow in runningWorkflows)
    {
        Console.WriteLine(runningWorkflow.WorkflowType);
    }

The advantage is you don't need to load the actual workflow definition into memory just to check it's type. The drawback is you have to add the SqlTrackingService as well with it's own database adding complexity and overhead.

Maurice
Thanks, Maurice. I had basically implemented the first option. The second one using tracking is more what I am after though I think. I think we will probably end up using tracking anyway, in which case I'll implement that option. Thanks again!
Ben Rice