MEF with MS Workflow

I’ve been a strong advocator of MEF for some time now, and with some of my work is now based around MS Workflow I thought it would be a good time to get MEF working with MS Workflow.

Before we start we need to know a few things:

  1. MS Workflow does not have a normals constructor
  2. When will we import our objects

Two simple tasks for any normal project, but MS Workflow is different, as we all know.

So to start with to deal with te fact that MS Workflow does not have any constructors we can use Extensions, as these are fired up when MS Workflow is started up.  To do this it is simpley a task to add the extension in to the web.config file under <extensions> tag

    <extensions>
      <behaviorExtensions>
        <add name="mefProviderExtension" type="MEFExtension.WorkflowServiceBehaviorElement, MEFExtension"/>
      </behaviorExtensions>
    </extensions>

The code by default picks up the Exported MEF objects from the BIN folder and a bin/PlugIns folder, however you can have a test in bin/tests folder, to do this you can add to behavioura mefProviderExtension

<mefProviderExtension  useTestServices="true"/>

Then with every CodeActivity use the MEF method of SatisfyImportOnce to inject the MEF objects.

context.GetExtension<MefWorkflowProvider>().Container.SatisfyImportsOnce(this);

I have taken the code written by Gareth Berlow from CodePlex called MEF and Workflow Services Integration and adapted the code to make it easier to use, simply but moving all the MEF code in to a MEFExtension but still allowing the ability to generate your own overrides of the MEF object if required.

MSWorkflow _MEF.zip (66.04 kb)