SendActivity in Windows Workflow 3.5

.NET 3.5 might come with some new items for Windows Workflow. In the March 2007 CTP two new Activities make their appearance: SendActivity and ReceiveActivity. This blog post is about the SendActivity.



The SendActivity is used to send and receive a message to a WCF endpoint. It’s usage is fairly straightforward. First, let’s take a look at the service contract:



using System;


using System.Runtime.Serialization;


using System.ServiceModel;


 


namespace HitchHikerServiceLibrary


{


  [ServiceContract]


  public interface IKnow


  {


    [OperationContract]


    int GetAnswerToEverything();


  }


}


and its implementation:



using System;


 


namespace HitchHikerServiceLibrary


{


    public class ZaphodService : IKnow


    {


        #region IKnow Members


 


        public int GetAnswerToEverything()


        {


            return 42;


        }


 


        #endregion


    }


}


That’s the service that will be called by the Send Activity. It is in a WCF Service Libary project called HitchHikerServiceLibrary, which can be hosted by the WCF Service Host. See here.


If you have a working version of Visual Studio Orcas (see this post), you are able to open a design surface for a workflow. Create a Sequential Workflow Application project. I’ve renamed the workflow to BablefishWorkflow and added a SendActivity and a CodeActivity.



Select the SendActivity and take a look at the Properties window.



The two handlers for AfterResponse and BeforeSend can be used to prepare and round-up the exchange of messages. The UI goodness is in the ServiceOperationInfo property designer. Click on the ellipsis (…) and watch the new dialog.



Clicking on Import will bring up the dialog to select the service contract type (be it interface or service) from the current project or a referenced assembly.



You can only import existing types and are not able to specify your own contract. The dialogs for ReceiveActivity are similar, but do allow the adding of contracts.


The workflow application needs to define the client side WCF configuration, that should contain the same information that a normal WCF client would have for the ZaphodService. You can simply use Add Service Reference to do the job. I named the single service endpoint EndOfUniversePoint. You must specify one of the endpoint in SendActivity’s EndPoint property.


The Return value property of SendActivity is a Binding property and must be bound to a dependency property. Select Bind to new member from the dialog, that appears when you click the ellipsis of the property. It will automatically create a full dependency property. I called the property Answer.


Finally, to display the results of the WCF message exchange, simply dump the value of the Answer property in the ExecuteCode event handler of the Code activity, like so:



private void codeActivity1_ExecuteCode(object sender, EventArgs e)


{


    Console.WriteLine(this.Answer);


}


Set a breakpoint on the SendActivity (select it and press F9) and on the GetAnswerToEverything method. Set your solution for multiple startup projects and start the WCF service library and the workflow application project (in that order).


Watch the magic happen. Your service should be called and the result of 42 will be dumped in the console.


Wanna play? Source is available here.

Advertisements
This entry was posted in Uncategorized. Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s