Invoke RFCs in SAP using the WCF Service Model

The Microsoft BizTalk Adapter for mySAP Business Suite surfaces RFCs on the SAP system as operations that can be invoked by a client program. In the WCF service model, these operations are invoked as methods of a generated WCF client class. You can use the Add Adapter Service Reference Visual Studio Plug-in to generate a WCF client class that contains methods for each RFC that you want to invoke in your code. The Add Adapter Service Reference Plug-in also generates .NET types to encapsulate the parameters and data types that are used by each RFC. You can then create an instance of this WCF client class and call its methods to invoke the target RFCs.

The following sections show you how to invoke RFCs on the SAP system by using the SAP adapter.

The WCF Client Class

The SAP adapter surfaces all RFC operations under a single service contract, "Rfc". This means that a single WCF client class, RfcClient, is created for all of the RFC operations that you want to invoke. Each target RFC is represented as a method of this class. In each method:

  • SAP export parameters are surfaced as out parameters

  • SAP changing parameters are surfaced as ref parameters.

  • Complex SAP types such as structures are surfaced as .NET classes with properties that correspond to the fields of the SAP type. These classes are defined in the following namespace: microsoft.lobservices.sap._2007._03.Types.Rfc.

    The following code shows part of the RfcClient class and the method that invokes SD_RFC_CUSTOMER_GET on the SAP system.

 [System.Diagnostics.DebuggerStepThroughAttribute()]  
[System.CodeDom.Compiler.GeneratedCodeAttribute("System.ServiceModel", "3.0.0.0")]  
public partial class RfcClient : System.ServiceModel.ClientBase<Rfc>, Rfc {  
  
    ...  
  
    /// <summary>The metadata for this RFC was generated using the RFC SDK.</summary>  
    /// <param name="KUNNR">Customer number</param>  
    /// <param name="NAME1">Name of the customer</param>  
    /// <param name="CUSTOMER_T">Table of the selected customers</param>  
    /// <returns></returns>  
    public void SD_RFC_CUSTOMER_GET(string KUNNR, string NAME1, ref microsoft.lobservices.sap._2007._03.Types.Rfc.RFCCUST[] CUSTOMER_T) {...}  
}  

How to Create an RFC Client Application

To create an RFC client application, perform the following steps.

To create an RFC client application

  1. Generate an RfcClient class. Use the Add Adapter Service Reference Visual Studio Plug-in or the ServiceModel Metadata Utility Tool (svcutil.exe) to generate an RfcClient class that targets the RFCs with which you want to work. For more information about how to generate a WCF client, see Generate a WCF Client or a WCF Service Contract for SAP Solution Artifacts.

  2. Create an instance of the RfcClient class generated in step 1, and specify a client binding. Specifying a client binding involves specifying the binding and endpoint address that the RfcClient will use. You can do this either imperatively in code or declaratively in configuration. For more information about how to specify a client binding, see Configure a Client Binding for the SAP System. The following code initializes the RfcClient from configuration and sets the credentials for the SAP system.

    RfcClient rfcClient = new RfcClient("SAPBinding_Rfc");  
    
    rfcClient.ClientCredentials.UserName.UserName = "YourUserName";  
    rfcClient.ClientCredentials.UserName.Password = "YourPassword";  
    
  3. Open the WCF client.

    rfcClient.Open();  
    
  4. Invoke methods on the RfcClient created in step 2 to perform operations on the SAP system. The following code invokes the SD_RFC_CUSTOMER_GET method of the RfcClient to invoke the RFC on the SAP system.

    microsoft.lobservices.sap._2007._03.Types.Rfc.RFCCUST[] customers =   
        new microsoft.lobservices.sap._2007._03.Types.Rfc.RFCCUST[0];  
    
    rfcClient.SD_RFC_CUSTOMER_GET(string.Empty, "AB*", ref customers);  
    
  5. Close the WCF client.

    rfcClient.Close();   
    

Example

The following is a complete code example that invokes SD_RFC_CUSTOMER_GET to return a list of customers with names that begin with "AB" and then writes the name and ID for each customer to the console. This example creates the RfcClient inside a using statement. There is no need to explicitly close the RfcClient; it will be closed when the execution path exits the context.

using System;  
using System.Collections.Generic;  
using System.Text;  
  
using System.ServiceModel;  
using Microsoft.ServiceModel.Channels;  
using Microsoft.Adapters.SAP;  
  
namespace SapRfcClientSM  
{  
    class Program  
    {  
        static void Main(string[] args)  
        {  
            try  
            {  
                // Create client from configuration  
                using (RfcClient rfcClient = new RfcClient("SAPBinding_Rfc"))  
                {  
  
                    rfcClient.ClientCredentials.UserName.UserName = "YourUserName";  
                    rfcClient.ClientCredentials.UserName.Password = "YourPassword";  
  
                    // Open client  
                    rfcClient.Open();  
  
                    microsoft.lobservices.sap._2007._03.Types.Rfc.RFCCUST[] customers = new microsoft.lobservices.sap._2007._03.Types.Rfc.RFCCUST[0];  
  
                    // Invoke SD_RFC_CUSTOMER_GET  
                    rfcClient.SD_RFC_CUSTOMER_GET(string.Empty, "AB*", ref customers);  
  
                    // Write the results to the console  
                    foreach (microsoft.lobservices.sap._2007._03.Types.Rfc.RFCCUST customer in customers)  
                    {  
                        Console.WriteLine("Customer Name = " + customer.NAME1);  
                        Console.WriteLine("         Id   = " + customer.KUNNR);  
                        Console.WriteLine();  
                    }  
                }  
            }  
            catch (Exception ex)  
            {  
                Console.WriteLine(ex.Message);  
                if (ex.InnerException != null)  
                    Console.WriteLine("Inner exception is :" + ex.InnerException);  
            }  
        }  
    }  
}  

See Also

Develop applications using the WCF Service Model
Message Schemas for RFC Operations