Sunday, April 27, 2008

Getting a list of services loaded into the workflow runtime

Getting a list of the services loaded into the workflow runtime is pretty simple. Here's some code to do it.

using System;
using System.Collections.ObjectModel;
using System.Workflow.Runtime;

namespace GetAllServices
{
  class Program
  {
    static void Main(string[] args)
    {
      WorkflowRuntime runtime = new WorkflowRuntime();
      runtime.StartRuntime();
      ReadOnlyCollection<object> services = runtime.GetAllServices(typeof(object));
      for (int i = 0; i < services.Count; i++)
      {
        Console.WriteLine(services[i].ToString());
      }
      Console.ReadLine();
    }
  }
}

If you try to get a service from inside an activity using the ActivityExecutionContext.GetService method, the available services will be different. Looking in Reflector, you'll see there are several services that won't be returned and the IStartWorkflow service will be created on the fly. Also have a look at WorkflowExecutor.GetService called from ActivityExecutionContext.GetService which also special cases some services before finally calling the runtime's GetService.

No comments: