NWRfcServer

NWRfcServer allows you to implement RFC Server functionality in your program, using the new NetWeaver (NWRFC) RFC library. Using this class requires SAP GUI 7.50 or later, or a standalone installation of the NWRFC SDK.

See RFC Servers for a detailed description on how to use this object. NWRfcServer and the (deprecated) RfcServer support exactly the same properties, methods and events.

This object was introduced with RfcConnector 3.0

Properties

Name Type Description
Error bool indicates an error (in which case the ErrorInfo property will contain more information)
ErrorInfo RfcServerErrorInfo error information
LicenseData LicenseData license information
ProgramID string Program ID under which the server registers at the SAP system
GatewayHost string hostname of SAP system (e.g. 'mysaphost.mycompany.com')
GatewayService string service name (this is always "sapgw" + system number, e.g. "sapgw01" for SYSNR=01)
Option Option Allows you to set and retrieve additional options, such as trace file location

Methods

Method Serve

After calling this method, the server will wait for incoming requests (=function calls). Since the listener is running in another thread, this method will return immediately after the server has been registered at the SAP system, but before any requests are received.

If the server could not be started (for example, if the SAP system is unreachable) the Error property will be set to true and the ErrorInfo property will contain additional information about the error.

The listener thread will be stopped when the server encounters an error (see ServerError event for details), when the Server.Shutdown() method is called or when the server object is disposed.

Method Shutdown

when this method is called, the listener thread will be stopped and the server unregisters itself at the SAP system.

Method InstallFunction

Parameter Type Description
type FunctionCall prototype of the function to install

Installs a function prototype, which can be either retrieved by Session.ImportCall(), loaded from XML (with FunctionCall.FromXML()) or constructed manually.

It is necessary for the RfcServer to know the prototype of the served functions before requests are received, because otherwise the parameters could not be decoded properly.

Events

Event: Logon

Parameter Type Description
LogonInfo RfcServerLogonInfo logon information

This event is fired to authenticate a caller before the IncomingCall event. Applications can subscribe to this event and use the information in LogonInfo to decide whether the request should be allowed or not. Applications do not need to subscribe to this event. If there are no subscribers to the Logon event, all requests will be allowed. To allow the request, set the RequestAllowed property of the LogonInfo object to true.

Event: IncomingCall

Parameter Type Description
call FunctionCall function call object

This event is fired when the server receives a function call, and the request is not denied in the event handler for Logon events.

The call parameters can be retrieved through the Importing, Changing and Tables properties of the FunctionCall object. Changes made to the Exporting, Changing and Tables parameters will be transported back to the caller.

To raise an exception, applications can use the RaiseException method of the function call. If an exception is raised, only the Tables parameter, but not the Changing and Exporting parameters are transported back to the caller. If the server receives a call to a function which has not been installed previously through the InstallFunction method, the Importing, Exporting, Changing and Tables parameters of the FunctionCall object are empty, as the server has no means to determine the structure of the data received from the caller.

Event: ServerError

Parameter Type Description
ErrorInfo RfcServerErrorInfo error info object

This event is fired when the listener thread is running and an error (or RFC_CLOSE from the gateway) is encountered. It is, however, not fired when the server could not be started or registered in the first place (to handle this situation, check the Error property of the RfcServer instance after calling RfcServer.Serve()).

By setting the Restart property of the ErrorInfo object to true, the application can indicate that the listener thread should be restarted, rather than terminated. If the Restart property is set to false, or if there are no subscribers to the ServerError event, the listener thread will terminate.

The recommended implementation is to subscribe to this event and set the Restart property to true, instead of creating new server instances. Creating new server instances in reaction to ServerError events may lead to resource and memory leaks and is generally not recommended.

Event: TransactionCheck

Parameter Type Description
TransactionInfo RfcTransactionInfo transaction information

The TransactionCheck event is fired after an inbound transactional (tRFC) call is received, and before the respective function module is called.

This event MUST be handled by Servers which support tRFC and such servers MUST set the Status field of the TransactionInfo property to one of the following values:

0transaction ID stored, transaction can be started
1transaction already done, skip the request
-1cannot lock transaction, internal error
By definition of tRFC it is allowed for the caller to call a tRFC function multiple times with the same transaction ID, and it is the responsibility of the tRFC server to make sure the function call is executed exactly once regardless of how often it is called (with the same TID). Therefore, consumers of this event are expected to
  • persist the transaction ID they receive and
  • determine whether the respective transaction has already been executed successfully, and, if yes, return 1 to indicate the call should not be repeated
  • return 0 when the transaction has not yet been started, or failed in the past and can be repeated right now
  • return -1 to indicate the transaction cannot be started right now (for example, because the transaction is currently being processed in another thread)

Event: TransactionCommit

Parameter Type Description
TransactionInfo RfcTransactionInfo transaction information

This event is fired after the inbound tRFC has been processed successfully and indicates that the server should persist the received information now (e.g. by calling COMMIT WORK when using a transactional database).

This event CAN be handled by Servers which support tRFC. There is no return value for this event, so Servers do not need to set the Status field.

Event: TransactionRollback

Parameter Type Description
TransactionInfo RfcTransactionInfo transaction information

This event is fired after the inbound tRFC has failed. It can be used to roll back changes to persistant storage, if necessary (e.g. by calling ROLLBACK WORK when using a transactional database).

This event CAN be handled by Servers which support tRFC. There is no return value for this event, so Servers do not need to set the Status field.

Event: TransactionConfirm

Parameter Type Description
TransactionInfo RfcTransactionInfo transaction information

This event is fired to confirm a transaction after it has been committed, and indicates there will be no further calls with the given TID. It can be used to clean up the persistent store of transaction IDs (which is filled in the TransactionCheck event).

This event CAN be handled by Servers which support tRFC. There is no return value for this event, so Servers do not need to set the Status field.