Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles / Hosted-services / Azure

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

4.50/5 (5 votes)
9 Feb 2015CPOL4 min read 12.1K  
This article will show you how to read from an Azure Service Bus Queue and then to use the new Windows Azure BizTalk Services to transform a brokered Xml message to soap web service.

Introduction

I decided to write a quick article to hopefully help others with the same path moving from BizTalk Server 2010/13 to Windows Azure BizTalk Services in the Cloud.

This article 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 article:

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 article. 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 is a few links:
https://msdn.microsoft.com/en-us/library/azure/ff687127.aspx
https://msdn.microsoft.com/en-us/library/azure/hh689760.aspx 

The Project

1.    Solution Architecture<o:p>

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.

C++
  <?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: a) The message is serialized by using the XmlSerializer.
          b) 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, Entity Name, Initial Status and Queue Name). 

- 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

- Entity Name: Call this RequestQueue
- Initual Status: Leave this on Start
- Queue Name: The Queue Name should be the same name as the queue name on Azure Management Console.

Open MessageFlowItinerary.bcs, 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 workspace, 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 workspace, then 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 are: ‘http://tempuri.org/IVendorResponseService/SendTransactionToVendorSync’ and make sure it is in double quotes. Also select Type : Soap and Identifier: Action

Image 12

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. 

History

 

License

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