TCP WCF LOB 适配器






4.75/5 (6投票s)
一个 BizTalk TCP WCF LOB 适配器。
引言
开发人员面临的挑战是构建能够在组织和平台边界之间互操作并连接到现有业务线 (LOB) 系统的服务。与此同时,这些服务和 LOB 系统相互独立地演变,使得连接应用程序和服务成为一个持续的挑战。
背景
在 BizTalk Server 的早期,微软提供了一组适配器,用于处理最常见的外部实体,例如 FILE、SOAP 和 HTTP,当集成价值对商业世界变得显而易见时,引入了一组新的适配器,直到我们达到了一个点,即提供的适配器远远超过了他们能提供的,因为复杂性因业务而异,协议也不同。
因此,他们引入了一个新的 SDK,可用于开发自定义适配器,与经典的 SDK 相比,这个 SDK 的附加值在于它使用 Windows Communication Foundation 与适配器通信,这使我们能够将我们的服务绑定到适配器,不仅是协议,还可以在 BizTalk 和非 BizTalk 应用程序中使用。
使用代码
首先,您必须从 http://www.microsoft.com/downloads/en/details.aspx?FamilyID=56278fde-b708-469c-987e-ded9c6c5e580 下载并安装 WCF LOB 适配器 SDK 2.0。然后打开 Visual Studio 2005 并单击“新建项目”,单击“Visual C#”,您将找到一个 WCF LOB 适配器模板。选择它并单击“确定”。然后您必须按照下面显示的向导操作。
单击“下一步”。
- 方案:将其视为跟踪日志的唯一名称和适配器连接的前缀。
- 项目命名空间:C# 应用程序的实际命名空间。
适配器 SDK 支持两个主要功能以及每个功能的两个子类别
- 出站:从 BizTalk 到外部位置;支持同步和异步通信。
- 入站:BizTalk 将侦听传入请求;支持同步和异步通信。
默认情况下,框架支持连接池;为了更安全,我们将添加 enablePooling
参数,该参数将启用或禁用默认的连接池。
我们的 TCP 适配器将有两个连接参数:IP 地址和端口。
public class AdapterBindingCollectionElement :
StandardBindingCollectionElement<AdapterBindingObject,AdapterBindingElement>
{
// This class Holds the WCF binding collection
// No Changes to be applied on this class.
}
public class AdapterBindingElement : StandardBindingElement
{
// This class provides a base class for WCF Binding element
// No Changes to be applied on this class
}
public class AdapterBindingElementExtensionElement : BindingElementExtensionElement
{
//This class is provided to surface Adapter as a binding element, so that it
//can be used within a user-defined WCF "Custom Binding".
//In configuration file, it is defined under
//No Changes to be applied on this class
}
public class AdapterBindingObject : AdapterBinding
{
//This is the class used while creating a binding for an adapter
private void ApplyConfiguration(string configurationName)
{
BindingsSection bindingsSection = (BindingsSection)
System.Configuration.ConfigurationManager.GetSection(
"system.serviceModel/bindings");
AdapterBindingCollectionElement bindingCollectionElement =
(AdapterBindingCollectionElement)
bindingsSection["LOBTCPAdapterBinding"];
AdapterBindingElement element =
bindingCollectionElement.Bindings[configurationName];
if (element != null)
{
element.ApplyConfiguration(this);
}
}
}
ApplyConfiguration
必须编辑以插入新的自定义绑定名称和位置。
public class AdapterConnectionFactory : IConnectionFactory
{
//Defines the connection factory for the target system
private AdapterConnectionUri connection;
public AdapterConnectionFactory(ConnectionUri connectionUri,
ClientCredentials clientCredentials, AdapterCore adapter)
{
this.clientCredentials = clientCredentials;
this.adapter = adapter;
this.connection = connectionUri as AdapterConnectionUri;
}
public AdapterConnectionUri Connection
{
get
{
return this.connection;
}
}
}
public class AdapterConnectionUri : ConnectionUri
{
//This is the class for representing an adapter connection uri
#region Custom Generated Fields
private string serverIP = "127.0.0.1";
private int serverPort = 0;
private UriBuilder uriBuilder;
#endregion Custom Generated Fields
// The above fields will be used to build adapter endpoint address
public AdapterConnectionUri() { Initialize(null); }
public AdapterConnectionUri(Uri uri): base()
{
Initialize(uri);
}
public override Uri Uri
{
get
{
return BuildUri();
}
set
{
ParseUri(value);
}
}
// we will override the URI property to build out address,
// the address should be {scheme://Ipaddress:port}
// Scheme property can be found the in the adapter core class
}
public class AdapterCore : Adapter
{
//The main adapter class which inherits from Adapter
public class AdapterCore : Adapter
{
// Will be used to hold the adapter operation configuration that will be used
// in the search, browse and resolver handlers
internal static XmlDocument operationsRepository = null;
}
public AdapterCore(): base(environmentSettings)
{
Settings.Metadata.DefaultMetadataNamespace = SERVICENAMESPACE;
if (operationsRepository == null)
{
operationsRepository = new XmlDocument();
operationsRepository.Load(@"C:\Program Files\Test\" +
@"LOBAdapters\TCP\OperationsConfiguration.xml");
}
}
public AdapterCore(AdapterCore binding): base(binding)
{
this.Settings.ConnectionPool.EnablePooling = binding.EnableConnectionPolling;
this.Settings.Metadata.GenerateWsdlDocumentation = true;
if (operationsRepository == null)
{
operationsRepository = new XmlDocument();
operationsRepository.Load(@"C:\Program Files\" +
@"Test\LOBAdapters\TCP\OperationsConfiguration.xml");
}
}
}
public abstract class AdapterHandlerBase
{
//This is the base class for handlers used
//to store common properties/helper functions
protected virtual void Dispose(bool disposing)
{
// NOTHING TO DO
}
}
public class AdapterMetadataBrowseHandler : AdapterHandlerBase, IMetadataBrowseHandler
{
//This class is used while performing a connection-based
//browse for metadata from the target system
//This class will use the OperationsConfiguration.xml file
}
public class AdapterMetadataResolverHandler : AdapterHandlerBase, IMetadataResolverHandler
{
//This class is used for performing a connection-based
//retrieval of metadata from the target system
}
public class AdapterMetadataSearchHandler : AdapterHandlerBase, IMetadataSearchHandler
{
//This class is used for performing a connection-based
//search for metadata from the target system
}
public class AdapterConnection : IConnection
{
//This class will be hold the actual communication
//with the LOB in our case it will be
//TCPClient class
}
public class AdapterOutboundHandler : AdapterHandlerBase, IOutboundHandler
{
// This class will handle the message receiving and we will implement data manipulation.
}
部署
要部署适配器,请按照以下步骤操作
- 将 DLL 放入 GAC。
- 将 OperationsConfiguration.xml 放入 "C:\Program Files\Test\LOBAdapters\TCP\"。
- 打开 C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\CONFIG\machine.config。
- 注册适配器。(配置附在适配器源代码中。)
在 BizTalk 中使用适配器
部署完成后,部署 BizTalk 测试项目并打开管理控制台,转到发送端口,创建一个新的静态请求-响应发送端口。在传输类型中,选择 WCF-Custom 并单击配置。
首先添加端点地址。
选择 LOBTCPAdapterBinding WCF 绑定。
关注点
限制:TCP 消息大小限制为 7168 字节。
此适配器可以从 BizTalk Server 之外调用,例如从 WCF 服务或 WWF 服务调用。
历史
- 版本 1.0。