在 WCF 通道模型中,您可以通过 WCF 通道与 mySAP Business Suite 的 Microsoft BizTalk 适配器交换 SOAP 消息,以在 SAP 系统上调用操作或从 SAP 系统接收消息。
- 通过使用 IRequestChannel 或 IOutputChannel,将消息发送到适配器,以调用操作(出站操作)。 
- 你通过 IReplyChannel 接收从 SAP 系统触发的消息。 - 本节中的主题提供了有关如何创建和配置用于入站和出站操作的通道形状的信息。 
创建出站(客户端)通道
可以使用 IRequestChannel 或 IOutputChannel 调用 SAP 系统上的操作。 在任一情况下,都首先使用相应的接口创建 System.ServiceModel.ChannelFactory 。 然后使用工厂创建通道。 创建信道后,可以使用它来调用适配器上的操作。
创建和打开出站通道
- 使用终结点和绑定为所需通道形状创建和初始化 ChannelFactory 实例。 终结点指定 SAP 连接 URI,绑定是 SAPDBBinding 的实例。 (在打开通道工厂之前设置所需的任何绑定属性。 
- 使用 ClientCredentials 属性为通道工厂提供 SAP 凭据。 
- 打开通道工厂。 
- 通过在通道工厂上调用 CreateChannel 方法获取通道的实例。 
- 打开通道。 - 可以在代码或配置中指定绑定和终结点地址。 
在代码中指定绑定和终结点地址
下面的代码示例演示如何通过在代码中指定绑定和终结点地址来创建 IRequestChannel 。 创建 IOutputChannel 的代码相同,只是必须为 ChannelFactory 和通道类型指定 IOutputChannel 接口。
// Create binding -- set binding properties before you open the factory.  
SAPBinding sapBinding = new SAPBinding();  
  
// Create address.  
EndpointAddress sapAddress = new EndpointAddress("sap://Client=800;lang=EN@A/YourSAPHost/00");  
  
// Create channel factory from binding and address.  
ChannelFactory<IRequestChannel> factory =   
    new ChannelFactory<IRequestChannel>(sapBinding, sapAddress);  
  
// Specify credentials.   
factory.Credentials.UserName.UserName = "YourUserName";  
factory.Credentials.UserName.Password = "YourPassword";  
  
// Open the factory  
factory.Open();  
  
// Get channel and open it.  
IRequestChannel channel = factory.CreateChannel();  
channel.Open();  
在配置中指定绑定和终结点地址
下面的代码示例演示如何从配置中指定的客户端终结点创建通道工厂。
// Create channel factory from configuration.  
ChannelFactory<IRequestChannel> factory =  
new ChannelFactory<IRequestChannel>("MyRequestChannel");  
  
// Specify credentials.  
factory.Credentials.UserName.UserName = "YourUserName";  
factory.Credentials.UserName.Password = "YourPassword";  
  
// Open the factory.  
factory.Open();  
  
// Get a channel and open it.  
IRequestChannel channel = factory.CreateChannel();  
channel.Open();  
配置设置
以下代码显示了用于上述示例的配置设置。 客户端终结点的协定必须是“System.ServiceModel.Channels.IRequestChannel”或“System.ServiceModel.Channels.IRequestChannel”,具体取决于要创建的通道形状的类型。
<?xml version="1.0" encoding="utf-8"?>  
<configuration xmlns="http://schemas.microsoft.com/.NetConfiguration/v2.0">  
    <system.serviceModel>  
        <bindings>  
            <sapBinding>  
                <binding name="SAPBinding" closeTimeout="00:01:00" openTimeout="00:01:00"  
                    receiveTimeout="00:10:00" sendTimeout="00:01:00" enableBizTalkCompatibilityMode="false"  
                    receiveIdocFormat="Typed" enableSafeTyping="false" generateFlatFileCompatibleIdocSchema="true"  
                    maxConnectionsPerSystem="50" enableConnectionPooling="true"  
                    idleConnectionTimeout="00:15:00" flatFileSegmentIndicator="SegmentDefinition"  
                    enablePerformanceCounters="false" autoConfirmSentIdocs="false"  
                    acceptCredentialsInUri="false"  
                    padReceivedIdocWithSpaces="false" sncLibrary="" sncPartnerName="" />  
            </sapBinding>  
        </bindings>  
        <client>  
            <endpoint address="sap://CLIENT=800;LANG=EN;@a/ADAPSAP47/00?RfcSdkTrace=False&AbapDebug=False"  
                binding="sapBinding" bindingConfiguration="SAPBinding" contract="System.ServiceModel.Channels.IRequestChannel"  
                name="MyRequestChannel" />  
        </client>  
    </system.serviceModel>  
</configuration>  
创建入站(服务)通道
通过在 SAPBinding 实例上设置绑定属性,将适配器配置为从 SAP 系统接收入站消息。 然后,使用此绑定构建一个通道侦听器,从中可以获取 IReplyChannel 通道以接收来自适配器的操作。
创建并打开 IReplyChannel 以接收数据更改的通知
- 创建 SAPBinding 实例。 
- 设置您要接收的操作所需的任何绑定属性。 请务必设置 AcceptCredentialsInUri 绑定属性。 
- 创建 BindingParameterCollection 并添加一个 InboundActionCollection,其中包含要接收的操作。 对于所有其他操作,适配器将返回异常给 SAP 系统。 此步骤是可选的。 有关详细信息,请参阅 使用 WCF 通道模型从 SAP 系统接收入站操作。 
- 通过在 SAPBinding 上调用 BuildChannelListener<IReplyChannel> 方法创建通道侦听器。 将 SAP 连接 URI 指定为此方法的参数之一。 连接 URI 必须包含 SAP 系统上 RFC 目标的参数。 有关 SAP 连接 URI 的详细信息,请参阅“ 创建 SAP 系统连接 URI”。 如果在步骤 3 中创建 BindingParameterCollection ,则创建通道侦听器时也会指定此项。 
- 打开侦听器。 
- 通过在侦听器上调用 AcceptChannel 方法获取 IReplyChannel 通道。 
- 打开通道。 - 以下代码演示了如何创建通道监听器并获取 IReplyChannel 以接收来自适配器的操作。 
// Create a binding and specify any binding properties required  
// for the opreations you want to receive  
SAPBinding binding = new SAPBinding();  
  
// Set AcceptCredentialsInUri because the URI must contain credentials.  
binding.AcceptCredentialsInUri = true;  
  
// Create an InboundActionCollection and add the message actions to listen for,  
// only the actions added to the InboundActionCollection are received on the channel.  
// In this case a single action is specified: http://Microsoft.LobServices.Sap/2007/03/Rfc/Z_RFC_MKD_ADD  
InboundActionCollection actions = new InboundActionCollection(listeneraddress);  
actions.Add("http://Microsoft.LobServices.Sap/2007/03/Rfc/Z_RFC_MKD_ADD");  
  
// Create a BindingParameterCollection and add the InboundActionCollection  
BindingParameterCollection bpcol = new BindingParameterCollection();  
bpcol.Add(actions);  
  
// Create the channel listener by specifying  
// the binding parameter collection (to filter for the Z_RFC_MKD_ADD action) and   
// a connection URI that contains listener parameters.  
Uri listeneraddress =  
new Uri("sap://User=YourUserName;Passwd=YourPassword;Client=800;Lang=EN;@a/YourSAPHost/00?ListenerGwServ=SAPGATEWAY&ListenerGwHost=YourSAPHost&ListenerProgramId=SAPAdapter");  
listener = binding.BuildChannelListener<IReplyChannel>(connectionUri, new BindingParameterCollection());  
listener.Open();  
  
// Get a channel from the listener and open it.  
channel = listener.AcceptChannel();  
channel.Open();