WCF サービス モデルを使用して IDOC を SAP に送信する

内部的には、microsoft BizTalk Adapter for mySAP Business Suite は、次の 2 つの RFC のいずれかを呼び出して IDOC を SAP システムに送信します。

  • バージョン 3 の IDOC のIDOC_INBOUND_ASYNCHRONOUS。

  • バージョン 2 の IDOC のINBOUND_IDOC_PROCESS。

    IDOC をアダプターに送信するには、適切な RFC (または tRFC) を呼び出します。ただし、次の 2 つの操作を使用して、IDOC をアダプターに送信することもできます。

  • SendIdoc は、IDOC ルート ノードの直下に表示されます。 SendIdoc 操作は、IDOC を文字列 (弱く型指定された) データとして SAP アダプターに送信します。

  • 送信 は、IDOC ごとに個別に表示されます。 Send 操作は、IDOC を厳密に型指定されたデータとして SAP アダプターに送信します。

    これらの操作により、IDOC データが SAP システムに送信される方法ではなく、アダプターに送信される方法が決まります。 アダプターは常に、適切な tRFC を使用して IDOC を SAP システムに送信します。

    SAP アダプターは IDOC を tRFC として送信するため、Send 操作と SendIdoc 操作の両方で、IDOC の確認 (コミット) に使用する GUID パラメーターが公開されます。 SAP アダプターは、この GUID を tRFC に関連付けられている SAP トランザクション ID (TID) と内部的にマップします。 IDOC は、次の 2 つの方法のいずれかで確認できます。

  • AutoConfirmSentIdocs バインド プロパティを使用します。 このバインディング プロパティが true に設定されている場合、アダプターは IDOC の送信に使用される tRFC を自動的に確認します。

  • RfcConfirmTransID を呼び出します。 この操作は、IDOC に関連付けられている GUID を使用して呼び出します。

    次のセクションでは、SendIdoc 操作と Send 操作を使用して IDOC を SAP システムに送信する方法について説明します。 IDOC を tRFC として送信する方法の詳細については、「 WCF サービス モデルを使用して SAP で tRFC を呼び出す」を参照してください。

WCF クライアント クラス

SendIdoc 操作

SAP アダプターは、IDOC を文字列形式で送信する 1 つの操作 (およびサービス コントラクト) を表示します。 サービス コントラクトの名前は "Idoc" で、WCF クライアント クラスは IdocClient です

このクライアントを使用して、任意の IDOC を SAP に送信できます。 これには、次の 2 つのパラメーターを受け取る 1 つのメソッド SendIdoc が含まれています。

  • idocData は、IDOC データを含む文字列です

  • guid は、SAP TID にマップされる GUID です。

    次のコードは、SendIdoc 操作用に生成される WCF クライアントを示しています。

[System.Diagnostics.DebuggerStepThroughAttribute()]  
[System.CodeDom.Compiler.GeneratedCodeAttribute("System.ServiceModel", "3.0.0.0")]  
public partial class IdocClient : System.ServiceModel.ClientBase<Idoc>, Idoc {  
  
    public void SendIdoc(string idocData, ref System.Nullable\<System.Guid\> guid) {…}  
}  

送信操作

送信操作では厳密に型指定されたデータが使用されるため、SAP アダプターは IDOC ごとに一意のサービス コントラクトを表示します。 このコントラクトに対して生成されるインターフェイス (および WCF クライアント クラス) の名前は、IDOC の種類、バージョン、リリース番号、CIM の種類 (該当する場合) に基づいています。 たとえば、ORDERS03.v3.620 IDOC の場合、インターフェイスの名前は "IdocORDERS03V3R620" で、WCF クライアント クラスは IdocORDERS03V3R620Client です

異なる種類の IDOC ごとに一意のクライアントを生成する必要があります。 このクライアントには、次の 2 つのパラメーターを受け取る 1 つのメソッド Send が含まれています。

  • idocData は、厳密に型指定された IDOC データを表すクラスです。

  • guid は、SAP TID にマップされる GUID の文字列表現です。

    次のコードは、ORDERS03.v3.620 IDOC に対して表示される送信操作用に生成される WCF クライアントを示しています。

[System.Diagnostics.DebuggerStepThroughAttribute()]  
[System.CodeDom.Compiler.GeneratedCodeAttribute("System.ServiceModel", "3.0.0.0")]  
public partial class IdocORDERS03V3R620Client : System.ServiceModel.ClientBase<IdocORDERS03V3R620>, IdocORDERS03V3R620 {  
  
    ...  
  
    public void Send(ORDERS03 idocData, ref string guid) { ... }  
}  

IDOC を送信するアプリケーションを作成する方法

IDOC を SAP システムに送信するには、次の手順を実行します。

IDOC を SAP システムに送信するには

  1. WCF クライアント クラスを生成します。 アダプター サービス参照の追加 Visual Studio プラグインまたは ServiceModel メタデータ ユーティリティ ツール (svcutil.exe) を使用して、作業する IDOC を対象とする WCF クライアント クラスを生成します。 WCF クライアントを生成する方法の詳細については、「WCF クライアントの生成」または「SAP ソリューション成果物の WCF サービス コントラクトの生成」を参照してください。 IDOC を明示的に確認する場合は、RfcConfirmTransID 操作用の WCF クライアントを生成してください。 操作は、次のノードにあります。

    • SendIdoc 操作。 IDOC ノードのすぐ下。

    • 送信操作。 ターゲット IDOC の種類、バージョン、リリース番号に対応するノードの下。

    • RfcConfirmTransID 操作。 TRFC ノードのすぐ下。

  2. 手順 1 で生成された WCF クライアント クラスのインスタンスを作成し、クライアント バインドを指定します。 クライアント バインドを指定するには、WCF クライアントが使用するバインディングとエンドポイント アドレスを指定する必要があります。 これは、コード内で命令的に行うか、構成で宣言的に行うことができます。 クライアント バインドを指定する方法の詳細については、「 SAP システムのクライアント バインドを構成する」を参照してください。 次のコードでは、構成から IdocClient (送信操作用) を初期化し、SAP システムの資格情報を設定します。

    SAPBinding binding = new SAPBinding();  
    
    // Set endpoint address  
    EndpointAddress endpointAddress = new EndpointAddress("sap://CLIENT=800;LANG=EN;@a/YourSAPHost/00?RfcSdkTrace=False&AbapDebug=False&UseSapGui=Without");  
    
    // Create client and set credentials  
    idocClient = new IdocClient(binding, endpointAddress);  
    idocClient.ClientCredentials.UserName.UserName = "YourUserName";  
    idocClient.ClientCredentials.UserName.Password = "YourPassword";;  
    
  3. アダプターが IDOC を送信した後に SAP システム上の tRFC を確認する場合は、 AutoConfirmSentIdocs バインディング プロパティを true に設定 します。 WCF クライアントを開く前に、これを行う必要があります。

    // Set AutoConfirmSentIdocs property to true  
    binding.AutoConfirmSentIdocs = true;  
    
  4. WCF クライアントを開きます。

    idocClient.Open();  
    
  5. 手順 2 で作成した WCF クライアントで適切なメソッドを呼び出して、IDOC を SAP システムに送信します。 GUID を含む変数、または guid パラメーターにnull に設定されている変数を渡すことができます。 GUID を指定しない場合、SAP アダプターは操作用の GUID を生成します (guidref パラメーターです)。 次のコードは、ファイルから IDOC (文字列形式) を読み取り、SendIdoc 操作を使用して SAP システムに送信します。

    // Read IDOC into string variable  
    using (StreamReader reader = new StreamReader("ORDERS03.txt"))  
    {  
    idocData = reader.ReadToEnd();  
    }  
    
    //Get a new GUID to pass to SendIdoc. You can also assign a null  
    //value to have the adapter generate a GUID.  
    adapterTxGuid = Guid.NewGuid();  
    
    //Invoke SendIdoc on the client to send the IDOC to the SAP system  
    idocClient.SendIdoc(idocData, ref adapterTxGuid);  
    
  6. AutoConfirmSentIdocs バインディング プロパティを true に設定しなかった場合 (手順 3) は、SAP システムで tRFC を確認する必要があります。 これを行うには、TrfcClientRfcConfirmTransID メソッドを呼び出す必要があります (作成は表示されません)。 パラメーターに手順 4 で返される GUID を指定します。

    trfcClient.RfcConfirmTransID(adapterTxGuid);  
    
  7. WCF クライアントの使用が完了したら (IDOC の送信が完了した後で) WCF クライアント (および TrfcClient を使用する場合) を閉じます。

    idocClient.Close();   
    

次の例では、SendIdoc メソッドを使用して IDOC を SAP システムに送信します。 IDOC は、ORDERS03.txt ファイルから読み取られます。 このファイルには、ORDERS03が含まれています。V3.620 IDOC と はサンプルに含まれています。ただし、SendIdoc 操作を使用して任意の IDOC を送信できます。

using System;  
using System.Collections.Generic;  
using System.Text;  
  
using System.IO;  
  
// Add WCF, WCF LOB Adapter SDK, and SAP adapter namepaces  
using System.ServiceModel;  
using Microsoft.Adapters.SAP;  
using Microsoft.ServiceModel.Channels;  
  
// Include this namespace for WCF LOB Adapter SDK and SAP exceptions  
using Microsoft.ServiceModel.Channels.Common;  
  
// This example sends a flat IDOC to the SAP system by using the SendIdoc operation.  
namespace SapIdocStringClientSM  
{  
    class Program  
    {  
        static void Main(string[] args)  
        {  
            // variable for the IDOC client  
            IdocClient idocClient = null;  
  
            Console.WriteLine("IDOC string client sample started");  
            try  
            {  
                // Variable for the GUID  
                System.Nullable<System.Guid> adapterTxGuid;  
                // string to hold the Idoc data  
                string idocData;  
                // string to hold the SAP transaction ID (TID)  
                string sapTxId;  
  
                // The client can be configured from app.config, but it is   
                // explicitly configured here for demonstration.  
                // set AutoConfirmSentIdocs property to true  
                SAPBinding binding = new SAPBinding();  
                binding.AutoConfirmSentIdocs = true;  
  
                // Set endpoint address  
                EndpointAddress endpointAddress = new EndpointAddress("sap://CLIENT=800;LANG=EN;@a/YourSAPServer/00?RfcSdkTrace=False&AbapDebug=False&UseSapGui=Without");  
  
                // Create client and set credentials  
                idocClient = new IdocClient(binding, endpointAddress);  
                idocClient.ClientCredentials.UserName.UserName = "YourUserName";  
                idocClient.ClientCredentials.UserName.Password = "YourPassword";  
  
                // Open the client and send the Idoc  
                idocClient.Open();  
  
                // Read IDOC into string variable  
                using (StreamReader reader = new StreamReader("ORDERS03.txt"))  
                {  
                    idocData = reader.ReadToEnd();  
                }  
  
                //Get a new GUID to pass to SendIdoc. You can also assign a null.  
                //value to have the adapter generate a GUID.  
                adapterTxGuid = Guid.NewGuid();  
  
                //Invoke SendIdoc on the client to send the IDOC to the SAP system.  
                idocClient.SendIdoc(idocData, ref adapterTxGuid);  
  
                // The AutoConfirmSentIdocs binding property is set to true, so there is no need to  
                // confirm the IDOC. If this property is not set to true, you must call the   
                // RfcConfirmTransID method of a TrfcClient with adapterTxGuid to   
                // confirm the transaction on the SAP system.   
  
                // Get SAP tx id from GUID  
                sapTxId = SAPAdapterUtilities.ConvertGuidToTid((Guid) adapterTxGuid);  
  
                Console.WriteLine("IDOC sent");  
                Console.WriteLine("The SAP Transaction Id is : " + sapTxId);  
  
            catch (Exception ex)  
            {  
                Console.WriteLine("Exception is: " + ex.Message);  
                if (ex.InnerException != null)  
                {  
                    Console.WriteLine("Inner Exception is: " + ex.InnerException.Message);  
                }  
            }  
            finally  
            {  
                // Close the IDOC client  
                if (idocClient != null)  
                {  
                    if (idocClient.State == CommunicationState.Opened)  
                        idocClient.Close();  
                    else  
                        idocClient.Abort();  
                }  
            }  
  
        }  
    }  
}  

参照

WCF サービス モデルを使用してアプリケーションを開発する