This is a mirror of official site: http://jasper-net.blogspot.com/

(WF4) Lesser Known WF Features: WorkflowDataContext

| Thursday, August 11, 2011
Sometimes it happens that via the forums I learn about a new [to me] beast in the WF Zoo. Today that animal is WorkflowDataContext. The MSDN document is terse:

    “Represents the data context of the current workflow environment and provides a bridge to bring workflow arguments and variables into the scope of data binding.”

Let's put that into everyday WF terms. How would you actually use one of these?
 
Application 1: Reading Variable and Argument Values from inside a C# custom Activity

It’s a simple workflow. Not shown in the picture above is an InArgument<string> called “argument1”. Also not shown above is that CodeActivity1 has an InArgument<string> called “Text” which is assigned expression “someText”.

CodeActivity1

Here’s a custom C# activity CodeActivity1 which will access the WorfklowDataContext.

public sealed class CodeActivity1 : CodeActivity
{
    public InArgument<string> Text { get; set; }
 
    protected override void Execute(CodeActivityContext context)
    {
        string s = Text.Get(context);
 
        WorkflowDataContext dc = context.DataContext;
        foreach (var p in dc.GetProperties())
        {
            Console.WriteLine("Property {0}: {1}", ((PropertyDescriptor)p).Name, ((PropertyDescriptor)p).GetValue(dc));
        }
    }
}

Notes:
-The DataContext property is defined on ActivityContext base class, so it's also available for writing NativeActivity and AsyncCodeActivity.
-The required parameter to PropertyDescriptor.GetValue is the WorkflowDataContext object. It acts as an explicit ‘this’ object for doing a property get.

Output

Here’s what we see if we run a simple console app which invokes the workflow and passes in a value for argument1.

Read more: The Activity Designer
QR: wf4-lesser-known-wf-features-workflowdatacontext.aspx

Posted via email from Jasper-net

0 comments: