Skip to content

Repository files navigation

SharpSapRfc

What is SharpSapRfc?

SAP NCo 3 is a library developed by SAP that allows .Net Developers connect to SAP through very easy to use API. Sharp SAP RFC is a top-level library that makes it even easier to call remote functions on SAP systems. Just read the examples bellow and you will see how powerfull it is.

Instead of this code:

RfcDestinationdestination=RfcDestinationManager.GetDestination("TST");RfcRepositoryrepository=destination.Repository;IRfcFunctionfunction=repository.CreateFunction("Z_SSRT_SUM");function.SetValue("i_num1",2);function.SetValue("i_num2",4);function.Invoke(destination);intresult=function.GetInt("e_result");

You can write this:

using(SapRfcConnectionconn=newPlainSapRfcConnection("TST")){varresult=conn.ExecuteFunction("Z_SSRT_SUM",new{i_num1=2,i_num2=4});intresult=result.GetOutput<int>("e_result");}

You might be asking: It's almost the same! Well, yes, it is.

But what about write this:

using(SapRfcConnectionconn=newPlainSapRfcConnection("TST")){varcustomer=newZCustomer(3,"Some Company",true);varresult=conn.ExecuteFunction("Z_SSRT_ADD_CUSTOMER",new{i_customer=customer});}publicclassZCustomer{publicintId{get;set;}publicstringName{get;set;}[RfcStructureField("ACTIVE")]publicboolIsActive{get;set;}publicZCustomer(intid,stringname,boolisActive){this.Id=id;this.Name=name;this.IsActive=isActive;}}

Instead of:

RfcDestinationdestination=RfcDestinationManager.GetDestination("TST");RfcRepositoryrepository=destination.Repository;IRfcFunctionfunction=repository.CreateFunction("Z_SSRT_ADD_CUSTOMER");IRfcStructurecustomer=function.GetStructure("i_customer");customer.SetValue("ID",3);customer.SetValue("NAME","Some Company");customer.SetValue("ACTIVE",true);function.SetValue("i_customer",customer);function.Invoke(destination);

That's more code, but it's because of Customer class. But because of this class we can work with strongly-typed parameters!

Check this out.

using(SapRfcConnectionconn=newPlainSapRfcConnection("TST")){varresult=conn.ExecuteFunction("Z_SSRT_GET_ALL_CUSTOMERS");varcustomers=result.GetTable<ZCustomer>("t_customers");}

It`s way easier than a iterating on each row and building the object by yourself. It also works for input parameters!

Two options available

SharpSapRfc comes with two flavors: RFC and SOAP. Both options have the same interface, so you can swap between them with a single line of code. ABAP requiriments are the same for both libraries, just create an RFC-enable Function Module and you're good to go, be it with RFC or SOAP.

Which one should I use?

We generally recommend RFC protocol because it is faster than SOAP. The main advantage of SOAP is that enables you to publish you application outside your LAN and connect to SAP thought a common protocol (HTTP/HTTPS) combined with DMZ and Reverse Proxy, for example.

How to swap between Plain RFC and SOAP

All examples on this document are using Plain RFC (class is PlainSapRfcConnection). If you want to use SOAP, just change to SoapSapRfcConnection. The configuration file is different for each library. Examples are on the bottom of this document.

How to install

For Plain RFC x86 apps

PM> Install-Package SharpSapRfc.Plain.x86

For Plain RFC x64 apps

PM> Install-Package SharpSapRfc.Plain.x64

Important: To use Plain RFC we need to reference assemblies sapnco.dll and sapnco_utils.dll found in SAP .NET Connector 3.0 (x86 or x64). Downloads for SAP Connectors are available at SAP Service Marketplace for customers and partners. More info at http://service.sap.com/connectors.

For SOAP

PM> Install-Package SharpSapRfc.Soap

Important: There are no additional dependencies to run Soap RFC, but the target SAP server has to enable RFC calls in ICF.

RFC Function Object

If you think your code that calls RFC is too much complicated, you can take advantage of the RFC Function Object Pattern. With this pattern you can encapsulate your RFC call in a class that is used by SharpSapRfc. Take a look at the following example.

using(SapRfcConnectionconn=newPlainSapRfcConnection("TST")){varcustomers=conn.ExecuteFunction(newGetAllCustomersFunction());}//GetAllCustomersFunction is a RFC Function Object that encapsulate the configuration and parameters for calling the remote functionpublicclassGetAllCustomersFunction:RfcFunctionObject<IEnumerable<ZCustomer>>{publicoverridestringFunctionName{get{return"Z_SSRT_GET_ALL_CUSTOMERS2";}}publicoverrideIEnumerable<ZCustomer>GetOutput(RfcResultresult){returnresult.GetTable<ZCustomer>("e_customers");}}

For more examples using simple return type and multiple return values, please check test case named FunctionObjectTestCase

RFC Read Table

There's also a shortcut to the RFC_READ_TABLE function.

You can use it like this:

publicclassAirlineCompany{[RfcStructureField("MANDT")]publicintClient{get;set;}[RfcStructureField("CARRID")]publicstringCode{get;set;}[RfcStructureField("CARRNAME")]publicstringName{get;set;}[RfcStructureField("CURRCODE")]publicstringCurrency{get;set;}[RfcStructureField("URL")]publicstringUrl{get;set;}}//Reading all table entriesusing(SapRfcConnectionconn=newPlainSapRfcConnection("TST")){varscarr=conn.ReadTable<AirlineCompany>("SCARR");Assert.AreEqual(18,scarr.Count());}//Reading two fields with where clauseusing(SapRfcConnectionconn=newPlainSapRfcConnection("TST")){varscarr=conn.ReadTable<AirlineCompany>("SCARR",fields:newstring[]{"CARRID","CARRNAME"},where:newstring[]{"CARRID = 'DL'"});}

Configuration Example

Plain RFC
<configuration>
<configSections>
<sectionGroupname="SAP.Middleware.Connector">
<sectionname="GeneralSettings"type="SAP.Middleware.Connector.RfcGeneralConfiguration,sapnco" />
<sectionGroupname="ClientSettings">
<sectionname="DestinationConfiguration"type="SAP.Middleware.Connector.RfcDestinationConfiguration, sapnco"/>
</sectionGroup>
</sectionGroup>
</configSections>
<SAP.Middleware.Connector>
<ClientSettings>
<DestinationConfiguration>
<destinations >
<addNAME="TST"USER="bcuser"PASSWD="sapadmin2"CLIENT="001"LANG="EN"ASHOST="sap-vm"SYSNR="00" />
</destinations>
</DestinationConfiguration>
</ClientSettings>
</SAP.Middleware.Connector>
</configuration>
SOAP
<configuration>
<configSections>
<sectionname="sapSoapRfc"type="SharpSapRfc.Soap.Configuration.SapSoapRfcConfigurationSection, SharpSapRfc.Soap" />
</configSections>
<sapSoapRfc>
<destinations>
<addname="TST-SOAP"rfcUrl="http://sap-vm:8000/sap/bc/soap/rfc"wsdlUrl="http://sap-vm:8000/sap/bc/soap/wsdl"client="001"user="bcuser"password="sapadmin2"/>
</destinations> </sapSoapRfc>
</configuration>

All Features

  • Mapping for Structures and Tables
  • 2 remote fields (DATE and TIME) mapped to a single DateTime property
  • Enum Mapping (both numbers and strings)
  • Boolean. Use True and False instead of "X" and "
  • Shortcut to RFC_READ_TABLE (with Fluent API)
  • RFC Object Function Pattern
  • Easier RFC calls

Take a look at the tests project for more examples.

About

Making SAP RFC calls even easier with .Net

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages