Workflow Foundation 4.0 - WorkflowDataContext

Ambily.raj
Posted by in WWF category on for Beginner level | Points: 150 | Views : 14850 red flag

Workflow Foundation 4.0 is introduced a significant amount of change from the previous versions of the technology. This article will be the third in a series of articles. In this article we will be discussing about how to pass data from host application to workflow without using bookmarks.


Part I : Introduction to Custom Activity Development
Part II : Bookmark
Part III : WorkflowDataContext 


WorkflowDataContext

As specified in Part II, we can pass data from host application to custom activities using Bookmarks. Bookmarks are event driven mechanism. Whenever we define a bookmark, workflow will go for idle state and wait for the resumption of the bookmark.

Here, we will discuss about another way of passing common data to all child activities defined in a workflow. If you want to pass some common data to all the activities defined in the workflow, we can use the WorkflowDataContext associated with the workflow.  

Custom Activity

In Custom Activity, we can get the workflow level data using WorkflowDataContext. Properties associated with the WorkflowDataContext represent the workflow level Arguments and variables.

In the following example, we extracted the properties from WorkflowDataContext and assigned to our OutArgument called DataFromHost.

public class CustomActivity:NativeActivity

    {

 

        protected override bool CanInduceIdle

        {

            get

            {

                return true;

            }

        }

 

        public OutArgument<string> DataFromHost

        {

            get;

            set;

        }

 

        protected override void Execute(NativeActivityContext context)

        {

            string dataFromHost = string.Empty;

            WorkflowDataContext dataContext = context.DataContext;

            PropertyDescriptorCollection propertyDescriptorCollection = dataContext.GetProperties();

            foreach (PropertyDescriptor propertyDesc in propertyDescriptorCollection)

            {

                dataFromHost += propertyDesc.Name + " :  " + propertyDesc.GetValue(dataContext) + "\n";

            }

 

            DataFromHost.Set(context, dataFromHost);

        }      

    }


WorkFlow

Sample workflow using our CustomActivity is defined as



 
Host Application

Define a dictionary for passing data from host to workflow. Add key value pairs to the dictionary. The keys should be same as the InArguments defined in the workflow. In our case, it will be data1 and data2. 

Dictionary<string, object> Data = new Dictionary<string, object>();

            Data.Add("data1", "Hello from Host.");

            Data.Add("data2", "Color.Blue");

 

            WorkflowApplication workflowApp =

                       new WorkflowApplication(new TestWorkflow(), Data);                      

            workflowApp.Run();

           

            Console.ReadLine();


Dictionary of data is specified along with the workflow object for creating the WorkflowApplication. Dictionary values are added to the WorkflowDataContext and this can be accessed by any child Activity using the context.DataContext.


Result

You may be noted that, even though we passed only two values, data1 and data2, it displaying a third key called dataFromHost. As I specified, WorkflowDataContext contains all Arguments and Variables defined in workflow level. dataFromHost is the variable defined in the Workflow for handling output from our CustomActivity.

Conclusion

Here, we discussed about another way of passing data from host application to workflow. This method is used for passing common value to all child activities defined in the workflow.

 

Page copy protected against web site content infringement by Copyscape

About the Author

Ambily.raj
Full Name: Ambily KK
Member Level: Silver
Member Status: Member,Microsoft_MVP,MVP
Member Since: 5/18/2010 1:05:25 AM
Country: India
Thanks Ambily K K http://ambilykk.com/
http://ambilykk.com/
I have over 9 years of experience working on Microsoft Technologies. I am carrying the passion on Microsoft technologies specifically on web technologies such as ASP .Net and Ajax. My interests also include Office Open XML, Azure, Visual Studio 2010. Technology adoption and learning is my key strength and technology sharing is my passion.

Login to vote for this post.

Comments or Responses

Posted by: Sponnuru on: 5/29/2012 | Points: 25
Hi,

I am new in writing test cases for the workflow, we have some codeactivitycontext, which will run under build definition workflows,
could you please help or guide me in mocking the codeactivitycontext and workflowdatacontext object examples.

your help is appreciated.

Thanks,
Subbu

Login to post response

Comment using Facebook(Author doesn't get notification)