Share via


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

  1. Create a DCS Solution project in Visual Studio. For detailed instructions, see Walkthrough: Creating a DCS Solution.
  2. 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

  1. In Solution Explorer, under WeatherInformationMessages, right-click GetWeatherRequest.cs, and then click View Code.

  2. 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.

  3. Your code should resemble the following example.

  4. #region Private Fields
    private string _CityName;
    #endregion
    
  5. 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.

  6. Your code should resemble the following example.

  7. #region Public Properties
    [DataMember]
    public string CityName
    {
        get { return _CityName; }
        set { _CityName = value; }
    }
    #endregion
    
  8. On the File menu, click Save GetWeatherRequest.cs.

  9. On the File menu, click Close.

  10. In Solution Explorer, under WeatherInformationMessages, right-click GetWeatherResponse.cs, and then click View Code.

  11. 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.

  12. Your code should resemble the following example.

  13. #region Private Fields
    private int _Temperature;
    #endregion
    
  14. 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.

  15. Your code should resemble the following example.

  16. #region Public Properties
    [DataMember]
    public int Temperature
    {
        get { return _Temperature; }
        set { _Temperature = value; }
    }
    #endregion
    
  17. On the File menu, click SaveGetWeatherResponse.cs.

  18. On the File menu, click Close.

  19. 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

  1. In Solution Explorer, right-click the BLSpecific folder, point to Add, and then click BL Specific.

  2. In the Add New Project dialog box, in the Templates list, click BL Specific.

  3. In the Name box, type WeatherInformationBLSpecific, and then click Browse.

  4. In the ProjectLocation dialog box, browse to E:\Walkthroughs\WeatherInformation\BLSpecific, and then click OK.

  5. In the Add New Project dialog box, click OK.

  6. The following image shows the Add New Project dialog box.

  7. Dd632186.6e578eaa-4806-48fe-9c2f-5536d9bf99f0(en-us,MSDN.10).png

  8. The Add New Project dialog box, configured to create a new BL Specific project named WeatherInformationBLSpecific, in the E:\Walkthroughs\WeatherInformation\BLSpecific folder

  9. In the Create New BL SpecificProject dialog box, in the Namespace box, type WeatherInformationNamespace

  10. In the Service Name box, type WeatherInformationService, and then click Finish.

  11. The following image shows the Create New BL SpecificProject dialog box.

  12. Dd632186.cc4391bb-fc54-44c0-98fa-c2d6627244de(en-us,MSDN.10).png

  13. 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

  1. In Solution Explorer, right-click WeatherInformationBLSpecific, and then click Add New Operation Request/Response.

  2. 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.

  3. The following image shows the Operation Configuration page.

  4. Dd632186.ff22fa5d-830a-4292-b57e-3dc9fe947d27(en-us,MSDN.10).png

  5. The Add New Operation Request/Response dialog box, with the OperationConfiguration page configured to create a new operation named GetTemperature

  6. On the Messages Selector page, click the ellipsis button (…) adjacent to the Select type of Request message field.

  7. 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.

  8. 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
  9. The following image shows the Messages Selector page.

  10. Dd632186.52c0a67c-ae14-41a5-a6b5-de1f568e397b(en-us,MSDN.10).png

  11. 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

  12. On the Messages Assemblies References page, in the Assemblies References list, verify that the WeatherInformationMessages(Project) item is selected, and then click Finish.

  13. The following image shows the Messages Assemblies References page.

  14. Dd632186.1edc0cd0-8502-4858-b3d9-b711a79998ef(en-us,MSDN.10).png

  15. 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

  1. In Solution Explorer, under WeatherInformationBLSpecific, in the GetTemperature folder, right-click the GetTemperatureWorkflow.cs file, and then click View Designer.

  2. 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.

  3. Change the activity (Name) property to retrieveCityTemperature, and then double-click the activity to create an event handler.

  4. Visual Studio opens the GetTemperatureWorkflow.cs code-behind file, and creates a new event handler method in the file.

  5. 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.

  6. Your code should resemble the following example.

  7. 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);
        }
    } 
    
  8. 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.

  9. Switch back to the workflow designer

  10. From the toolbox, in the DCS group, drag a SendResponse activity onto the designer surface, and drop it below the retrieveCityTemperature activity.

  11. Change the activity (Name) property to sendResponse.

  12. Bind the activity Response property to the workflow Response property:

    1. Click the Response property, and then click the ellipsis button (…) that appears.
    2. In the Bind 'Response' to an activity's property dialog box, on the Bindto an existing member tab, click Response, and then click OK.
  13. Your workflow should resemble the following image.

  14. Dd632186.14b123ef-298d-441b-aeb0-973aa41f0295(en-us,MSDN.10).png

  15. A BLSpecific Request/Response sequential workflow implementation containing a single CodeActivity activity and a SendResponseActivity activity.

  16. 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

Invoking a DCS Service from a Client Application

Building Client Applications