Share via


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

clip_image002

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

[OperationContract]
string GetData(int value);

[OperationContract]
CompositeType GetDataUsingDataContract(CompositeType composite);

Now build the project.

2. Add a Workflow Project to the solution

clip_image004

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.

clip_image005

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

clip_image006

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

clip_image008

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

    clip_image010

    Looking at the GetData operation contract,

    [OperationContract]

     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.

    clip_image012

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

     clip_image014

     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.

    clip_image016

     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

  • Anonymous
    November 14, 2010
    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.

  • Anonymous
    June 14, 2011
    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?

  • Anonymous
    October 16, 2011
    The comment has been removed

  • Anonymous
    July 10, 2015
    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? :)