This section shows how to create, register and use RFC server functions.
Register the RFC Server Function
Initialize an RFCServer object. Provide the gateway host, the gateway service and the program ID to register on the SAP gateway. The program ID must be available as a registered destination in SAP, see Set Up an RFC Destination.
Register an RFCServerFunction object using RegisteredFunctions.Add. An RFCServer object can hold more than one RFCServerFunction object.
Add Imports and Exports. They are handled the same way as when calling RFC functions as a client.
When an ABAP program calls the function, the event IncomingCall is triggered. When using C#, the event call-back must be defined in a separate line of code.
Start the server using Start.
Note
If the RFC destination is set to Unicode (SAP transaction code SM59), the property IsUnicode of the RFCServer object must be set to true.
In the following sample applications a simple RFC server function is created that allows ABAP programs to add numbers.
usingERPConnect;staticvoidMain(string[]args){RFCServers=newRFCServer();s.GatewayHost="sap-erp-as05.example.com";s.GatewayService="sapgw11";s.ProgramID="ERPTEST";s.IncomingCall+=newERPConnect.RFCServer.OnIncomingCall(s_IncomingCall);RFCServerFunctionf=s.RegisteredFunctions.Add("Z_ADD");f.Imports.Add("NUMBER1",RFCTYPE.INT);f.Imports.Add("NUMBER2",RFCTYPE.INT);f.Exports.Add("RES",RFCTYPE.INT);s.Start();Console.Write("Server is running. Press any key to exit.");Console.ReadLine();}
Two input parameters (NUMBER1 and NUMBER2) are added in a new function (Z_ADD). The result (RES) is passed back to the calling ABAP program, see Call RFC Server Functions in ABAP.
usingERPConnect;staticvoidMain(string[]args){staticRFCServers=newRFCServer();s.GatewayHost="SAPServer";s.GatewayService="sapgw00";s.ProgramID="ERPConnectTEST";s.IncomingCall+=newRFCServer.OnIncomingCall(s_IncomingCall);RFCServerFunctionf=s.RegisteredFunctions.Add("Z_ADD_2");f.Exports.Add("RES",RFCTYPE.INT);RFCTablenumbertable=f.Tables.Add("NUMBERS");numbertable.Columns.Add("NUMB",10,0,RFCTYPE.NUM);s.Start();Console.WriteLine("Press Enter to quit");Console.ReadLine();}
A table (NUMBERS) with a column (NUMB) is added to a table collection using Add(). The result (RES) is passed back to the calling ABAP program, see Call RFC Server Functions in ABAP.
usingERPConnect;staticvoidMain(string[]args){staticRFCServers=newRFCServer();s.GatewayHost="hamlet";s.GatewayService="sapgw11";s.ProgramID="ERPTEST";s.IncomingCall+=newRFCServer.OnIncomingCall(s_IncomingCall);RFCServerFunctionf=s.RegisteredFunctions.Add("Z_ADD_3");RFCTableColumnCollectionColumns=newRFCTableColumnCollection();Columns.Add("NUMB",10,0,RFCTYPE.NUM);Columns.Add("NUMB2",10,0,RFCTYPE.NUM);f.Imports.Add("NUMBERS",Columns);f.Exports.Add("RES",RFCTYPE.INT);s.Start();Console.WriteLine("Press Enter to quit");Console.ReadLine();}
A structure (NUMBERS) with 2 columns (NUMB1 and NUMB2) is added to a column collection via RFCTableColumnCollection(). The result (RES) is passed back to the calling ABAP program, see Call RFC Server Functions in ABAP.
usingERPConnect;staticvoidMain(string[]args){staticRFCServers=newRFCServer();s.GatewayHost="hamlet";s.GatewayService="sapgw11";s.ProgramID="ERPTEST";s.IncomingCall+=newRFCServer.OnIncomingCall(s_IncomingCall);RFCServerFunctionf=s.RegisteredFunctions.Add("Z_ADD_4");RFCTableColumnCollectionColumns=newRFCTableColumnCollection();Columns.Add("NUMB1",10,0,RFCTYPE.NUM);Columns.Add("NUMB2",10,0,RFCTYPE.NUM);RFCTableColumnCollectionEXColumns=newRFCTableColumnCollection();EXColumns.Add("NUMB1",10,0,RFCTYPE.NUM);EXColumns.Add("NUMB2",10,0,RFCTYPE.NUM);f.Imports.Add("NUMBERS",Columns);f.Exports.Add("EXNUMBERS",EXColumns);s.Start();Console.WriteLine("Press Enter to quit");Console.ReadLine();}
Two structures (NUMBERS and EXNUMBERS) with 2 columns (NUMB1 and NUMB2) are added to column collections via RFCTableColumnCollection(). One structure (NUMBERS) is used for import and the other (EXNUMBERS) is passed back to the calling ABAP program, see Call RFC Server Functions in ABAP.
Handle Incoming Calls
The following code shows how the IncomingCall event is handled:
The values from the IMPORT collection (numberstruc) are set in variables. Then different values are added to them and are written back to the EXPORT collection (EXnumbers).
The import parameters are passed by the calling SAP system. The export parameters are passed back to SAP.
Call RFC Server Functions in ABAP
The following ABAP code is used to call the new function Z_ADD in the remote destination ERPTEST.
REPORTz_add_testDATAresult TYPE i.CALL FUNCTION'Z_ADD'DESTINATION'ERPTEST'EXPORTINGnumber1=26number2=25IMPORTINGres=result.WRITE:/'Result: ',result.
The two numbers 26 and 25 are passed, and the result 51 is passed back.
The two numbers 1 and 2 are passed, and the result 3 is passed back.
REPORTZADDTEST4..DATAnumbsLIKE zaddstruc2.DATAexnumberslike zaddstruc2.numbs-numb1='1'.numbs-numb2='2'.CALL FUNCTION'Z_ADD_4'DESTINATION'ERPTEST'EXPORTINGNUMBERS=numbsIMPORTINGEXNUMBERS=exnumbers.WRITE:/'First result is (added 1): ',exnumbers-numb1.WRITE:/'Second result is (added 98): ',exnumbers-numb2.
The two numbers 1 and 2 are passed, and the results 2 and 100 are passed back.