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:
- The Solution Architecture
- Brokered Message (XML Layout, Code to move message onto the queue)
- 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
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.
="1.0"="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
- The message is serialized by using the
XmlSerializer
. - 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.
private bool ForwardQueueMessage(MyTransaction transaction)
{
try
{
const string QueueName = "requestqueue";
string connectionString = CloudConfigurationManager.GetSetting
("Microsoft.ServiceBus.ConnectionString");
var namespaceManager =
NamespaceManager.CreateFromConnectionString(connectionString);
if (!namespaceManager.QueueExists(QueueName))
{
namespaceManager.CreateQueue(QueueName);
}
MessagingFactory factory =
MessagingFactory.CreateFromConnectionString(connectionString);
XmlSerializer xmlSerializer = new XmlSerializer(typeof(MyTransaction));
MemoryStream stream = new MemoryStream();
xmlSerializer.Serialize(stream, transaction);
stream.Position = 0;
QueueClient Client =
QueueClient.CreateFromConnectionString(connectionString, QueueName);
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:
- 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.
- 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).
- 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.
- 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.
- 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.
- Now the fun begins:
- Double click the XML On-Way Bridge
- Select the plus (+) and add the Request Message Type. Select the Transaction Schema.
- 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.
- 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.
- 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
- 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.