Consuming WCF Service in Workflow 4.0 – 1

Keywords: .Net 4.0, Visual Studio 2010, Workflow, WCF, WF 4.0, Add Service Reference, ASR

WF4 provides powerful messaging activities to achieve close integration with WCF. However, manually configuring a set of messaging activities to consume a WCF service would be tedious.

This blog introduces how Visual Studio 2010 can help you consume a WCF service in WF4 very effectively.

If you are familiar with WCF, Visual Studio’s Add Service Reference feature (ASR) feature should be nothing new to you. In workflow 4.0 projects, you can still ASR!

I will describe the basic usage of this feature below.

1. Prepare a sample WCF Service


Open Visual Studio’s New Project dialog, create a sample service from WCF Service Application template. This application exposes Service1 with 2 operation contracts:

string GetData(int value);

CompositeType GetDataUsingDataContract(CompositeType composite);

Now build the project.

2. Add a Workflow Project to the solution


Add a Workflow Console Application project to the solution. Name it “MyTestWF”.

3. Add Service Reference

Right Click on MyTestWF project, and select “Add Service Reference…” The Add Service Reference Dialog will open. Now Click the “Discover” button in the dialog, the sample WCF service we created previously will be automatically found.


Click OK and wait for a few seconds, you will see the following message box.


Just click OK to dismiss it. You may want to check the “In the future, do not show this message” check box if you do not want to see this message again.

In this demo, I used “Discover” button to find the sample service in the same solution. If your WCF Service is already running, simply specify the metadata address in the Address text box and click “Go”.

4. Build workflow project, you should see a set of activities being generated in the toolbox


Visual Studio generates one activity for each operation. The activity name is the same as the operation name. Using these activities, you can call the service operations.

5. Consume the activities

Consuming WCF services with the generated activities is just Drag & Drop! Following demonstrate a simple scenario

      1) Drag a Sequence activity (from Toolbox Control Flow Category) to the workflow

      2) Drag the GetData activity to the Sequence activity. Observe the properties of the GetData activity


    Looking at the GetData operation contract,


     string GetData(int value);

     It is easy to understand that the “value” field is the input integer you can pass to the service and the “GetDataResult” field is where you can put an string    variable to store the returned results.

    3) To store the returned results, we add a string variable v1 to the sequence.

    Click the Sequence and then click the “Variables” button at the bottom of the designer, the variable designer will open. Add a string variable v1 there.


     4) Click the GetData activity, modify the “value” field and “GetDataResult” field as following


     v1 will store the result returned from the service.

    5) Now the workflow is ready to invoke the service.

    Before running the workflow, we may want to use a WriteLine activity to display the v1 value.


     6) Click the WriteLine activity, press F9 to set a breakpoint

     7) Set the Workflow Console Application project as the startup project, press F5 to start debugging

     8) Program will break at the WriteLine activity, press F10 to step over, open the Console window, you should see “You Entered: 5”

      That is all. You consumed a WCF service in WF 4.0!

In Visual Studio 2010, this feature supports three WF 4.0 project templates: Activity Library, WCF Workflow Service Application and Workflow Console Application. They are all under Workflow node. When doing Add Service Reference in other project types, we will generate normal client proxy code.

I will talk about advanced usage of the ASR generated activities, including usage of the “EndpointConfigurationName” field and correlation handling in upcoming blogs.

Comments (4)

  1. Justin says:

    Thanks for great article. It is so useful….Could you please explain something about workflow instance in this approach since I could not see any properties i can specify create instance for each request.

  2. Steve says:

    Any way to get around this?  I want to run against WCF services in a code activity that I create in my workflow project, but it doesn't create a client proxy any more.  The only way to call the operations on the service is by using the activities.  Anyway I can make Visual Studio just add the good ol' fashioned service reference with a client proxy?

  3. cherry says:

    British reporters are known for doing almost anything to get a Mulberry Bags. But reports that a newspaper secretly listened to telephone messages of murdered schoolgirls and other private citizens have produced Mulberry Handbags and anger.

    On Friday, British police arrested Andy Coulson, former editor of Mulberry Bag Britain's best-selling newspaper, News of Mulberry Outlet the World. The investigation led him to Mulberry UK Sale resign in January as communications director to Prime Minister David Cameron.

    The arrest came in a widening investigation of Mulberry UK telephone hacking. Other accusations include paying police for mulberry shoulder bags information on stories. The Reuters news agency reported that Mr. Coulson was released on Mulberry Bag UK until a date in October.

    Prime Minister Cameron promised Men's Mulberry Bags Friday that a judge will lead a full public inquiry into Women's Mulberry Bags the case after police complete their investigation.

    DAVID CAMERON: "Murder victims, terrorist victims, families who have lost loved ones, sometimes defending our country, that these people could have had their phones hacked into, in order to generate stories for Mulberry Bags  Mulberry Handbags  Mulberry Bag   Mulberry Outlet  Mulberry UK  mulberry bayswater bag  Mulberry Alexa Bag, is simply disgusting."

  4. SwastikMS says:

    Thanks for posting such a nice and easy way to consume WCF service in Workflows!!! I know it's very late but can you tell me where can I find your other blogs specifically on workflows? 🙂

Skip to main content