使用 SAP 建立通道
在 WCF 通道模型中,您可以透過 WCF 通道與 Microsoft BizTalk Adapter for mySAP Business Suite 交換 SOAP 訊息,以叫用 SAP 系統上的作業,或從 SAP 系統接收訊息。
您可以使用 IRequestChannel 或 IOutputChannel 將訊息傳送至配接器, (輸出作業) 叫用作業
您會收到透過 IReplyChannel從 SAP 系統) 觸發的 (訊息。
本節中的主題提供如何建立及設定用於輸入和輸出作業的通道圖形的相關資訊。
建立輸出 (用戶端) 通道
您可以使用 IRequestChannel 或 IOutputChannel 來叫用 SAP 系統上的作業。 不論是哪一種情況,您都先使用適當的介面來建立 System.ServiceModel.ChannelFactory 。 接著,您可以使用處理站來建立通道。 建立通道之後,您可以使用通道來叫用介面卡上的作業。
若要建立和開啟輸出通道
使用端點和系結,為所需的通道圖形建立和初始化 ChannelFactory 的實例。 端點會指定 SAP 連線 URI,而系結是 SAPDBBinding的實例。 (開啟 channel factory.) 之前設定任何必要的系結屬性
使用 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 並新增 InputActionCollection ,其中包含您想要接收之作業的動作。 配接器會針對所有其他作業,將例外狀況傳回 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();