简体   繁体   中英

Dependency injection in custom workflow activity

I have a standard application that has a workflow. The customer can customize the workflow in the designer. Now we are making some custom activities for a specific customer. The custom activity is communicating against the business layer by a interface. How do I give the interface an implementation of that interface?

It is very important that the standard application doesn't know about the interface and implementation of that interface because it is custom code for that specific customer. The activity is found by standard workflow so that works already.

I see a lot of information about extensions but I don't really know how it works.

Custom activity

public sealed class GetDealerDetails : CodeActivity
{
    /////// <summary>
    /////// The dealer controller with all the businesslogic.
    /////// </summary>
    ////private readonly IDealerController _dealerController;

    [Inject]
    public IDealerController DealerController { private get; set; }

    ////public GetDealerDetails()
    ////{

    ////}

    ////[Inject]
    ////public GetDealerDetails(IDealerController dealerController)
    ////{            
    ////    _dealerController = dealerController;
    ////}

    protected override void Execute(CodeActivityContext context)
    {
        Dealer dealer = DealerController.GetDealerDetails(5);
    }
}

I use Ninject in my standard application. I tried to use constructor injection and property injection but it doesn't work. The DealerController stays null .

Edit The rest of the code can be found here: Inject custom code in standard application

Import is that you have to use the workflow application wrapper I provide with ninject. Only with that I can build up the activities. The trick is the following: you cannot use constructor injection with custom workflow activities. Activities are very special in WF. Usually when you have coded workflow you build them up we the new operator in a lambda expression which is then lazy executed. So my ninject extension can only work its magic when the activities are already built up. Therefore you need to pass in the root activities of your activity tree in the workflow application of ninject. This does then internally resolve the whole activity tree und injects all properties which are decorated with the inject attribute.

But your actual problem was a bug in the library which I have fixed now. The BookmarkInfo decorator assumed that the scope info is always set and this wasn't the case.

Extensions are what the framework provides for injection in workflows. When you execute the workflow you add all dependencies that you will use in your activities.

[Dependency]
public IMyService MyService{ get; set; }

WorkflowApplication instance = new WorkflowApplication(myWorkflow, inParameters);
instance.Extensions.Add(MyService);
instance.Run();

And then you can get the extension in your activity in order to use it.

protected override void Execute(NativeActivityContext context){    
    var myservice = context.GetExtension<IMyService>();
    myservice.MyMethod();
    }

I hope it helps.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM