7 min read

The flow of the solution looks like the following:

Building the Content Based Routing Solution on Microsoft Platform

An order comes from a customer to a single endpoint at McKeever Technologies. This single endpoint then routes the order based on the content of the order (that is, the value of the Product ID element). The router sends requests to WCF Workflow Services, which can provide us durability and persistence when talking to the backend order management systems. If an order system is down, then the workflow gets suspended and will be capable of resuming once the system comes back online.

Setup

First, create a new database named Chapter8Db in your local SQL Server 2008 instance. Then locate the database script named Chapter8Db.sql in the folder Chapter8Begin and install the tables into your new database. When completed, your configuration should look like the following screenshot:

Building the Content Based Routing Solution on Microsoft Platform

Next, open Chapter8.sln in the Chapter8Begin folder. In this base solution you will find two WCF services that represent the interfaces in front of the two order management systems at McKeever Technologies. Build the services and then add both of them as applications in IIS. Make sure you select the .NET 4.0 application pool.

Building the Content Based Routing Solution on Microsoft Platform

If you choose, you can test these services using the WCF Test Client application that comes with the .NET 4.0 framework. If your service is configured correctly, an invocation of the service should result in a new record in the corresponding SQL Server database table.

Building the workflow

Now that our base infrastructure is in place, we can construct the workflows that will execute these order system services.

  1. Launch Visual Studio.NET 2010 and open Chapter8.sln in the <Installation Directory>Chapter8Begin folder. You should see two WCF services.

    Building the Content Based Routing Solution on Microsoft Platform

  2. We now must add a new workflow project to the solution. Recall that this workflow will sit in front of our order service and give us a stronger quality of service, thanks to the persistence capability of AppFabric. In Visual Studio .NET 2010, go to File and select New Project.
  3. Select the WCF Workflow Service project type under the Workflow category and add the project named Chapter8.SystemA.WorkflowService to our existing solution.

    Building the Content Based Routing Solution on Microsoft Platform

  4. This project is now part of the solution and has a default workflow named Service1.xamlx.

    Building the Content Based Routing Solution on Microsoft Platform

  5. Rename the Service1.xamlx file to SystemAOrderService.xamlx from within the Solution Explorer. Also click the whitespace within the workflow to change both the ConfigurationName and Name properties.

    Building the Content Based Routing Solution on Microsoft Platform

  6. We want all our service-fronting workflows to have the same external-facing contract interface so that we can effectively abstract the underlying service contracts or implementation nuances. Hence, we add a new class file named OrderDataContract.cs to this workflow project. This class will hold the data contracts defining the input and output for all workflows that sit in front of order systems.
  7. Make sure the project itself has a reference to System.Runtime.Serialization, and then add a using statement for System.Runtime.Serialization to the top of the OrderDataContract.cs class. Add the following code to the class:

    namespace Chapter8.WorkflowService
    {
    [DataContract(
    Namespace = "http://Chapter8/OrderManagement/DataContract")] public class NewOrderRequest
    {
    [DataMember] public string OrderId { get; set; }
    [DataMember] public string ProductId { get; set; }
    [DataMember] public string CustomerId { get; set; }
    [DataMember] public int Quantity { get; set; }
    [DataMember] public DateTime DateOrdered { get; set; }
    [DataMember] public string ContractId { get; set; }
    [DataMember] public string Status { get; set; }
    }
    [DataContract(
    Namespace = "http://Chapter8/OrderManagement/DataContract")] public class OrderAckResponse
    {
    [DataMember] public string OrderId { get; set; }
    }
    }

  8. Open the SystemAOrderService.xamlx workflow, click on the top ReceiveRequest shape, and set the following property values. Note that we will use the same values for all workflows so that the external-facing contract of each workflow appears the same.
    Property Value
    DisplayName ReceiveOrderRequest
    OperationName SubmitOrder
    ServiceContractName {http://Chapter8/OrderManagement} ServiceContract
    Action http://Chapter8/OrderManagement/SubmitOrder
    CanCreateInstance True
  9. Click the Variables tab at the bottom of the workflow designer to show the default variables added to the workflow.

    Building the Content Based Routing Solution on Microsoft Platform

  10. Delete the data variable.
  11. Create a new variable named OrderReq. For the variable type, choose Browse for Types and choose the NewOrderRequest type we defined earlier in the OrderDataContract.cs class.

    Building the Content Based Routing Solution on Microsoft Platform

  12. Add another variable named OrderResp and choose the previously defined OrderAckResponse .NET type.
  13. The OrderReq variable gets instantiated by the initial request, but we need to explicitly set the OrderResp variable. In the Default column within the Variables window, set the value to New OrderAckResponse().
  14. Set a proper variable for the initial receive shape by clicking on the ReceiveOrderRequest shape and click on the View Message link. Choose OrderReq as the Message data and set the type as NewOrderRequest.

    Building the Content Based Routing Solution on Microsoft Platform

  15. Now we do the same for the response shape. Select the SendResponse shape and click on the View Message link. Choose the OrderResp variable as the Message data and OrderAckResponse as the Message type.
  16. Keep the SendResponse shape selected and set its PersistBeforeSend property to On. This forces a persistence point into our workflow and ensures that any errors that occur later in the workflow will lead to a suspended/resumable instance.
  17. We can test our workflow service prior to completing it. We want to populate our service response object, so go to the Workflow toolbox, view the Primitives tab, and drag an Assign shape in between the existing receive and send shapes.

    Building the Content Based Routing Solution on Microsoft Platform

  18. In the Assign shape, set the To value to OrderResp.OrderID and the right side of the equation to System.GUID.NewGUID().ToString(). This sets the single attribute of our response node to a unique tracking value.
  19. Build the workflow and if no errors exist, right-click the SystemAOrderSystem.xamlx workflow in the Solution Explorer and choose View in Browser.
  20. Open the WCF Test Client application and point it to our Workflow Service endpoint. Double-click the Submit Order operation, select the datatype in the Value column, and enter test input data. Click on the Invoke button and observe the response object coming back with a GUID value returned in the OrderId attribute.

    Building the Content Based Routing Solution on Microsoft Platform

  21. Now we’re ready to complete our workflow by actually calling our target WCF service that adds a record to the database table. Return to Visual Studio. NET, right-click the Chapter8.SystemA.WorkflowService project, and choose Add Service Reference.
  22. Point to the service located at http://localhost/Chapter8.OrderManagement.SystemA/OrderIntakeService.svc and type SystemASvcRef as the namespace.
  23. If the reference is successfully added and the project is rebuilt, then a new custom workflow activity should be added to the workflow toolbox. This activity encapsulates everything needed to invoke our system service.

    Building the Content Based Routing Solution on Microsoft Platform

  24. Add variables to the workflow that represent the input and output of our system service. Create a variable named ServiceRequest and browse for the type Order, which can be found under the service reference. Set the default value of this variable to New Order().

    Building the Content Based Routing Solution on Microsoft Platform

  25. Create another variable named ServiceResponse and pick the same order object but do not set a default value.
  26. Drag the custom AddOrder activity from the workflow toolbox and place it after the SendResponse shape. This sits after the workflow service response is sent, so that if errors occur the caller will not be impacted.
  27. Click the AddOrder shape and set its NewOrder property to the ServiceRequest variable and its AddOrderResult property to ServiceResponse.
  28. Now we have to populate the service request object. Drag a Sequence workflow activity from the Control Flow tab and drop it immediately before the AddOrder shape.
  29. Add six Assign shapes to the Sequence and set each activity’s left and right fields as follows:
    Left Side Right Side
    ServiceRequest.ContractId OrderReq.ContractId
    ServiceRequest.CustomerId OrderReq.CustomerId
    ServiceRequest.DateOrdered OrderReq.DateOrdered
    ServiceRequest.OrderNumber OrderResp.OrderId
    ServiceRequest.ProductId OrderReq.ProductId
    ServiceRequest.Quantity OrderReq.Quantity

    Note that the OrderNumber value of the request is set using the OrderResp object as that is the one to which we added the GUID value.

  30. Our final workflow should look like the following:

    Building the Content Based Routing Solution on Microsoft Platform


Subscribe to the weekly Packt Hub newsletter. We'll send you the results of our AI Now Survey, featuring data and insights from across the tech landscape.

LEAVE A REPLY

Please enter your comment!
Please enter your name here