Workflow Foundation 4.0 - Extension

Ambily.raj
Posted by in WWF category on for Intermediate level | Points: 250 | Views : 20772 red flag
Rating: 4.5 out of 5  
 2 vote(s)

Workflow Foundation 4.0 is introduced a significant amount of change from the previous versions of the technology. In WF4.0, we are using Extensions for communication between the host and workflow, the workflow and its child activities and between child activities. There are two types of extensions in WF4.0, in this article we will look into the normal extensions.

Extension

Extension is a normal class with public methods and properties. Once we add the extension class to the workflow application, we can use the same in any child activities. For each instance of the workflow application will have one instance of its extension class. Using the context associated with the custom activity, we can access the extensions.

Sample Extension

For our sample, I am using a simple extension with one property and a Method. The dictionary property is used to pass data from host application to child activity. Same is used for communicating to another child activity.

public class SimpleExtension

    {

        private Dictionary<string, object> data;

        public Dictionary<string, object> Data

        {

            get

            {

                if (data == null)

                {

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

                }

                return data;

            }           

        }

 

        public void AddData(string key, object value)

        {

            if (string.IsNullOrEmpty(key))

                return;

            Data.Add(key, value);

        }

    }

Custom Activity to Access the Extension

Here, we are creating a custom activity, which will access the extension and pass values to the extension property.

Our custom activity will have an InArgument called Message. Custom activity will access the extension using the Context object. In the following code, we are extracting the data passed by the host application and combining the same with the Message of the custom activity and updating the same back to the extension. 

public class CustomActivity:NativeActivity

    {

        public InArgument<string> Message

        {

            get;

            set;

        }

        protected override void Execute(NativeActivityContext context)

        {

            SimpleExtension extensionObj = context.GetExtension<SimpleExtension>();

            if (extensionObj != null)

            {

                // extract the data added from host application

 

                object data=null;

                if (extensionObj.Data.ContainsKey("Host"))

                {

                    data = extensionObj.Data["Host"];

                }

               

                //Pass data to the extension

                extensionObj.AddData(this.DisplayName, Message.Get(context)+data);

            }

        }

    }

Define the Workflow

Now we are creating a new workflow using the custom activity. Our sample workflow will look like



Attaching to Workflow Application

After creating the workflow application object, add the extension object to the workflow application’s Extensions collection. The code used by the Host application is

static void Main(string[] args)

        {

            // Create workflowapplication object

            WorkflowApplication wfApp = new WorkflowApplication(new SampleWorkflow());

 

            //Create the extension object

            SimpleExtension extensionObj = new SimpleExtension();

            extensionObj.AddData("Host", " Host Application Data ");

 

            //Attach the extension to the workflow application

            wfApp.Extensions.Add(extensionObj);

 

            //When the workflow complete, display the data from extension

            wfApp.Completed=delegate(WorkflowApplicationCompletedEventArgs a)

            {

                foreach (string key in extensionObj.Data.Keys)

                {

                    Console.WriteLine("{0}   :  {1}", key, extensionObj.Data[key].ToString());

                }

            };

 

            // Start the workflow

            wfApp.Run();

 

            Console.Read();

        }

Result

From the result screen, you can observe the messages from both the custom activities and host application. Custom activity message is appended by the host application message.


Adding Extensions from Custom Activity

Extensions can be added using the CacheMetadata of the custom activity. In the following example, we add a second extension to our workflow using the custom activity. 

protected override void CacheMetadata(NativeActivityMetadata metadata)

        {

            base.CacheMetadata(metadata);           

            metadata.AddDefaultExtensionProvider(() => new SecondExtension());

        }

Conclusion      

Here, we discussed about the Extension used in custom activity development in Windows Workflow Foundation 4.0. Extensions are used for communication between the host and workflow, communication between the workflow and its child activities and communication between child activities.

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: Kweeks on: 12/18/2011 | Points: 25
Hi Ambily,

Thanks for the article... it was very helpful to me in trying to create a custom WorkflowServiceHost in order to add my singleton to the workflow host...

However, I didn't get the same results as you've mentioned above. I was able to access the instance of my singleton in the workflow but I couldn't access the singleton in the workflow extension as you show above. Each time the extension is null in my custom activity. However, just trying to instantiate the singleton provides me a functionality I was looking for.

I just want to understand why I cannot access the singleton via the WorkflowExtension collection...

Thanks!

Login to post response

Comment using Facebook(Author doesn't get notification)