Skip to content

How to use Unity container extension to inject dependencies into WF service

dimaKudr edited this page Jun 19, 2012 · 2 revisions

Ok, you already know how to use Unity container 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. Unity container will push all dependencies into activity's properties marked with [Dependency] attribute. So injection works in exactly the same way in both situations. You can find sample activity here.

Secondly, you should somehow add Unity container extension 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 SamplePushFactory : UnityServiceHostFactory
{
    protected override void ConfigureContainer(IUnityContainer container)
    {
        container.RegisterType<ICalcService, CalcService>();
    }

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

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.SamplePushFactory"
         relativeAddress="./Service2.xamlx"
         service="Service2.xamlx"/>
  </serviceActivations>
</serviceHostingEnvironment>

That's it.