Creating complex activities in the workflow designer

Note: This blog post is written using the .NET framework 4.0 Beta 2

In Windows Workflow Foundation 4 there are  a number of “activities” in the designer that aren’t really activities but activity templates. The most obvious of there are the ReceiveAndSendReply and the SendAndReceiveReply in the Messaging section of the toolbox.

If you search for either of these activities you are not going to find them. Their real name is ReceiveAndSendReplyFactory and SendAndReceiveReplyFactory respectively and they aren’t activities but activity templates.

 

So how to create an activity template?

Quite simply create a class and implement the IActivityTemplateFactory interface. This interface is simple and consists of a single Create function that returns the activity to be added to the workflow. Need to create multiple activities like the messaging activities do? Just wrap them in a Sequence activity and return that.

So what does that look like? Well in the case of the ReceiveAndSendReplyFactory the Create() looks like this according to Reflector:

public Activity Create(DependencyObject target)


{


    string str = ActivityDesignerHelper.GenerateUniqueVariableNameForContext(target, "__handle");


    Variable<CorrelationHandle> variable = new Variable<CorrelationHandle>


    {


        Name = str


    };


    VisualBasicSettings settings = new VisualBasicSettings


    {


        ImportReferences = 


        { 


            new VisualBasicImportReference 


            { 


                Assembly = requiredAssemblyName, 


                Import = requiredNamespace 


            } 


        }


    };


    Receive receive = new Receive


    {


        OperationName = "Operation1",


        ServiceContractName = XName.Get("IService", "http://tempuri.org/"),


        CorrelationInitializers = 


        { 


            new RequestReplyCorrelationInitializer 


            { 


                CorrelationHandle = new VisualBasicValue<CorrelationHandle> 


                { 


                    ExpressionText = str, 


                    Settings = settings 


                } 


            } 


        }


    };


    return new Sequence


    {


        Variables = { variable },


        Activities = 


        { 


            receive, 


            new SendReply                 


            { 


                DisplayName = "SendReplyToReceive", 


                Request = receive 


            } 


        }


    };


}



 



Nice to know what is happening behind the scenes and IActivityTemplateFactory is a nice interface to have when you start creating your own complex activities.



Enjoy!



[f1]
[f2]

Leave a Reply

Your email address will not be published. Required fields are marked *


*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>