使用 IMetadataRetrievalContract 获取 Oracle 数据库中的元数据

适用于 Oracle 数据库的 Microsoft BizTalk 适配器公开 IMetadataRetrievalContract终结点,可用于浏览和搜索 Oracle 数据库项目,以及检索 Web 服务描述语言 (WSDL) 文档形式的操作元数据。

IMetadataRetrievalContract 接口由 Microsoft Windows Communication Foundation (WCF) 业务线 (LOB) 适配器 SDK 实现,并提供元数据浏览、搜索和检索功能。 除了 IMetadataRetrievalContract 接口外,WCF LOB 适配器 SDK 还公开了实现接口的 MetadataRetrievalClient 类。 可以使用 IMetadataRetrievalContract 通道或 MetadataRetrievalClient 来处理元数据;在每种情况下,用于浏览、搜索和检索元数据的方法都相同。

以下部分提供有关如何使用 IMetadataRetrievalContract 接口的信息。

IMetadataRetrievalContract 接口

下表提供有关使用 IMetadataRetrievalContract 接口时使用的重要类的信息。

类或接口 说明
IMetadataRetrievalContract 接口

(Microsoft.ServiceModel.Channels)
定义 BrowseSearchGetMetadata 方法。 通过使用 IMetadataRetrievalContract 通道或 MetadataRetrievalClient 来调用这些方法,以使用适配器元数据。
MetadataRetrievalClient

(Microsoft.ServiceModel.Channels)
实现 IMetadataRetrievalContract 接口。 可以创建此类的实例,并通过提供 OracleDBBindingEndpointAddress 为 Oracle 数据库配置它。 然后,可以调用其方法来处理元数据。
MetadataRetrievalNode

(Microsoft.ServiceModel.Channels)
表示适配器上的元数据节点。 BrowseSearch 方法返回此类型的节点,GetMetadata 方法将此类型的节点作为参数。
ServiceDescription

(System.Web.Services.Description)
提供创建有效的 WSDL 文档文件并设置其格式的方法。 GetMetadata 方法返回 ServiceDescription 对象。

元数据节点 ID

适配器将其元数据组织为节点的分层树。 在此树结构中,有两种类型的元数据节点:

  • 操作节点 表示适配器在 Oracle 数据库项目上显示的操作。 操作节点是树的叶子。

  • 类别节点 表示不直接对应于适配器上的操作的 Oracle 数据库项目和 Oracle 数据库项目的分组。 类别节点是树的分支;它们包含其他类别节点和/或操作节点。 例如,Oracle 表和包表示为类别节点。

    适配器显示的每个元数据节点都由唯一的节点 ID 标识。 有关适配器显示的元数据节点 ID 的详细信息,请参阅 元数据节点 ID。 使用 IMetadataRetrievalContract 接口浏览、搜索和检索元数据时,可以使用这些节点 ID 指定目标 Oracle 数据库项目。

绑定属性

无论是使用 IMetadataRetrievalContract 通道还是 IMetadataRetrievalClient 来处理元数据,都必须在创建实例时指定 OracleDBBinding

有几个绑定属性会影响适配器生成元数据的方式。 这些属性为:

  • EnableSafeTyping

  • UseSchemaInNamespace

  • PollingStatement

重要

如果要检索 POLLINGSTMT 操作的元数据,则必须设置 PollingStatement 绑定属性。

在打开元数据检索对象之前,应确保将这些绑定属性设置为应用程序所需的值。 有关 Oracle 数据库适配器绑定属性的详细信息,请参阅 阅读有关 Oracle 数据库适配器绑定属性的信息

浏览元数据节点

使用 Browse 方法可返回父节点中包含的所有元数据节点。 以下示例浏览 Oracle 数据库上的前三个架构。 在此示例中, 客户端MetadataRetrievalClient 的实例。

// The first parameter is the node ID.   
// The second parameter is the start index.  
// The third parameter is the maximum number of nodes to return.  
                MetadataRetrievalNode[] nodes = client.Browse(MetadataRetrievalNode.Root.NodeId, 0, 3);  

重要

只能浏览类别节点;无法浏览操作节点。

搜索元数据节点

使用 Search 方法对父节点包含的节点执行搜索。 适配器支持搜索表达式中的通配符;例如,可以指定百分比 (%) 通配符来匹配零个或多个字符。 以下示例演示搜索 SCOTT 架构中包含字符串“EMP”的所有表。 在此示例中, 客户端MetadataRetrievalClient 的实例。

// Search for all nodes that contain "EMP" under the SCOTT.Table node.  
// The parameters are the parent node ID, the search expression, and   
// the maximum number of nodes to return.  
IMetadataRetrievalNode[] nodes = client.Search("http://Microsoft.LobServices.OracleDB/2007/03/SCOTT/Table", "%EMP%", 3);  
  

重要

仅在一组有限的节点上支持搜索。 有关支持搜索的节点以及搜索表达式中支持的通配符的详细信息,请参阅 元数据节点 ID

检索操作 (WSDL) 元数据

使用 GetMetadata 方法检索一组操作节点的服务说明 (WSDL 文档) 。 以下示例检索一个服务说明,其中包含适配器为 SCOTT 显示的所有操作。EMP 表通过指定其节点 ID。 在此示例中, 客户端MetadataRetrievalClient 的实例。

// Get a service description that contains all of the operations   
// surfaced for the SCOTT.EMP table. The IsOperation  
// property is set false because this is a category node.  
nodes = new MetadataRetrievalNode[1];  
nodes[0] = new MetadataRetrievalNode();  
nodes[0].NodeId = "http://Microsoft.LobServices.OracleDB/2007/03/SCOTT/Table/EMP";  
nodes[0].IsOperation = false;  
System.Web.Services.Description.ServiceDescription description = client.GetMetadata(nodes);  
  

重要

对于类别节点, IsOperation 属性应为 false,对于操作节点应为 true。

使用 MetadataRetrievalClient

创建和使用 MetadataRetrievalClient 与任何其他 WCF 客户端大致相同。 通过指定终结点和 OracleDBBinding 实例来创建客户端。 可以在配置中以声明方式执行此操作,也可以在代码中以命令性方式执行此操作。 然后调用 MetadataRetrievalClient 的方法,从适配器中浏览、搜索和检索元数据。

以下示例演示如何使用 MetadataRetrievalClient 浏览、搜索和检索 Oracle 数据库适配器中的元数据。 该示例演示了:

  • 浏览 Oracle 数据库架构的元数据树的根节点。

  • 在 SCOTT 架构中搜索包含字符串“EMP”的名称的表。

  • 检索 SCOTT 支持的所有操作的元数据。通过将类别节点传递到 GetMetadata 方法来 EMP 表。

  • 通过将 POLLINGSTMT 操作节点传递到 GetMetadata 方法检索 POLLINGSTMT 操作的元数据。

using System;  
using System.Collections.Generic;  
using System.Text;  
  
using System.ServiceModel;  
using Microsoft.Adapters.OracleDB;  
using Microsoft.ServiceModel.Channels;  
  
using System.Web.Services.Description;  
  
namespace OracleMetadataRetrieval  
{  
    class NodeWriter  
    {  
        // This method writes the value of a collection of metadata retrieval nodes  
        // to the console  
        public void Write(string title, MetadataRetrievalNode[] nodes)  
        {  
            Console.WriteLine(title);  
            Console.WriteLine();  
  
            //write all the nodes returned to the console.  
            foreach (MetadataRetrievalNode node in nodes)  
            {  
                Console.WriteLine("NodeId = " + node.NodeId);  
                Console.WriteLine("\tDirection   = " + node.Direction.ToString());  
                Console.WriteLine("\tIsOperation = " + node.IsOperation.ToString());  
                Console.WriteLine("\tDisplayName = " + node.DisplayName);  
                Console.WriteLine("\tDescription = " + node.Description);  
            }  
            Console.WriteLine();  
        }  
    }  
  
    class Program  
    {  
        static void Main(string[] args)  
        {  
            MetadataRetrievalClient client = null;  
            NodeWriter nodeWriter = new NodeWriter();  
  
            try  
            {  
                // create a binding and   
                // set the PollingStatement binding property if you want to  
                // return metadata for the POLLINGSTMT operation  
                OracleDBBinding binding = new OracleDBBinding();  
                binding.PollingStatement = "SELECT * FROM EMP";  
  
                // Set PollingId parameter if you want to alter the namespace of the POLLINGSTMT operation  
                EndpointAddress address = new EndpointAddress("oracledb://ADAPTER?PollingId=1");  
  
                client = new MetadataRetrievalClient(binding, address);  
                client.ClientCredentials.UserName.UserName = "SCOTT";  
                client.ClientCredentials.UserName.Password = "TIGER";  
                client.Open();  
  
                // Browse for the first 3 (schema) nodes directly under the root  
                // The parameters are the parent Node ID, the start index, and the maximum number  
                // of nodes to return  
                MetadataRetrievalNode[] nodes = client.Browse(MetadataRetrievalNode.Root.NodeId, 0, 3);  
                nodeWriter.Write("Browse results for the root node:", nodes);  
  
                // Search for first 3 tables that contain "EMP" in the SCOTT schema  
                // The parameters are the parent node ID, the search expression, and the maximum number  
                // of nodes to return  
                nodes = client.Search("http://Microsoft.LobServices.OracleDB/2007/03/SCOTT/Table", "%EMP%", 3);  
                nodeWriter.Write(String.Format("Search results for \"%EMP%\" under {0} node:", "http://Microsoft.LobServices.OracleDB/2007/03/SCOTT/Table"), nodes);  
  
                // Get a WSDL document that specifies a contract that contains the operations  
                // surfaced for the SCOTT.EMP table. The IsOperation property is set false  
                // because this is a category node.  
                nodes = new MetadataRetrievalNode[1];  
                nodes[0] = new MetadataRetrievalNode();  
                nodes[0].NodeId = "http://Microsoft.LobServices.OracleDB/2007/03/SCOTT/Table/EMP";  
                nodes[0].IsOperation = false;  
                System.Web.Services.Description.ServiceDescription description = client.GetMetadata(nodes);  
                description.Write("EmpTableContract.wsdl");  
  
                // Get a WSDL document that specifies a contract that contains the   
                // the POLLINGSTMT operation. The IsOperation property is set true  
                // because this is an operation node.  
                // Note that the operation NodeId is equivalent to the message action.  
                nodes = new MetadataRetrievalNode[1];  
                nodes[0] = new MetadataRetrievalNode();  
                nodes[0].NodeId = "http://Microsoft.LobServices.OracleDB/2007/03/POLLINGSTMT";  
                nodes[0].IsOperation = true;  
                description = client.GetMetadata(nodes);  
                description.Write("PollingContract.wsdl");  
  
            }  
            catch (Exception ex)  
            {  
                Console.WriteLine("Exception is: " + ex.Message);  
            }  
            finally  
            {  
                if (client != null)  
                {  
                    if (client.State == CommunicationState.Opened)  
                        client.Close();  
                    else  
                        client.Abort();  
                }  
            }  
        }  
    }  
}  

下面显示了主机上此程序的输出。 可以看到每个方法返回的元数据检索节点的结构。 程序还会将两个 WSDL 文档写入文件。

Browse results for the root node:  
  
NodeId = http://Microsoft.LobServices.OracleDB/2007/03/ADAPTERTEST  
        Direction   = Outbound  
        IsOperation = False  
        DisplayName = ADAPTERTEST  
        Description = Owned By ADAPTERTEST  
NodeId = http://Microsoft.LobServices.OracleDB/2007/03/ADAPTEST  
        Direction   = Outbound  
        IsOperation = False  
        DisplayName = ADAPTEST  
        Description = Owned By ADAPTEST  
NodeId = http://Microsoft.LobServices.OracleDB/2007/03/ADMIN123  
        Direction   = Outbound  
        IsOperation = False  
        DisplayName = ADMIN123  
        Description = Owned By ADMIN123  
  
Search results for "%EMP%" under http://Microsoft.LobServices.OracleDB/2007/03/SCOTT/Table node:  
  
NodeId = http://Microsoft.LobServices.OracleDB/2007/03/SCOTT/Table/AEMP  
        Direction   = Outbound  
        IsOperation = False  
        DisplayName = AEMP  
        Description = Table.AEMP  
NodeId = http://Microsoft.LobServices.OracleDB/2007/03/SCOTT/Table/EMP  
        Direction   = Outbound  
        IsOperation = False  
        DisplayName = EMP  
        Description = Table.EMP  
NodeId = http://Microsoft.LobServices.OracleDB/2007/03/SCOTT/Table/EMP1  
        Direction   = Outbound  
        IsOperation = False  
        DisplayName = EMP1  
        Description = Table.EMP1  

使用 IMetadataRetrievalContract 通道

还可以创建 IMetadataRetrievalContract 通道,然后使用此通道从适配器中浏览、搜索和检索元数据。 (方法签名与 MetadataRetrievalClient 类相同。) 以下示例演示如何执行此操作。

…  
//Create a binding and endpoint address.  
OracleDBBinding binding = new OracleDBBinding();  
EndpointAddress address = new EndpointAddress("oracledb://ADAPTER/");  
  
//Create and open a channel factory that will return an IMetadataRetrievalContract object, on which browse, search, and get can be performed.  
ChannelFactory<IMetadataRetrievalContract> factory = new ChannelFactory<IMetadataRetrievalContract>(binding, address);  
factory.Credentials.UserName.UserName = "SCOTT";  
factory.Credentials.UserName.Password = "TIGER";  
factory.Open();  
  
//Obtain an IMetadataRetrievalContract channel from the factory.  
IMetadataRetrievalContract channel = factory.CreateChannel();  
  
//Perform a search using the channel.  
MetadataRetrievalNode[] nodes = channel.Search("http://Microsoft.LobServices.OracleDB/2007/03/SCOTT/Table", "%EMP%", int.MaxValue);  
…  

另请参阅

以编程方式从 Oracle 数据库获取元数据