Determine SAP connection parameters

To connect to SAP you need to instantiate a Session instance. Rfc Connector supports three different session types:

NwRfcSession

Use NwRfcSession to create a session using the RFC protocol. The RFC protocol is supported by any SAP backend and allows to use the full range of features, including Single Sign On (SSO) and „visual“ RFC (e.g. showing a dynpro as part of the function call). NwRfcSession is the recommended way of connecting to SAP for new projects.

However, to use NWRFC („NetWeaver RFC“), you need the proprietary NWRFC library from SAP. It gets automatically installed with SAP GUI 7.50 and later. If you do not have SAP GUI installed, you can also download and install the „Netweaver RFC SDK“ from the SAP Service Marketplace (login required).

Before you can connect to the SAP system with NwRfcSession, you need to configure the connection string, which tells NwRfcSession how to find the backend system. Ideally, the system you want to connect to is already configured in SAP Logon. In this case, you can just re-use the existing connection data with SAPLOGON_ID:

// create a new NwRfcSession instance
NwRfcSession session = new NwRfcSession();
// 'ABC' is the id of the system in SAP Logon
session.RfcSystemData.ConnectString = "SAPLOGON_ID=ABC";

If you do not have an entry for your system in SAP Logon, ask your SAP system administrator for the correct connection string to use, or refer to Knowledgebase Entry #0008.

SOAPSession

Unlike NwRfcSession, SOAPSession does not require any additional libraries. Since SOAP uses http(s) as transport protocol, you need to know the hostname and port number of the SAP system.

// create a new NwRfcSession instance
SOAPSession session = new SOAPSession();

session.HttpSystemData.Protocol = "http"; // or "https"
session.HttpSystemData.Host = "myhost";
session.HttpSystemData.Port = 8042; // SYSNR=42

Note: The port number for http is usually SYSNR+8000. To be able to use SOAP, you might need to enable the service /sap/bc/soap/rfc in transaction SICF.

RfcSession (deprecated)

The connection string for RfcSession is the same as for NwRfcSession. However, please keep in mind that classic RFC is not supported any more by SAP, so you should only use this in legacy environments where an upgrade is not possible.

Logon Data

The SAP system won‘t let you connect without proper authorization, which means you need to set at least the SAP client (a 3-digit number), a user and a password. You can also set the language in which you want to receive translateable texts such as error messages:

session.LogonData.Client = "001";
session.LogonData.User = "DEVELOPER";
session.LogonData.Password = "***";
session.LogonData.Language = "DE";

Tracing Calls

You can trace any activity of Rfc Connector by defining a trace file. This is useful for getting more information about an error.

session.set_Option("trace.file", "C:\\tracefile.txt");

Note: While useful for development, tracing every call in production will cause significant overhead and may degrade performance. It is recommended to turn off tracing in production, and only enable it when necessary to analyze a problem.

Connect the session to the backend

To connect to the backend, just call the session.Connect() after setting up the connection and logon data:

try {
    session.Connect();
} catch (COMException ex) {
    // something went wrong. Check the ErrorInfo property of the Session
    // instance to get more information about the error.
}

After you have set up the connection, you can call a BAPI or function module or read data from SAP tables.