Click here to Skip to main content
15,881,803 members
Articles / Hosted Services / Azure
Tip/Trick

Azure BizTalk Services Reading Service Bus Queue and Send to Web Service

Rate me:
Please Sign up or sign in to vote.
5.00/5 (1 vote)
12 Feb 2015CPOL4 min read 8.7K   2  
This tip will show you how to read from an Azure Service Bus Queue and then to use the new BizTalk Services to transform the message to call a soap webservice.

Introduction

I decided to write this tip to hopefully help some of you also on the same path moving from BizTalk Server 2010/13 to Azure BizTalk Services in the Cloud.

This tip will show you how to read from an Azure Service Bus Queue and then to use the new BizTalk Services to transform the message to call a soap web service.

I will explain the following steps in this post:

  1. The Solution Architecture
  2. Brokered Message (XML Layout, Code to move message onto the queue)
  3. Create BizTalk Services project (step by step)

Background

I will not cover how to setup and configure your Azure BizTalk Services and Azure Service Bus in Visual Studio 2012/2013 in this tip. I used Visual Studio Premium 2012 to develop my solution.

Make sure you have all the Azure SDK’s and Azure BizTalk Services SDK and correctly configured:

Here are a few links:

Using Azure BizTalk Services to read Azure Service Bus Queue and Send to Web Service

1. Solution Architecture

Image 1

Please note, Windows Azure BizTalk Services (WABS) only works asynchronously, and you will have to create a response queue with a call-back from the external web service to be able to manage responses from the external web service.

A calling application will move an XML Brokered Message onto a Request Queue. BizTalk will pick up the message from the request queue, transform from one XML to another XML, and forward the message to an external web service.

2. Brokered Message

Create or use the same XML Schema used to generate the Brokered Message.

XML
  <?xml version="1.0" encoding="utf-16" ?> 
- <xs:schema xmlns=http://AzureBizTalkTest.TransactionSchema 
    xmlns:b=http://schemas.microsoft.com/BizTalk/2003 
    targetNamespace=http://AzureBizTalkTest.TransactionSchema 
    xmlns:xs="http://www.w3.org/2001/XMLSchema">
- <xs:element name="Transaction">
- <xs:complexType>
- <xs:sequence>
  <xs:element name="TransactionID" type="xs:string" /> 
  <xs:element name="TypeTransaction" type="xs:string" /> 
  <xs:element name="Amount" type="xs:double" /> 
  <xs:element name="Qty" type="xs:int" /> 
  </xs:sequence>
  </xs:complexType>
  </xs:element>
  </xs:schema>

The following code describes how to move the message to the Azure Service Bus Queue named requestqueue.

Note

  1. The message is serialized by using the XmlSerializer.
  2. Make sure when you create the Queue (requestqueue) on Windows Azure Manage Console, configure the queue with a shared access policy, with Manage, Send, Listen permissions.
C#
private bool ForwardQueueMessage(MyTransaction transaction)
        {
            try
            {
                const string QueueName = "requestqueue"; // Request Queue Name
                string connectionString = CloudConfigurationManager.GetSetting
                        ("Microsoft.ServiceBus.ConnectionString");
                var namespaceManager = 
		NamespaceManager.CreateFromConnectionString(connectionString);
 
                //Check to see if Queue exists, if it doesn’t, create it
                if (!namespaceManager.QueueExists(QueueName))
                {
                    namespaceManager.CreateQueue(QueueName);
                }
 
                MessagingFactory factory = 
		MessagingFactory.CreateFromConnectionString(connectionString);
 
                // Serialize the message
                XmlSerializer xmlSerializer = new XmlSerializer(typeof(MyTransaction));
                MemoryStream stream = new MemoryStream();
                xmlSerializer.Serialize(stream, transaction);
                stream.Position = 0;
 
                // Create Queue Client
                QueueClient Client = 
		QueueClient.CreateFromConnectionString(connectionString, QueueName);
 
                // Send the Message to the queue. Use the stream
                Client.Send(new BrokeredMessage(stream, true));
 
                return true;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

3. Create BizTalk Services Project

  • When creating the Azure BizTalk project, make sure to add the Schema file (xsd) to the project as shown below:

    Image 2

  • Open MessageFlowItinerary.bcs, select its own properties and make sure you set the correct BizTalk Service URL and then select the Service Bus Queue Source from the toolbox and drop on the work space.

    Image 3

  • Set the RequestQueue properties.
    Connection String: Go to the manage.windowsazure.com management console in Service Bus Queue, select the main Service Bus, and open Connection Information. Copy the connection string, and paste in the BizTalk Services Project RequestQueue properties (as shown below).

    Image 4

    Image 5

  • The Queue Name should be the same as the queue created in Azure Management Console.
  • Drag and drop an XML One-Way Bridge on the work space and link the RequestQueue to the XmlOnWayBridge1 by using a Connector.

    Image 6

  • Add a Service Reference to the project that links to your soap web service.
  • Add new Item to the project, select Map from the BizTalk Services list, then name it MapTransToWeb and click ok.
  • Select Source Schema : TransactionSchema
  • Select the Destination Schema: Select the last schema populated by the Service Reference.
  • Map the fields needed for the web service call.

    Image 7

  • Go back to MessageFlowItinerary.bcs, drag and drop One-Way External Service Endpoint and name is TransactionWebService.
  • First go to the project and open TransactionWebService.config, change the endpoint address to the web service URL.
  • Go back to MessageFlowItinerary.bcs, connect the XmlOneWayBridge1 to the TransactionWebService using a Connector.

    Image 8

  • Now the fun begins:
  • Double click the XML On-Way Bridge
  • Select the plus (+) and add the Request Message Type. Select the Transaction Schema.

    Image 9

  • Disable the following properties after Message Type: Validate, Enrich
  • Then click on XML Transform, select Collection on Properties and select the MapTransToWeb.trfm and click OK.

    Image 10

  • Disable the following properties after Transform: Enrich, Encode
  • Go back to MessageFlowItinerary.bcs workspace, then select the connector between XmlOneWayBridge1 and open the Filter Condition. Select Match All.

    Image 11

  • Then open Route Action, click add. Select Expression, then use the Web Service Reference OperationContractAttribute, which in my case is: ‘http://tempuri.org/IVendorResponseService/SendTransactionToVendorSync’ and make sure it is in double quotes. Also select Type : Soap and Identifier: Action

    Image 12

  • And that’s it, now you can write a test app, move to the Brokered Message to the queue.
  • To deploy, right click on the Project and enter your details, such as Deployment Endpoint, Acs Namespace, Issuer Name and Shared Secret.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Architect
South Africa South Africa
Hands-on Solution Architect developing on C#, SQL and also Azure Cloud Services. I am TOGAF Certified and have been a solution architect for more than 7 years and more than total 22+ years experience in IT, mostly software development, design and integration.

Comments and Discussions

 
-- There are no messages in this forum --