使用 Azure BizTalk 服务读取 Azure 服务总线队列并发送到 Web 服务






4.50/5 (5投票s)
本文将向您展示如何从 Azure Service Bus 队列读取消息,然后使用新的 Windows Azure BizTalk 服务将已路由的 XML 消息转换为 SOAP Web 服务。
引言
我决定写一篇简短的文章,希望能帮助其他人将从 BizTalk Server 2010/13 迁移到云中的 Windows Azure BizTalk 服务。
本文将向您展示如何从 Azure Service Bus 队列读取消息,然后使用新的 BizTalk 服务将消息转换为可调用 SOAP Web 服务的格式。
本文将解释以下步骤:
1.解决方案架构
2.已路由消息(XML 布局,将消息放入队列的代码)
3.创建 BizTalk 服务项目(分步说明)
背景
本文不介绍如何在 Visual Studio 2012/2013 中设置和配置您的 Azure BizTalk 服务和 Azure Service Bus。我使用 Visual Studio Premium 2012 开发了我的解决方案。
请确保已正确安装和配置所有 Azure SDK 和 Azure BizTalk 服务 SDK。
这里有一些链接:
https://msdn.microsoft.com/en-us/library/azure/ff687127.aspx
https://msdn.microsoft.com/en-us/library/azure/hh689760.aspx
项目
1. 解决方案架构
请注意,Windows Azure BizTalk 服务 (WABS) 仅支持异步操作,您需要创建一个响应队列,并设置一个回调,以便从外部 Web 服务接收响应。
调用应用程序会将一个 XML 已路由消息放入请求队列。BizTalk 将从请求队列中提取消息,将其从一种 XML 格式转换为另一种 XML 格式,然后将其转发到外部 Web 服务。
2. 已路由消息
创建或使用用于生成已路由消息的 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>
以下代码描述了如何将消息发送到名为 requestqueue 的 Azure Service Bus 队列。
注意: a) 消息使用 XmlSerializer 进行序列化。
b) 在 Windows Azure 管理控制台中创建队列(requestqueue)时,请确保配置队列以使用共享访问策略,并授予管理、发送和侦听权限。
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. 创建 BizTalk 服务项目
创建 Azure BizTalk 项目时,请确保将架构文件 (xsd) 添加到项目中,如下图所示。
打开 MessageFlowItinerary.bcs,选择其属性,确保设置了正确的 BizTalk 服务 URL,然后从工具箱中选择 Service Bus 队列源并将其拖放到工作区。
设置 RequestQueue 的属性(连接字符串、实体名称、初始状态和队列名称)。
- 连接字符串:转到 Service Bus 队列的 manage.windowsazure.com 管理控制台,选择主 Service Bus,然后打开连接信息。复制连接字符串,并将其粘贴到 BizTalk 服务项目 RequestQueue 的属性中(如下图所示)。
- 实体名称:将其命名为 RequestQueue。
- 初始状态:将其保持为 Start。
- 队列名称:队列名称应与 Azure 管理控制台中的队列名称相同。
打开 MessageFlowItinerary.bcs,将一个 Xml One-Way Bridge 拖放到工作区,然后使用 Connector 将 RequestQueue 连接到 XmlOnWayBridge1。
为项目添加一个服务引用,该引用指向您的 SOAP Web 服务。
将新项添加到项目,从 BizTalk 服务列表中选择 Map,然后将其命名为 MapTransToWeb 并单击确定。
选择源架构:TransactionSchema。
选择目标架构:选择服务引用填充的最后一个架构。
映射 Web 服务调用所需的字段。
回到 MessageFlowItinerary.bcs 工作区,拖放一个 One-Way External Service Endpoint 并将其命名为 TransactionWebService。
首先转到项目并打开 TransactionWebService.config,将终结点地址更改为 Web 服务的 URL。
回到 MessageFlowItinerary.bcs 工作区,然后使用 Connector 将 XmlOneWayBridge1 连接到 TransactionWebService。
现在,好戏开始::-)
双击 Xml On-Way Bridge。
选择加号(+)并添加请求消息类型。选择 TransactionSchema。
在 Message Type 之后禁用以下属性:Validate, Enrich。
然后单击 Xml Transform,选择 Properties 中的 Collection,然后选择 MapTransToWeb.trfm 并单击确定。
在 Transform 之后禁用以下属性:Enrich, Encode。
回到 MessageFlowItinerary.bcs 工作区,然后选择 XmlOneWayBridge1 和之间的 Connector,打开 Filter Condition。选择 Match All。
然后打开 Route Action,单击 Add。选择 Expression,然后使用 Web Service Reference OperationContractAttribute,在本例中是:‘http://tempuri.org/IVendorResponseService/SendTransactionToVendorSync’,并确保其用双引号括起来。还请选择 Type:Soap,Identifier:Action。
就是这样!现在您可以编写一个测试应用程序,将已路由消息放入队列。
要进行部署,请右键单击项目并输入您的详细信息,例如 Deployment Endpoint、Acs Namespace、Issuer Name 和 Shared Secret。
历史