完成时间: 30 分钟
在本教程的此步骤中,你将实现 Echo 适配器的搜索功能。 与浏览不同,搜索是可选的。 根据 WCF LOB 适配器 SDK,若要支持搜索功能,必须实现 Microsoft.ServiceModel.Channels.Common.IMetadataSearchHandler
接口。 对于 Echo 适配器,适配器开发向导自动生成一个名为 EchoAdapterMetadataSearchHandler 的派生类。
首先更新 EchoAdapterMetadataSearchHandler 类,以便更好地了解如何实现此接口、如何填充 Microsoft.ServiceModel.Channels.MetadataRetrievalNode
对象,以及如何在“添加适配器服务引用插件”工具中显示搜索结果。
先决条件
在开始此步骤之前,请完成 步骤 4:实现 Echo 适配器的元数据浏览处理程序。 你还需要清楚了解以下类别:
Microsoft.ServiceModel.Channels.MetadataRetrievalNode
Microsoft.ServiceModel.Channels.Common.IMetadataSearchHandler
Microsoft.ServiceModel.Channels.MetadataRetrievalNodeDirections
IMetadataSearchHandler 接口
Microsoft.ServiceModel.Channels.Common.IMetadataSearchHandler
定义为:
public interface IMetadataSearchHandler : IConnectionHandler, IDisposable
{
MetadataRetrievalNode[] Search(string nodeId, string searchCriteria, int maxChildNodes, TimeSpan timeout);
}
该方法 Microsoft.ServiceModel.Channels.Common.IMetadataSearchHandler.Search%2A
基于搜索条件返回对象数组 Microsoft.ServiceModel.Channels.MetadataRetrievalNode
。 下表描述了 Search 方法的参数定义:
参数 | 定义 |
---|---|
nodeId | 要从中开始搜索的节点 ID。 如果为 null 或空字符串(“”),将从根节点(“/”)检索操作。 |
搜索条件 | 特定于适配器的搜索条件。 如果未指定搜索条件,适配器应返回所有节点。 |
maxChildNodes | 要返回的结果节点的最大数目。 使用 Int32.Max 检索所有结果节点。 Echo 适配器不支持。 |
超时 | 完成操作所允许的最长时间。 Echo 适配器不支持。 |
对于搜索结果,适配器可以选择返回类别节点、操作节点或同时返回两者。 它由适配器支持的搜索功能类型决定。
Echo适配器元数据搜索
根据目标系统的类别和操作,有多种方法可以生成包含Microsoft.ServiceModel.Channels.MetadataRetrievalNode
对象的数组以供返回。 Echo 适配器实现搜索功能的方式是通过以下列表中的节点 ID 遍历每个操作。
Echo/OnReceiveEcho, inbound operation
Echo/EchoStrings, outbound operation
Echo/EchoGreetings, outbound operation
Echo/EchoGreetingFromFile, outbound operation
如果节点 ID 与搜索条件匹配,它将使用操作的节点 ID 创建对象
Microsoft.ServiceModel.Channels.MetadataRetrievalNode
,然后为属性分配相应的值。 例如,MetadataRetrievalNode nodeInbound = new MetadataRetrievalNode("Echo/OnReceiveEcho"); //create the MetadataRetrievalNode using the operation's node ID. nodeInbound.DisplayName = "OnReceiveEcho"; //The Display Name shown in the Name column of the Add Adapter Service Reference Visual Studio Plug-in nodeInbound.Description = "This operation echoes the ___location and length of a file dropped in the specified file system."; //The Description shown as the tool tip in the Add Adapter Service Visual Studio Plug-in nodeInbound.Direction = MetadataRetrievalNodeDirections.Inbound; //It is an inbound operation nodeInbound.IsOperation = true; //It is an operation, not category.
然后将对象添加到
Microsoft.ServiceModel.Channels.MetadataRetrievalNode
的集合,例如,resultList.Add(nodeInbound);
最后以数组格式返回集合。
return resultList.ToArray();
可以使用“添加适配器服务引用插件”和“使用适配器服务加载项工具”来执行基于连接的可用操作的搜索。 例如,下图显示搜索条件为字符串 Greeting 时,搜索将返回 EchoGreetings 和 EchoGreetingFromFile 作。
如果未找到匹配项,则任一工具都将返回下图所示的错误消息。
实现 IMetadataSearchHandler 的过程
你将实现 Microsoft.ServiceModel.Channels.Common.IMetadataSearchHandler
接口中的 Microsoft.ServiceModel.Channels.Common.IMetadataSearchHandler.Search%2A
方法。 如果作的显示名称与搜索条件匹配,你将为该作创建一个 Microsoft.ServiceModel.Channels.MetadataRetrievalNode
对象,然后将该对象添加到对象的数组 Microsoft.ServiceModel.Channels.MetadataRetrievalNode
。
更新 EchoAdapterMetadataSearchHandler 类
在解决方案资源管理器中,双击 EchoAdapterMetadataSearchHandler.cs 文件。
在 Visual Studio 编辑器的 Search 方法中,将现有逻辑替换为以下内容。 如果 Echo/OnReceiveEcho 与指定的搜索条件匹配,则此逻辑将创建一个
Microsoft.ServiceModel.Channels.MetadataRetrievalNode
对象。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 echoes the ___location and length of a file dropped in the specified file system."; nodeInbound.Direction = MetadataRetrievalNodeDirections.Inbound; nodeInbound.IsOperation = true; resultList.Add(nodeInbound); }
如果 Echo/EchoStrings 与指定的搜索条件匹配,请继续添加以下逻辑来创建
Microsoft.ServiceModel.Channels.MetadataRetrievalNode
对象。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); }
如果 Echo/EchoGreetings 与指定的搜索条件匹配,请继续添加以下逻辑来创建
Microsoft.ServiceModel.Channels.MetadataRetrievalNode
对象。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); }
如果 Echo/EchoGreetingFromFile 与指定的搜索条件匹配,请继续添加以下代码以创建
Microsoft.ServiceModel.Channels.MetadataRetrievalNode
对象。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); }
继续添加以下代码以返回对象数组
Microsoft.ServiceModel.Channels.MetadataRetrievalNode
。return resultList.ToArray();
在 Visual Studio 的 “文件 ”菜单上,单击“ 全部保存”。
在 “生成” 菜单上,单击 “生成解决方案” 。 你本应该成功编译该项目。 否则,请确保已按照上述每个步骤进行操作。
注释
你保存了你的工作。 此时可以安全地关闭 Visual Studio,也可以转到下一步 ,步骤 6:实现 Echo 适配器的元数据解析处理程序。
我刚刚做了什么?
你刚刚通过实现Microsoft.ServiceModel.Channels.Common.IMetadataSearchHandler
接口的Microsoft.ServiceModel.Channels.Common.IMetadataSearchHandler.Search%2A
方法,实现了Echo适配器的元数据搜索功能。 具体而言,你为每个符合条件的作创建了一个 Microsoft.ServiceModel.Channels.MetadataRetrievalNode
对象,然后返回对象 Microsoft.ServiceModel.Channels.MetadataRetrievalNode
数组。
后续步骤
你将实现元数据解析功能以及出站和入站消息交换功能。 最后,你将生成并部署 Echo 适配器。
另请参阅
步骤 4:实现 Echo 适配器的元数据浏览处理程序
步骤 6:实现 Echo 适配器的元数据解析处理程序
教程 1:开发 Echo 适配器