Now we have to configure Receive activity to accept following three parameters. To do this you will have to select the receive activity and change its "Content" property by clicking [dotdot.png] button. This will open up the "Content Definition" window.Click on the parameter radio button to define parameters. Message data option can be used to receive custom class objects.
[Image8.png]
Define three parameters (as shown in the figure above) by clicking on the "Add new parameters" text. "Parameter1" and "parameter2" will hold two numbers and "operationName" will hold the name of the operation to be performed on these numbers.
After adding parameters we have to assign workflow level variables to the parameters so that all the input data will be available for all workflow activities. To do this just select the "Enter a VB expression" text in the cell next to the parameter name and type in the variable that you want to assign to the parameter. In this exercise I will assign parameter1 to operand1, parameter2 to operand2 and operationName to OperationName workflow variable.
Image9.png]
Click OK to close the window.
To configure the send activity select the "SendResponse" activity in the designer. Change its "Content" property to configure what to send back to the caller. Select the [dotdot.png] button next to "Contents in the property box. This will open up a new "Content Definition" window same as what we have seen before during the receive activity configuration. We do not have to configure parameters here Message configuration will just do fine. Just remove "data.ToString()" with "Result" variable that we have declared in the beginning. On more great features of the designer is that it provides intelli-sense help throughout the process and that makes it easier to select and find variables available (see screen shot below)
[Image11.png]
Click OK to close the window and get back to the designer.
Now we are done with the web service input and output configurations. Next step is to perform some operation based on the "operationName" parameter. For that we are going to use a flow chart activity. Before we start build a flow chart let's build a custom code activity to log some trace information in a file and also add some text to the final result. We will use this activity in the flow chart.
1. Right click on the CalculateWFService project and select add new item.
2. The in the installed template section select "workflow"
3. Select the "Code Activity"
4. Click "Add" to add this activity to the project (we will leave the default name "codeactivity1.cs" as is)
[Image12.png]
5. Every code activity can have input and output arguments just like any C# methods has input and output parameters. I find it easier to think of it as a C# class method and the way it deals with the input and output parameters. The only exception is that in code activity they have to be declared as InArguments and OutArguments and have to be accessed via current context. In this code activity we are going to use the default text InArgument and will add an OutArgument "result". Just type the following in the top section of the class.
public OutArgument<string> result { get; set; }
6. Add following simple execution code in the Execute method of the code activity class.
File.AppendAllText(@"C:\WorkflowLog.txt", "Result is:" + text);
context.SetValue(result, "Result is:" + text);
All this code is doing is writing the input of this code activity in a file and returning the input of the activity after appending some text in it. Also, you will have to add the following in the file header:
using System.IO;
In code activities all the activity execution logic should be written in the Execute method. Please check the following link for more information on code activities and execute method:
"http://msdn.microsoft.com/en-us/library/ee264176.aspx".
7. Build the project at this point to check for any errors and to add this new code activity in the "Toolbox"
[Image13.png]
8. Select the "CalculatorService.xamlx" to go back to the designer
There are other options to get the result and this simple task can be accomplished by using one custom code activity or via simple expression in the send activity's configuration. But my goal is to give overview of all as much workflow features as possible in this exercise and this is why I am going to use a flow chart.
A Flowchart activity is an activity that contains a collection of activities to be executed. Flowcharts also contain flow control elements such as FlowDecision and FlowSwitch that direct execution between contained activities based on the values of variables.
Different types of elements are used depending on the type of flow control required when the element executes. Types of flowchart elements include:
Models one step of execution in the flowchart.
Branches execution based on a Boolean condition, similar to If.
Branches execution based on an exclusive switch, similar to C# Switch. Each link has an Action property that defines a ActivityAction that can be used to execute child activities, and one or more Next properties that define which element or elements to execute when the current element finishes execution. You can fine out more about flowcharts here.
Ok enough about flowcharts let's get started on building one to calculate the result.
Comments
There are no comments yet. Be the first to comment!