0

Is it OK to use IUnityContainer as an extension in workflow activities (WF 4.0)?

protected override bool Execute(CodeActivityContext context)
{
    var container = context.GetExtension<IUnityContainer>();
    var db = container.Resolve<IDatabaseContext>();
    // ...
}

Please, give me an example how to do it right.

3 Answers 3

3

I'd refrain from having the container be placed in the extension list. Just get the extensions you really need, IDatabaseContext in your case. Less is usually better.

Regarding the best approach to including a dependency injection framework, one way would be to wrap a WorkflowApplication or WorkflowInvoker object in a utility class that supports Unity, and providing all the interfaces/types as extensions to the internal WorkflowApplication.

One other recommendation is register the required extensions in your activity's CacheMetadata method. This method is used in validating the workflow prior to its execution.

public sealed class SampleActivity : CodeActivity
{
    protected override void CacheMetadata(CodeActivityMetadata metadata)
    {
        // Require the IDatabaseContext extension to be defined in the workflow host
        // application. The workflow will fail validation if an extension with this
        // interface is not provided.
        metadata.RequireExtension<IDatabaseContext>();
    }

    protected override void Execute(CodeActivityContext context)
    {
        var dbContext = context.GetExtension<IDatabaseContext>();
    }
}

public interface IDatabaseContext
{
}
0

This thread provides code samples and links that explain how to achieve what you are looking for.

1
  • I looked into the thread you provided. But it doesn't look to me like an answer to my question. Oct 3, 2012 at 19:01
0
public sealed class SampleActivity : CodeActivity
{
    protected override void CacheMetadata(CodeActivityMetadata metadata)
    {
        // Require the IDatabaseContext extension to be defined in the workflow host
        // application. The workflow will fail validation if an extension with this
        // interface is not provided.
        metadata.RequireExtension<IDatabaseContext>();
    }

    protected override void Execute(CodeActivityContext context)
    {
        var dbContext = context.GetExtension<IDatabaseContext>();
    }
}

public interface IDatabaseContext
{
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.