staticRFCServers=newRFCServer();staticSqlConnectionSQLConn=null;staticvoidMain(string[]args){RFCServers=newRFCServer();s.GatewayHost="hamlet";s.GatewayService="sapgw11";s.ProgramID="ERPTEST";s.IncomingCall+=newERPConnect.RFCServer.OnIncomingCall(s_IncomingCall);s.TRFCCheckTID+=newRFCServer.OnTRFCCheckTID(s_TRFCCheckTID);s.TRFCCommit+=newRFCServer.OnTRFCCommit(s_TRFCCommit);s.TRFCConfirm+=newRFCServer.OnTRFCConfirm(s_TRFCConfirm);s.TRFCRollback+=newRFCServer.OnTRFCRollback(s_TRFCRollback);// Add and register function moduleRFCServerFunctionf=s.RegisteredFunctions.Add("Z_TRFC");f.Imports.Add("STRING1",RFCTYPE.CHAR,10);f.Imports.Add("STRING2",RFCTYPE.CHAR,10);// start servers.Start();Console.Write("Server started. Please press any key to stop");Console.ReadLine();s.Stop();}
The following events are fired from the client within the server program:
staticvoids_TRFCCheckTID(RFCServerSender,stringTID,refboolOK){DataTableDtTID=GetDataTableBySQL("Select * from TransactionID whereTID='" + TID + "'andXExecuted='False'");if(DtTID.Rows.Count>0){Console.WriteLine("TransactionalID "+TID+" is already executed");OK=false;}else{Console.WriteLine("TRFCCheckTID is OK");InsertUpdateBySQL("Insert TransactionID (TID) Values ('"+TID+"')");OK=true;}}staticvoids_IncomingCall(RFCServerSender,RFCServerFunctionCalledFunction){if(CalledFunction.FunctionName=="Z_TRFC"){stringstring1=CalledFunction.Imports["STRING1"].ToString();stringstring2=CalledFunction.Imports["STRING2"].ToString();stringstring3=string1+" "+string2;InsertUpdateBySQL("Update TransactionID set Result = '"+string3+"',XExecuted='True'whereTID='" + Sender.LastTID.ToString() + "'");}elsethrownewERPConnect.ERPException("Function unknown");}staticvoids_TRFCCommit(RFCServerSender,stringTID,refboolOK){Console.WriteLine("TransactionalID "+TID+" is committed");InsertUpdateBySQL("Update TransactionID set XCommit = 'True' where TID = '"+TID+"'");OK=false;}staticvoids_TRFCConfirm(RFCServerSender,stringTID,refboolOK){Console.WriteLine("TransactionalID "+TID+" was confirmed");InsertUpdateBySQL("Update TransactionID set XConfirm = 'True' where TID = '"+TID+"'");OK=true;}staticvoids_TRFCRollback(RFCServerSender,stringTID){Console.WriteLine("TransactionalID "+TID+" is rolled back");InsertUpdateBySQL("Update TransactionID set XRolledBack = 'True' where TID = '"+TID+"'");}
Function Call in ABAP
In the ABAP program the RFC function is called with the ABAP function CALL FUNCTION “Z_TRFC” IN BACKGROUND TASK for the asynchronous execution:
The ABAP statement is not executed immediately, but with the EXPORTING and/or TABLES content stored in a SAP database table.
COMMIT WORK starts the processing of the function
.NET fires the event TRFCCheckTID.
The transaction ID in the depicted example is checked to see if it can be found in the table on the SQL server.
If the transaction ID is missing, or available but not executed yet, TRFCCheckTID returns True.
The IncomingCall event is fired.
If the processing is successful, the Commit event is fired, which confirms the execution.
The Confirm event is fired to tidy up the transaction management.
With tRFC the remote function is processed, even if at the time of the call the remote partner is not available or the connection was lost.
Contrary to the synchronous RFC you can combine different functions for the remote processing into a logical unit or work (SAP-LUW). Rollback on the whole unit is then possible.
Tip
Tools for monitoring and administration are available with SAP transaction SM58.
Table on the SQL Server
The depicted example requires a table on the SQL server (TransactionID). The table includes:
4 flags (XCommit, XConfirm, XRollback and XExecuted)