本部分介绍如何通过分别实现 IMetadataBrowseHandler 和 IMetadataSearchHandler 来公开使用适配器的浏览和搜索功能。
IMetadataBrowseHandler
将适配器添加到项目时,IMetadataBrowseHandler 允许浏览适配器支持的类别和操作。 这允许适配器使用者在设计时查看元数据信息,并仅选择客户端流程所需的操作。
使用 "Add Adapter Service Reference Visual Studio 插件" 或 "Consume Adapter Service BizTalk Project Add-in" 将适配器添加到项目时,IMetadataBrowseHandler 将填充 “选择协定类型”、“选择类别” 和 “可用类别和操作” 框。
以下示例演示如何实现 IMetadataBrowseHandler。 它构造了一个 MetadataRetrievalNode 数组,其中包含有关适配器支持的类别和操作的信息。
public class EchoAdapterMetadataBrowseHandler : EchoAdapterHandlerBase, IMetadataBrowseHandler
{
/// <summary>
/// Initializes a new instance of the EchoAdapterMetadataBrowseHandler class
/// </summary>
public EchoAdapterMetadataBrowseHandler(EchoAdapterConnection connection
, MetadataLookup metadataLookup)
: base(connection, metadataLookup)
{
}
#region IMetadataBrowseHandler Members
/// <summary>
/// Retrieves an array of MetadataRetrievalNodes from the target system.
/// The browse operation will return nodes starting from the childStartIndex in the path provided in absoluteName, and the number of nodes returned is limited by maxChildNodes.
/// The method should complete within the specified timespan or throw a timeout exception.
/// If absoluteName is null or an empty string, return nodes starting from the root + childStartIndex.
/// If childStartIndex is zero, then return starting at the node indicated by absoluteName (or the root node if absoluteName is null or empty).
/// </summary>
public MetadataRetrievalNode[] Browse(string nodeId
, int childStartIndex
, int maxChildNodes, TimeSpan timeout)
{
// note we don't support timeout in this sample
if (MetadataRetrievalNode.Root.NodeId.Equals(nodeId))
{
MetadataRetrievalNode node = new MetadataRetrievalNode("EchoMainCategory");
node.DisplayName = "Main Category";
node.IsOperation = false;
node.Description = "This category contains inbound and outbound categories.";
node.Direction = MetadataRetrievalNodeDirections.Inbound | MetadataRetrievalNodeDirections.Outbound;
return new MetadataRetrievalNode[] { node };
}
else if( "EchoMainCategory".CompareTo(nodeId) == 0 )
{
// Inbound operations
MetadataRetrievalNode inOpNode1 = new MetadataRetrievalNode("Echo/OnReceiveEcho");
inOpNode1.DisplayName = "OnReceiveEcho";
inOpNode1.Description = "This operation echoes the ___location and length of a file dropped in the specified file system.";
inOpNode1.Direction = MetadataRetrievalNodeDirections.Inbound;
inOpNode1.IsOperation = true;
// Outbound operations
MetadataRetrievalNode outOpNode1 = new MetadataRetrievalNode("Echo/EchoStrings");
outOpNode1.DisplayName = "EchoStrings";
outOpNode1.Description = "This operation echoes the incoming string COUNT number of times in a string array.";
outOpNode1.Direction = MetadataRetrievalNodeDirections.Outbound;
outOpNode1.IsOperation = true;
MetadataRetrievalNode outOpNode2 = new MetadataRetrievalNode("Echo/EchoGreetings");
outOpNode2.DisplayName = "EchoGreetings";
outOpNode2.Description = "This operation echoes the incoming Greeting object COUNT number of times in an array of type Greeting.";
outOpNode2.Direction = MetadataRetrievalNodeDirections.Outbound;
outOpNode2.IsOperation = true;
MetadataRetrievalNode outOpNode3 = new MetadataRetrievalNode("Echo/EchoCustomGreetingFromFile");
outOpNode3.DisplayName = "EchoCustomGreetingFromFile";
outOpNode3.Description = "This operation echoes the greeting object by reading its instance from a file. The Greeting object's metadata is obtained from a predefined XSD file.";
outOpNode3.Direction = MetadataRetrievalNodeDirections.Outbound;
outOpNode3.IsOperation = true;
return new MetadataRetrievalNode[] { inOpNode1, outOpNode1, outOpNode2, outOpNode3 };
}
return null;
}
#endregion IMetadataBrowseHandler Members
}
IMetadataSearchHandler
在适配器中实现 IMetadataSearchHandler,可以在设计时通过输入搜索词(例如操作名称的一部分)来搜索可用的操作。 如果适配器包含许多操作,这非常有用,因为可以输入搜索值来限制返回的操作。
使用添加适配器服务引用 Visual Studio 插件或适配器服务 BizTalk 项目外接程序将适配器添加到项目时,IMetadataSearchHandler 解析在 在类别中搜索 框中输入的搜索字符串,并返回 可用类别和操作 框中匹配项的列表。
还可以在为适配器生成 WSDL 或代理时使用 svcutil.exe 执行搜索,方法是将搜索值作为 op=value 格式的查询字符串传递。 下面是一个示例,演示如何使用svcutil.exe仅返回Echo/EchoStrings操作信息。
svcutil.exe “echov2://lobhostname/lobapplication?enableAuthentication=False&op=Echo/EchoStrings” /target:metadata
注释
WCF LOB 适配器 SDK 不提供默认通配符搜索功能,如 Echo* 或 %Echo%。 适配器作者可实现通配符或模式匹配功能。
以下示例演示如何实现 IMetadataSearchHandler。 它构造一个 MetadataRetrievalNode 数组,其中包含有关适配器支持的类别和操作的信息。
public class EchoAdapterMetadataSearchHandler : EchoAdapterHandlerBase, IMetadataSearchHandler
{
/// <summary>
/// Initializes a new instance of the EchoAdapterMetadataSearchHandler class
/// </summary>
public EchoAdapterMetadataSearchHandler(EchoAdapterConnection connection
, MetadataLookup metadataLookup)
: base(connection, metadataLookup)
{
}
#region IMetadataSearchHandler Members
/// <summary>
/// Retrieves an array of MetadataRetrievalNodes (see Microsoft.ServiceModel.Channels) from the target system.
/// The search will begin at the path provided in absoluteName, which points to a ___location in the tree of metadata nodes.
/// The contents of the array are filtered by SearchCriteria and the number of nodes returned is limited by maxChildNodes.
/// The method should complete within the specified timespan or throw a timeout exception. If absoluteName is null or an empty string, return nodes starting from the root.
/// If SearchCriteria is null or an empty string, return all nodes.
/// </summary>
public MetadataRetrievalNode[] Search(string nodeId
, string searchCriteria
, int maxChildNodes, TimeSpan timeout)
{
List<MetadataRetrievalNode> resultList = new List<MetadataRetrievalNode>();
if ("OnReceiveEcho".ToLower().Contains(searchCriteria.ToLower()))
{
MetadataRetrievalNode nodeInbound = new MetadataRetrievalNode("Echo/OnReceiveEcho");
nodeInbound.DisplayName = "OnReceiveEcho";
nodeInbound.Description = "This operation echos the ___location and length of a file dropped in the specified file system.";
nodeInbound.Direction = MetadataRetrievalNodeDirections.Inbound;
nodeInbound.IsOperation = true;
resultList.Add(nodeInbound);
}
if ("EchoStrings".ToLower().Contains(searchCriteria.ToLower()))
{
MetadataRetrievalNode outOpNode1 = new MetadataRetrievalNode("Echo/EchoStrings");
outOpNode1.DisplayName = "EchoStrings";
outOpNode1.Description = "This operation echoes the incoming string COUNT number of times in a string array.";
outOpNode1.Direction = MetadataRetrievalNodeDirections.Outbound;
outOpNode1.IsOperation = true;
resultList.Add(outOpNode1);
}
if ("EchoGreetings".ToLower().Contains(searchCriteria.ToLower()))
{
MetadataRetrievalNode outOpNode2 = new MetadataRetrievalNode("Echo/EchoGreetings");
outOpNode2.DisplayName = "EchoGreetings";
outOpNode2.Description = "This operation echoes the incoming Greeting object COUNT number of times in an array of type Greeting.";
outOpNode2.Direction = MetadataRetrievalNodeDirections.Outbound;
outOpNode2.IsOperation = true;
resultList.Add(outOpNode2);
}
if ("EchoCustomGreetingFromFile".ToLower().Contains(searchCriteria.ToLower()))
{
MetadataRetrievalNode outOpNode3 = new MetadataRetrievalNode("Echo/EchoCustomGreetingFromFile");
outOpNode3.DisplayName = "EchoCustomGreetingFromFile";
outOpNode3.Description = "This operation echoes the greeting object by reading its instance from a file. The Greeting object's metadata is obtained from a predefined XSD file.";
outOpNode3.Direction = MetadataRetrievalNodeDirections.Outbound;
outOpNode3.IsOperation = true;
resultList.Add(outOpNode3);
}
return resultList.ToArray();
}
#endregion IMetadataSearchHandler Members
}