Walkthrough: Implementing a Service by Using a Specific Workflow
This walkthrough creates a specific workflow implementation that accepts a city name in the Request message, simulates retrieving that city's temperature, and then returns an integer representing the temperature in the city in the Response message.
The walkthrough creates a BLSpecific service project named WeatherInformationService in a DCS Solution project named WeatherInformation. The walkthrough also creates messages in a messages project named WeatherInformationMessages.
Before you begin
- Create a DCS Solution project in Visual Studio. For detailed instructions, see Walkthrough: Creating a DCS Solution.
- Create the messages WeatherInformationMessages project in the DCS Solution project. For detailed instructions, see Walkthrough: Creating a DCS Messages Project.
Detailed Instructions
You must customize the default Request and Response classes to contain the data required by the workflow.
To customize the GetWeatherRequest and GetWeatherResponse classes
In Solution Explorer, under WeatherInformationMessages, right-click GetWeatherRequest.cs, and then click View Code.
In the GetWeatherRequest.cs file, expand the Private Fields region, and then add a line of code to create a private string field named _CityName.
Your code should resemble the following example.
-
#region Private Fields private string _CityName; #endregion
Expand the Public Properties region, and add code to create a public property to expose the _CityName field. Name the property CityName, and decorate it with the DataMember attribute. This enables the DCS runtime to serialize and deserialize message properties.
Your code should resemble the following example.
-
#region Public Properties [DataMember] public string CityName { get { return _CityName; } set { _CityName = value; } } #endregion
On the File menu, click Save GetWeatherRequest.cs.
On the File menu, click Close.
In Solution Explorer, under WeatherInformationMessages, right-click GetWeatherResponse.cs, and then click View Code.
In the GetWeatherResponse.cs file, expand the Private Fields region, and then add a line of code to create a private integer field named _Temperature.
Your code should resemble the following example.
-
#region Private Fields private int _Temperature; #endregion
Expand the Public Properties region, and add code to create a public property to expose the private integer field. Name the property Temperature, and decorate it with the DataMember attribute.
Your code should resemble the following example.
-
#region Public Properties [DataMember] public int Temperature { get { return _Temperature; } set { _Temperature = value; } } #endregion
On the File menu, click SaveGetWeatherResponse.cs.
On the File menu, click Close.
On the Build menu, click BuildWeatherInformationMessages, and then verify that there are no errors.
After you create the Message classes, you can create the service.
To create a BLSpecific Service project
In Solution Explorer, right-click the BLSpecific folder, point to Add, and then click BL Specific.
In the Add New Project dialog box, in the Templates list, click BL Specific.
In the Name box, type WeatherInformationBLSpecific, and then click Browse.
In the ProjectLocation dialog box, browse to E:\Walkthroughs\WeatherInformation\BLSpecific, and then click OK.
In the Add New Project dialog box, click OK.
The following image shows the Add New Project dialog box.
The Add New Project dialog box, configured to create a new BL Specific project named WeatherInformationBLSpecific, in the E:\Walkthroughs\WeatherInformation\BLSpecific folder
In the Create New BL SpecificProject dialog box, in the Namespace box, type WeatherInformationNamespace
In the Service Name box, type WeatherInformationService, and then click Finish.
The following image shows the Create New BL SpecificProject dialog box.
The Create New BL Specific Project dialog box, configured to create the new project in the WeatherInformationNamespace, and create a service instance named WeatherInformationService
After you create the BLSpecific project, you can implement the operation by using one of the Add Operation recipes.
To create a specific workflow operation
In Solution Explorer, right-click WeatherInformationBLSpecific, and then click Add New Operation Request/Response.
In the Add New Operation Request/Response dialog box on the Operation Configuration page, in the Operation Name box, type GetTemperature, and then click Next.
The following image shows the Operation Configuration page.
The Add New Operation Request/Response dialog box, with the OperationConfiguration page configured to create a new operation named GetTemperature
On the Messages Selector page, click the ellipsis button (…) adjacent to the Select type of Request message field.
In the Please choose a file dialog box, browse to the E:\Walkthroughs\WeatherInformation\Messages\WeatherInformationMessages\bin\Debug folder, click WeatherInformationMessages.dll, and then click Open.
In the Add New Operation Request/Response dialog box on the Messages Selector page, configure the operation messages as follows, and then click Next:
-
- Select type of Request message: GetWeatherRequest
- Select type of Response message: GetWeatherResponse
- Select type of Error: WeatherInformationError
- Select type of Exception: WeatherInformationException
The following image shows the Messages Selector page.
The Message Selector page of the Add New Operation Request/Response dialog box, configured to set the Request message to GetWeatherRequest, and the Response message to GetWeatherResponse
On the Messages Assemblies References page, in the Assemblies References list, verify that the WeatherInformationMessages(Project) item is selected, and then click Finish.
The following image shows the Messages Assemblies References page.
The Messages Assemblies References page of the Add New Operation Request/Response dialog box
When you complete the Add Operation recipe, Visual Studio generates the template operation code in a new folder inside the BLSpecific project. You can customize the workflow to implement the required functionality.
To implement workflow functionality in a specific workflow
In Solution Explorer, under WeatherInformationBLSpecific, in the GetTemperature folder, right-click the GetTemperatureWorkflow.cs file, and then click View Designer.
In the workflow designer, drag a Code activity from the toolbox onto the designer surface, and drop it at the top of the workflow sequence.
Change the activity (Name) property to retrieveCityTemperature, and then double-click the activity to create an event handler.
Visual Studio opens the GetTemperatureWorkflow.cs code-behind file, and creates a new event handler method in the file.
Inside the RetrieveCityTemperature _ExecuteCode method, add code to verify that the cityName property of the Request message contains a city name, and if so, set the Temperature property of the Response message to 23. This code simulates retrieving the real city temperature from a data source such as another Web service or database. If the cityName property is null or empty, throw a WeatherException.
Your code should resemble the following example.
-
private void retrieveCityTemperature_ExecuteCode(object sender, EventArgs e) { if (!string.IsNullOrEmpty(this.Request.CityName)) { this.Response.Temperature = 23; } else { WeatherInformation.WeatherInformationMessages.WeatherInformationException exception = new WeatherInformation.WeatherInformationMessages.WeatherInformationException ("CityName property cannot be empty"); WeatherInformation.WeatherInformationMessages.WeatherInformationError error = new WeatherInformation.WeatherInformationMessages.WeatherInformationError (exception); throw new System.ServiceModel.FaultException< WeatherInformation.WeatherInformationMessages.WeatherInformationError>(error); } }
-
Note
This code makes use of a customized exception class, WeatherInformation.WeatherInformationMessages.WeatherInformationException, and a custom error class, WeatherInformation.WeatherInformationMessages.WeatherInformationError, to pass custom exception data to the client application. For more information on defining custom errors, see Walkthrough: Creating a DCS Messages Project. For information on catching custom exceptions, see Invoking a DCS Service from a Client Application.
Switch back to the workflow designer
From the toolbox, in the DCS group, drag a SendResponse activity onto the designer surface, and drop it below the retrieveCityTemperature activity.
Change the activity (Name) property to sendResponse.
Bind the activity Response property to the workflow Response property:
-
- Click the Response property, and then click the ellipsis button (…) that appears.
- In the Bind 'Response' to an activity's property dialog box, on the Bindto an existing member tab, click Response, and then click OK.
Your workflow should resemble the following image.
A BLSpecific Request/Response sequential workflow implementation containing a single CodeActivity activity and a SendResponseActivity activity.
On the Build menu, click BuildWeatherInformationBLSpecific .
You can deploy the project and create a client application to invoke the operation functionality. For more information, see Walkthrough: Deploying DCS Services by Using Visual Studio. For more information about creating client applications to invoke DCS Services, see Building Client Applications.
See Also
Walkthrough: Creating a DCS Solution
Walkthrough: Creating a DCS Messages Project
Walkthrough: Deploying DCS Services by Using Visual Studio