使用 IMetadataRetrievalContract 在 SAP 中获取元数据

适用于 mySAP Business Suite 的 Microsoft BizTalk 适配器公开了 一个 IMetadataRetrievalContract终结点,可用于浏览和搜索 SAP 系统项目,以及以 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 接口。 可以创建此类的实例,并通过提供 SAPBindingEndpointAddress 为 SAP 系统配置它。 然后,可以调用其方法来处理元数据。
MetadataRetrievalNode

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

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

有关 IMetadataRetrievalContract 接口、 MetadataRetrievalClient 类和 MetadataRetrievalNode 类的详细信息;请参阅 中的 Microsoft.ServiceModel.Channels 托管引用 https://go.microsoft.com/fwlink/?LinkId=105566

元数据节点 ID

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

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

  • 类别节点 表示不直接对应于适配器上的操作的 SAP 项目和 SAP 项目的分组。 类别节点是树的分支;它们包含其他类别节点和/或操作节点。 例如,RFC 功能组或 SAP 业务对象表示为类别节点。

    适配器显示的每个元数据节点都由唯一的节点 ID 标识。 有关适配器显示的元数据节点 ID 的详细信息,请参阅 元数据节点 ID。 使用 IMetadataRetrievalContract 接口浏览、搜索和检索元数据时,可以使用这些节点 ID 指定目标 SAP 项目。 可以使用 和 Microsoft.Adapters.SAP.SAPAdapterConstants.ActionConstantsMicrosoft.Adapters.SAP.SAPAdapterConstants.MetadataConstants定义的常量来帮助构造元数据节点 ID。

绑定属性

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

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

浏览元数据节点

使用 Browse 方法可返回父节点中包含的所有元数据节点。 以下示例浏览 SAP 系统上的前三个 RFC 功能组。 在此示例中, 客户端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.
IMetadataRetrievalNode[] nodes = client.Browse(Microsoft.Adapters.SAP.SAPAdapterConstants.MetadataConstants.RfcSection, 0, 3);

重要

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

搜索元数据节点

使用 Search 方法对父节点包含的节点执行搜索。 可以指定星号 (*) 通配符。 此字符匹配零个或多个字符。 以下示例演示了对包含字符串“BAPI”的所有 RFC 的搜索。 在此示例中, 客户端MetadataRetrievalClient 的实例。

// Search for all nodes that contain "BAPI" under the RFC node.
// The parameters are the parent node ID, the search expression, and
// the maximum number of nodes to return.
IMetadataRetrievalNode[] nodes = client.Search(Microsoft.Adapters.SAP.SAPAdapterConstants.MetadataConstants.RfcSection, "*BAPI*", int.MaxValue);

重要

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

检索操作 (WSDL) 元数据

使用 GetMetadata 方法检索一组操作节点的服务说明 (WSDL 文档) 。 以下示例检索BAPI_TRANSACTION_COMMIT RFC 的服务说明。 在此示例中, 客户端MetadataRetrievalClient 的实例。 请注意,操作节点的节点 ID 等效于该操作的消息操作。

// Get a WSDL document that specifies a contract that contains the
// BAPI_TRANSACTION_COMMIT operation.
MetadataRetrievalNode[] nodes = new MetadataRetrievalNode[1];
nodes[0] = new MetadataRetrievalNode();
nodes[0].NodeId = Microsoft.Adapters.SAP.SAPAdapterConstants.ActionConstants.RfcActionPrefix + "BAPI_TRANSACTION_COMMIT";
nodes[0].IsOperation = true;

System.Web.Services.Description.ServiceDescription description = client.GetMetadata(nodes);

重要

应仅在 GetMetadata 方法中指定操作节点。

使用 MetadataRetrievalClient

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

以下示例演示如何使用 MetadataRetrievalClient 从 SAP 适配器中浏览、搜索和检索元数据。

using System;
using System.Collections.Generic;
using System.Text;

using System.ServiceModel;
using Microsoft.Adapters.SAP;
using Microsoft.ServiceModel.Channels;

using System.Web.Services.Description;

namespace SapMetaDataBrowseClient
{
    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 browser = null;
            NodeWriter nodeWriter = new NodeWriter();

            try
            {
                // Create a binding
                SAPBinding binding = new SAPBinding();

                EndpointAddress address = new EndpointAddress("sap://Client=800;lang=EN@A/YourSAPHost/00");

                browser = new MetadataRetrievalClient(binding, address);
                browser.ClientCredentials.UserName.UserName = "YourUserName";
                browser.ClientCredentials.UserName.Password = "YourPassword";
                browser.Open();

                // Return the 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 = browser.Browse(MetadataRetrievalNode.Root.NodeId, 0, int.MaxValue);
                nodeWriter.Write("Browse results for the root node:", nodes);

                // Return first 3 RFC group nodes.
                nodes = browser.Browse(Microsoft.Adapters.SAP.SAPAdapterConstants.MetadataConstants.RfcSection, 0, 3);
                nodeWriter.Write(String.Format("Browse results for the first {1} nodes of {0}:", Microsoft.Adapters.SAP.SAPAdapterConstants.MetadataConstants.RfcSection, nodes.Length), nodes);

                // Search for first 3 nodes that begin with "BAPI_TRANSACTION" under the RFC node.
                // The parameters are the parent node ID, the search expression, and the maximum number
                // of nodes to return.
                nodes = browser.Search(Microsoft.Adapters.SAP.SAPAdapterConstants.MetadataConstants.RfcSection, "*BAPI_TRANSACTION*", 3);
                nodeWriter.Write(String.Format("Search results for \"*BAPI_TRANSACTION*\" under {0} node:", Microsoft.Adapters.SAP.SAPAdapterConstants.MetadataConstants.RfcSection), nodes);

                // Get a WSDL document that specifies a contract that contains the operations
                // found in the search and write it to a file.
                // You could explicitly specify operations as in the following:
                //    nodes[0] = new MetadataRetrievalNode();
                //    nodes[0].NodeId = Microsoft.Adapters.SAP.SAPAdapterConstants.ActionConstants.RfcActionPrefix + "BAPI_TRANSACTION_COMMIT";
                //    nodes[0].IsOperation = true;
                // Note that the operation NodeId is equivalent to the message action.
                System.Web.Services.Description.ServiceDescription description = browser.GetMetadata(nodes);
                description.Write("SampleContract.wsdl");
            }
            catch (Exception ex)
            {
                Console.WriteLine("Exception is: " + ex.Message);
            }
            finally
            {
                if (browser != null)
                {
                    if (browser.State == CommunicationState.Opened)
                        browser.Close();
                    else
                        browser.Abort();
                }
            }
        }
    }
}

下面显示了主机上此程序的输出。 可以看到为每个方法返回的元数据检索节点的结构。 程序还会编写一个 WSDL 文档,该文档描述由搜索返回到文件的节点组成的服务协定。 (对于大多数 SAP 安装,服务说明将包含BAPI_TRANSACTION_COMMIT和BAPI_TRANSACTION_ROLLBACK operations。)

Browse results for the root node:

NodeId = http://Microsoft.LobServices.Sap/2007/03/BAPISECTION/000001
        Direction   = Outbound
        IsOperation = False
        DisplayName = BAPI
        Description = BAPI
NodeId = http://Microsoft.LobServices.Sap/2007/03/IDOCSECTION/
        Direction   = Inbound, Outbound
        IsOperation = False
        DisplayName = IDOC
        Description = IDOC
NodeId = http://Microsoft.LobServices.Sap/2007/03/RFCSECTION/
        Direction   = Inbound, Outbound
        IsOperation = False
        DisplayName = RFC
        Description = RFC
NodeId = http://Microsoft.LobServices.Sap/2007/03/TRFCSECTION/
        Direction   = Inbound, Outbound
        IsOperation = False
        DisplayName = TRFC
        Description = TRFC

Browse results for the first 3 nodes of http://Microsoft.LobServices.Sap/2007/03
/RFCSECTION/:

NodeId = http://Microsoft.LobServices.Sap/2007/03/RFCGROUP/A
        Direction   = Inbound, Outbound
        IsOperation = False
        DisplayName = Asset Accounting
        Description = Asset Accounting
NodeId = http://Microsoft.LobServices.Sap/2007/03/RFCGROUP/S
        Direction   = Inbound, Outbound
        IsOperation = False
        DisplayName = Basis
        Description = Basis
NodeId = http://Microsoft.LobServices.Sap/2007/03/RFCGROUP/B
        Direction   = Inbound, Outbound
        IsOperation = False
        DisplayName = Business Information Warehouse
        Description = Business Information Warehouse

Search results for "*BAPI_TRANSACTION*" under http://Microsoft.LobServices.Sap/2
007/03/RFCSECTION/ node:

NodeId = http://Microsoft.LobServices.Sap/2007/03/Rfc/BAPI_TRANSACTION_COMMIT
        Direction   = Inbound, Outbound
        IsOperation = True
        DisplayName = BAPI_TRANSACTION_COMMIT
        Description = Execute external Commit when using BAPIs
NodeId = http://Microsoft.LobServices.Sap/2007/03/Rfc/BAPI_TRANSACTION_ROLLBACK
        Direction   = Inbound, Outbound
        IsOperation = True
        DisplayName = BAPI_TRANSACTION_ROLLBACK
        Description = Execute external Rollback when using BAPIs

使用 IMetadataRetrievalContract 通道

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

…
//Create a binding and endpoint address.
SAPBinding binding = new SAPBinding();
EndpointAddress address = new EndpointAddress("sap://Client=800;lang=EN@A/YourSAPHost/00");

//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 = "YourUserName";
factory.Credentials.UserName.Password = "YourPassword";
factory.Open();

//Obtain an IMetadataRetrievalContract channel from the factory.
IMetadataRetrievalContract channel = factory.CreateChannel();

//Perform a search using the channel.
MetadataRetrievalNode[] nodes = channel.Search(Microsoft.Adapters.SAP.SAPAdapterConstants.MetadataConstants.RfcSection, "BAPI_TRANSACTION*", int.MaxValue);
…

另请参阅

以编程方式从 SAP 检索元数据