Skip to content

How to use Workflow instance extension to inject dependencies into WF service

dimaKudr edited this page Jun 19, 2012 · 4 revisions

Ok, you already know how to use Workflow instance extension to inject dependencies into WF Activity. But what if you want to publish your workflow as a service? Let me show you how Unity.WF can help in this situation.

Firstly, please notice that for the activity it does not matter how you are going to execute it: as a standalone activity or as a workflow service. Activity will pull its dependencies from wokflow instance extension in exactly the same way in both situations. You can find sample activity here.

Secondly, you should somehow add DependencyInjectionExtension instance with properly configured Unity container to workflow runtime. To do that Unity.WF contains UnityServiceHostFactory class. You should just extend it with proper configuration of Unity container and dependency injection type.

public class SamplePullFactory : UnityServiceHostFactory
{
    protected override void ConfigureContainer(IUnityContainer container)
    {
        container.RegisterType<ICalcService, CalcService>();
    }

    protected override InjectionTypes ConfigureInjectionType()
    {
        return InjectionTypes.Pull;
    }
}

And lastly, you should modify web.config file to let runtime know how to instantiate your workflow service.

<serviceHostingEnvironment>
  <serviceActivations>
    <add factory="Unity.WF.SampleService.SamplePullFactory"
         relativeAddress="./Service1.xamlx"
         service="Service1.xamlx"/>
  </serviceActivations>
</serviceHostingEnvironment>

That's it.