Share via


Exercise 1: Submit Application Service

In the first exercise you will create a simple service that receives an Application from the smart client application and returns a response.

Task 0 – Opening the Solution

  1. Start Microsoft Visual Studio 2010 from Start | All Programs | Microsoft Visual Studio 2010.
  2. Open the starting solution for Exercise 1 located under the Source\Ex1\Begin folder (choosing the folder that matches the language of your preference.) Use it as the starting point for this exercise.

    Note:
    What is in this solution?

    This solution contains a web site created from the WCF Workflow Service template, and a data layer project you will be using in this lab. The Web.config file has some minor modifications to include an appSettings section, and connection string for the database.

  3. Press CTRL+SHIFT+B to build the solution.

Task 1 – Adding a new a Workflow Service

  1. Add a new Workflow Service. To do this, right-click on HRApplicationServices, and select Add / New Item… (CTRL+SHIFT+A), in the Workflow Templates select the WCF Workflow Service template, name the service to SubmitApplication.xamlx, and then click Add.

    Figure 5

    Create a New Workflow Service in C#

    Figure 6

    Create a New Workflow Service in Visual Basic

Task 2 – Exploring the Workflow Service

You probably have some questions about your service. What is the service contract? What binding does it use? What address is it hosted at? In this task, you will explore your service and how it interacts with the world.

Figure 7

The default Workflow Service

  1. Workflow Services have an implicit contract that is defined by the messaging activities contained in the service. SubmitApplication.xamlx already has a working contract that might look familiar if you have built a WCF service before. Look at SubmitApplication.xamlx in the workflow designer. Notice that it has a sequence with two activities, a request labeled ReceiveRequest and a response labeled SendResponse. The request determines the parameters to the service. To see the message content, click View messagein the ReceiveRequest activity, next to the Content label.

    Figure 8

    The message content – the parameters are implied by the data

  2. Click OK to dismiss the Content Definition dialog.
  3. The content of the message is a variable named data. To see this variable; click the Sequential Service activity and then click the Variables button in the lower left of the design surface.

    Figure 9

    Variables created by the Workflow Services template

  4. There are two variables. The first variable handle is used to correlate the request and response messages. The second variable data defines the message content that you expect to receive.
  5. To see what the return value is, in the SendResponse activity click View message.

    Figure 10

    The response message will be the data converted to a string

  6. Click OK to dismiss the Content Definition dialog.
  7. You now have a workflow service, but it does not have Metadata publishing enabled. To add this, you can use the new “tagless” syntax for WCF configuration in .NET 4. Open the Web.config file, and add the following section. Also notice that there is no <service> definition. The simplified configuration for WCF will supply a default binding (basicHttpBinding) address and contract as required.

    (Code Snippet – Introduction to WF Services Lab – WCF serviceMetatdata XML)

    XML

    <system.serviceModel> <behaviors> <serviceBehaviors> <behavior> <serviceMetadata httpGetEnabled="true"/> </behavior> </serviceBehaviors> </behaviors> </system.serviceModel>

  8. You now have a working Workflow Service. To see it in action, right-click the SubmitApplication.xamlx file, and select View in Browser (CTRL+SHIFT+W). You should see a familiar site for WCF developers.

    Figure 11

    The service running in the browser

Task 3 – Creating Service Implementation Outline

Your service has three phases to it. It will receive, screen and notify. Throughout the lab, you will be implementing these phases. To begin with, you will add placeholders for each phase.

  1. Switch back to SubmitApplication.xamlx in the workflow designer.
  2. Change the Display Name property of the outermost sequence from Sequential Service to Application Service. You can change it directly on the design surface or in the Properties window.

    Figure 12

    Change the name of the service to Application Service

  3. The first placeholder will require a transaction because you will be updating a database as well as persisting your workflow to the persistence database. Drag a TransactedReceiveScope from the Messaging group in the Toolbox and drop it at the top of the Application Service sequence. Set the Display Name to Receive Application.
  4. Drag the existing ReceiveRequest inside the Request area of the Receive Application activity. When the message is received it will create a transaction.

    Figure 13

    Drag the existing ReceiveRequest inside of the Request area of the Receive Application

  5. In the body area you want to put the work that will be done inside of the transaction. You will have more than one activity here so drop a Sequence from the Control Flow Toolbox group inside of the body and set the properties.
    1. Display Name: Save and Respond
  6. Drag the SendResponse and drop it inside of the Save and Respond sequence.

    Figure 14

    The Service with the TransactedReceiveScope containing the messaging activities

  7. Collapse the Receive Application activity ().
  8. The second phase of the service will be to screen the applicant. You will implement this later but for now drag a Sequence activity from the Control Flow group and drop it below the Receive Application shape and set the Display Name to Screen Applicant then collapse the sequence.
  9. The third phase will be notifying the applicant via email and by sending a notification message to the smart client application. Drop another Sequence, set the Display Name to Notify Applicant and collapse the sequence.

    Figure 15

    Collapsed view of the workflow service with three phases

Task 4 – Adding Custom Activities

Your service will accept a job application from the smart client app. In this task, you will make the modifications necessary to prepare the service to accept a job application.

  1. Add existing projects containing the solution's data contracts and custom activities. To do this, in Solution Explorer right-click the solution node, and select Add / Existing Project. In the Add Existing Project dialog, browse to Source\Assets folder of this lab (choosing the folder that matches the language of your preference) and select the following project(s):
    1. HRApplicationServices.Contracts
    2. HRApplicationServices.Activities
  2. In the HRApplicationServices project, add references to the HRApplicationServices.Contracts and HRApplicationService.Activities projects. To do this in Solution Explorer, right-click the HRApplicationServices project and select Add Reference. In the Add Reference dialog, switch to the Projects tab, and select the projects. Click OK to add the references.
  3. Your workflow is going to be long running. It will do some initial work and then wait for a human to respond. This means that you need to configure a persistence store. To add this, open the Web.config from the HRApplicationServices project and add the following settings.
    1. To the <connectionStrings> section add the following connection string which will use the WF4Persistence store you created in the setup for the lab.

      (Code Snippet – Introduction to WF Services Lab – WF4 Persistence XML)

      XML

      <connectionStrings>
      <add name="WF4Persistence" connectionString="Data Source=.\sqlexpress; Database=WF4Persistence; Integrated Security=True"/>

    2. To the <behavior> section add the sqlWorkflowInstanceStore behavior right after the serviceMetadata behavior.

      (Code Snippet – Introduction to WF Services Lab – WF4 sqlWorkflowInstanceStore XML)

      XML

      <serviceMetadata httpGetEnabled="true"/>
      <sqlWorkflowInstanceStore connectionStringName="WF4Persistence" />

      Note:
      When you deploy the website to AppFabric you can configure the persistence store within IIS manager. While working with the ASP.NET Development server you have to create the persistence stores and add the persistence behavior manually.

  4. To make the custom activities available to your service you must build the solution. From the menu select Build / Build Solution (CTRL+SHIFT+B). Ensure that the build succeeded by inspecting the Output window. If you look at SubmitApplication.xamlx in the workflow designer, you should now see a HRApplicationService.Activities group in the toolbox.

Task 5 – Receiving a Job Application

  1. Open SubmitApplication.xamlx and create variables to store the application request and response. To do this, follow these steps:
    1. Select the Application Service activity by clicking over the shape surface.
    2. Click the Variables button located in lower left side of the workflow designer. A panel that displays the available variables for the Application Service activity appears.
    3. Delete the data variable by selecting it and pressing the DELETE key.

      Note:
      When you delete the data variable the workflow designer will show warning icons indicating that the workflow is invalid until you update the activities that used the data variable.

    4. Click Create Variable.

      Figure 16

      Adding a new variable

    5. Type ApplicationRequest in the Name box.
    6. In the Variable type dropdown list select Browse for Types.
    7. In the Type Name box of the dialog box, type the first few characters of the type you are looking for such as SubmitJob. As you type matching names will show up in the list. Click on the SubmitJobApplicationRequest type to select it.

      Figure 17

      Selecting the SubmitJobApplicationRequest type from the type picker box

    8. Click OK to finish.

      Figure 18

      Variables window

  2. Repeat the previous process to add another variable named ApplicationResponse of type SubmitJobApplicationResponse.
  3. You need to create a response object to store the response. Set the Default value of the ApplicationResponse variable to New SubmitJobApplicationResponse(). Remember that expressions must be written with Visual Basic regardless of the language of the project.
  4. Now you can change the contract to receive a job application. To do this, expand the Receive Application sequence and select the ReceiveRequest activity in the designer and press F4 to display the Properties Window. Set the following values:
    1. OperationName: SubmitJobApplication
    2. ServiceContractName: {https://contoso.com/hr/}IApplicationService
    3. Content: Click the … button to display the content definition dialog and change the Message data to ApplicationRequest.
    4. Check the CanCreateInstance checkbox. This will cause every incoming message received by the Receive Job Application activity to create a new workflow instance.

      Figure 19

      Properties of the ReceiveRequest activity

Task 6 – Saving the Job Application

You need to store the job application data in the HR database. The provided SaveJobApplication custom activity does this using the ADO.NET Entity Framework. When the job application is saved the database will generate an Application ID that you will use as the primary key for the application.

  1. Drop a SaveJobApplication activity from the HRApplicationServices.Activities group inside the Save and Respond sequence above the SendResponse activity.

    Figure 20

    The SaveJobApplication activity

  2. Set the properties of the SaveJobApplication activity:
    1. AppRequest: ApplicationRequest
    2. Result: ApplicationResponse

Task 7 – Configuring the Response

Though you have stored the application in the database, you are not finished processing it. You want to let the user of the client application know that you have received and are processing their job application. And you have to initialize the correlation handle that you will use to identify messages bound for this workflow instance using content based correlation.

  1. First, you need to create a correlation handle that will be used to correlate messages for this workflow instance. Select the Application Service scope, click the Variables button and just as you did before, add a new variable ApplicationIDHandle of type CorrelationHandle to the Application Service scope.
  2. Next set the response data. In the SendResponse activity, click on View message… to display the content definition dialog. Change the Message data to use the variable ApplicationResponse.

    Figure 21

    Set the message data of the response to ApplicationResponse

  3. Now you can define the CorrelationInitializers. The initializer will extract data from the outgoing message using an XPath expression. The extracted key will be used to identify the persisted workflow instance. With the SendResponse activity selected, in the Properties window click beside CorrelationIntializers to display the Add Correlation Initializers dialog.
  4. Click Add initializer and add ApplicationIDHandle.
  5. With the ApplicationIDHandle initializer selected, in the XPath Queries window double-click the ApplicationID : Int32 message content.

    Figure 22

    Double-click the ApplicationID : Int32 message content

  6. Click OK to dismiss the dialog.
  7. The default namespace for types in your workflow service is tempuri.org. If you want to change the default namespace you will need to edit the XAMLX file using the XML editor. To do this, in Solution Explorer right-click on SubmitApplication.xamlx and select View Code.
  8. Locate the Name= attribute and insert p: in front of the name as shown. This will cause the service to use the namespace of the operation contract as the default namespace for the WSDL types.

    XML

    <WorkflowService 
    FakePre-f50ebc75288c4a8abab75fc19044cf03-b0c42b2c841f4c718a2c67634b2fb3e5FakePre-51e94b32d1784235a7ae6573f06665f7-b33ae9f01b7f4058ba2775baeb4168a4FakePre-a6a676ed60c54ca1bd04d7de07727b95-276a5f5eb0654fa38853b0650a5b0939 Name="p:SubmitApplication"

Task 8 – Adding the Client Application

  1. Add the existing HRClient project. To do this, in Solution Explorer right-click the solution node, and select Add / Existing Project. In the Add Existing Project dialog, browse to Source\Assets folder of this lab (choosing the folder that matches the language of your preference) and select the HRClient project.
  2. Add a Service Reference to the SubmitApplication.xamlx service, to do this:
    1. Right-click on the HRClient project and select Add Service Reference.
    2. In the Add Service Reference dialog click on the Discover button to discover services in the solution. Visual Studio will find the SubmitApplication.xamlx file. Double-click it to see the details of the service. This will launch the service in the ASP.NET Development server.
    3. Set the namespace of the service to SubmitApp.

      Figure 23

      Configuring the service reference

    4. Click the Advanced button, check the Generate asynchronous operations checkbox and click OK.

      Figure 24

      Check the Generate asynchronous operations checkbox in the advanced settings

  3. Click OK to save and dismiss the Add Service Reference dialog.
  4. From the menu select Build / Build Solution (CTRL+SHIFT+B).

Next Step

Exercise 1: Verification