Posted by: Cirilo Meggiolaro | 03/27/2009

Tip of the day #164 – Invoking a web service from a workflow (WWF)

Tip #147 demonstrated how to expose a Windows Workflow Foundation (WWF) as a web service. Let’s check today how to create a WWF workflow that consumes a web service.

How to…

  • Open Microsoft Visual Studio;
  • Create a new Sequential Workflow Console Application project;
  • Add a new ASP.NET Web Service Application project to the solution. If you already have any web service published that you can use, you may skip this step;
  • It is not the objective of this tip to show how to create web services so If you have added the web service project to the solution, we are going to use the default HelloWorld method available there;
  • A workflow named Workflow1 is added to the project. If it’s not opened yet, just double-click the Workflow1.cs file on the Solution Explorer window;
  • Drag a InvokeWebService activity onto the Workflow1 surface;
  • The Add Web Reference window is opened automatically. If you have created the web service project click on “Web Services in this solution” link otherwise you may type the URL on the control available on the top of the screen and click Go or press Enter;
Picture 1 - Add Web Reference dialog

Picture 1 - Add Web Reference dialog

  • Click on the web method name as shown on the following picture;
Picture 2 - Add Web Reference dialog with the web method selected

Picture 2 - Add Web Reference dialog with the web method selected

  • The web method details are going to be displayed. Enter a text on the Web reference name textbox and click Add Reference;
  • A reference will be added under the Web References folder. Your Solution Explorer window must be similar to the picture below;
Picture 3 - The Solution Explorer window

Picture 3 - The Solution Explorer window

  • Since you have a reference added to your project it’s time to hook up some properties. Select the InvokeWebService activity on the workflow designer and on the Properties window, select the HelloWorld method from the dropdown list available beside the Method Name property;
  • Even though we have added the method name we still have a warning icon on the activity. It happens because we haven’t defined how to store the web service’s return value;
Picture 4 - The InvokeWebService activity still displays a warning

Picture 4 - The InvokeWebService activity still displays a warning

  • Press F7 or right-click and select View Code. Create a string property called WebServiceReturn just after the workflow type declaration;

public string WebServiceReturn { get; set; }

  • Back to the workflow, select the browse button (the one with the three dots) beside the ReturnValue property;
  • From the Bind to an activity property window, select WebServiceReturn and click OK;
Picture 5 - Bind to a new property dialog

Picture 5 - Bind to a new property dialog

Congratulations! You have built in a few steps a workflow that consumes a web service and stores the return value into a property. You have two options now: You may either add new activities after the InvokeWebService activity or invoke a method by defining an event handler for the Invoked property on the Properties window. For this example, let’s create a code activity that will write to the console window the return value of our web service;

  • Drag a code activity onto the workflow surface;
  • On the Properties window, type DisplayReturnName for the ExecuteCode property and press Enter. This will create an event handler with the name you typed and will be invoked by the runtime after the previous activity has been executed;
  • Add a Console.WriteLine call to write the value of the WebServiceReturn property;

Console.WriteLine(WebServiceReturn);

  • Your final workflow designer and code must be similar to the following:
Picture 6 - The final workflow designer

Picture 6 - The final workflow designer

public sealed partial class Workflow1 : SequentialWorkflowActivity
{
    public string WebServiceReturn { get; set; }

    public Workflow1()
    {
        InitializeComponent();
    }

    private void DisplayReturnValue(object sender, EventArgs e)
    {
        Console.WriteLine(WebServiceReturn);
    }
}

  • Build and run the application. You will see that the classic message HelloWorld is displayed on the console.
Advertisements

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 )

Twitter picture

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

Facebook photo

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

Google+ photo

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

Connecting to %s

Categories

%d bloggers like this: