步骤 5:实现 Echo 适配器的元数据搜索处理程序
)
完成时间: 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 或空字符串 (“”) ,则将从根节点 (“/”) 检索操作。 |
searchCriteria | 特定于适配器的搜索条件。 如果未指定搜索条件,适配器应返回所有节点。 |
maxChildNodes | 要返回的结果节点的最大数目。 使用 Int32.Max 检索所有结果节点。 Echo 适配器不支持。 |
timeout | 允许完成操作的最长时间。 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.Search%2A
接口的 Microsoft.ServiceModel.Channels.Common.IMetadataSearchHandler
方法。 如果操作的显示名称与搜索条件匹配,您将为该操作创建一个 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.Search%2A
实现 接口的 方法实现了 Echo 适配器的 Microsoft.ServiceModel.Channels.Common.IMetadataSearchHandler
元数据搜索功能。 具体而言,你为每个操作创建了一个 Microsoft.ServiceModel.Channels.MetadataRetrievalNode
对象,该对象与条件匹配,然后返回对象的数组 Microsoft.ServiceModel.Channels.MetadataRetrievalNode
。
后续步骤
你将实现元数据解析功能,以及出站和入站消息交换功能。 最后,你将生成并部署 Echo 适配器。
另请参阅
步骤 4:实现 Echo 适配器的元数据浏览处理程序
步骤 6:实现 Echo 适配器的元数据解析处理程序
教程 1:开发 Echo 适配器
反馈
https://aka.ms/ContentUserFeedback。
即将发布:在整个 2024 年,我们将逐步淘汰作为内容反馈机制的“GitHub 问题”,并将其取代为新的反馈系统。 有关详细信息,请参阅:提交和查看相关反馈