views:

91

answers:

1

I've got common code that can run in a number of execution enviroments - within IIS, within a WCF service, in a stand-alone application, or in a Windows Workflow instance.

But what is the best way to check whether the code is running inside a Workflow? For the moment, I've been looking for WorkflowEnvironment.WorkflowInstanceID and catching any exceptions with this code:

...
try
{
  if (WorkflowEnvironment.WorkflowInstanceId != null)
  {
    return ExecutionContext.Workflow;
  }
}
catch
{
}

// return unknown
return ExecutionContext.Unknown;

Isn't there a better way? I want to remove the need for a try...catch block.

A: 

There really is no better way of doing this.

One thing you can check first is System.Diagnostics.Trace.CorrelationManager.ActivityId. If this is equal to Guid.Empty then you are not inside of a workflow using the DefaultWorkflowSchedulerService. Unfortunately the manual scheduler doesn't set this ActivityId so in that case it doesn't help.

BTW This ActivityId hase nothing to do with workflow but is the E2E tracing activityId that is also set to the workflow InstanceId.

Maurice