In some applications you need to send the data to SAP System and also get the data from them to display it on web pages. This article will help you to achieve that.
This article defines how to connect to SAP RFC using SAP.Net Connector 3.0 called as NCo. This article expects that you have basic knowledge of development using C# and ASP.Net
Using the code
The most important part of SAP Connector is to have required dlls:
- .Net Connector 3.0 DLL’s. There are 32 bit (x86) and 64 bit versions available for it
To get the SAP Connector DLL, you need to go to SAP Market Place and you need to have userid and password to download it. If you are a .net developer, probably you do not have it. Ask the SAP Basis team to download and get it for you as they normally do have market place access.
If you get it from somewhere you are ready to go ahead.
If you are in development mode, using VS 2012/2013, you need to select Build platform Target as Any CPU. And add reference of 32-bit DLL even if you are using 64 bit machine.
On deployment to IIS7.5 or 8, if its 64 bit machine. Replace the 32-bit dll with 64-bit in the BIN Folder.
- SAP Destination Server configuration that needs to added to your config file.These are the main key-value pairs required to connect to SAP.
Launch the Visual Studio 2012 or 2013
- Create a web application SAPConnector3Example and add reference of sapnco.dll and sapnco_utils.dll
- Add a new class called SAPDestinationConfig that you will use to read the configuration
- Add SAP.Middleware.Connector namespace to this class which comes from the dll’s that you just added as reference in your project.
- To get the destination needed, SAP Connector provides an interface called IDestinationConfiguration. This interface provides a method called GetParameters. You can use configuration manager to read this and implement this method.
Inherit IDestinationConfiguration to your class and right click on it to implement interface. After this step the class will appear as below
- GetParameters method returns RfcConfigParameters which is class in SAP Connector DLL. Update the code to read the config values, and the code appears like this
- Now the application is able to get the configuration, next step is to call this method. It can be called in a class where you would connect to the SAP. But the SAP connector has a method called RegisterDestinationConfiguration. All the destinations that have been defined like QA, DEV, PROD needs to be registered first before the call.
You can do this step just before connecting to the SAP when you instantiate the class. But think if many users are connected, each one would be calling RegisterDestinationConfiguration every time they make a call to SAP. So the best place to do it is Global.asax under Application_Start.
As soon as you deploy your application and start it, there would be a destination connection already registered and ready to be used.
This will also resolve one of the important error messages, something like “destination configuration already initialized”
- Your Global.asax should look something like this. Make sure to include SAP.Middleware.Connector namespace in this class as well
- The next step is to create a class which will act as a middleman between the .Net application and the SAP. This class will be responsible for connecting to SAP Server using SAP Connector, sending data and fetching data. Add a new class called SAPConnectorInterface. Add using SAP.Middleware.Connector namespace ,as this is also going to use the SAP Connector DLL.
- Let’s create a Test Connection to check if you are able to connect to SAP. RfcDestination class has a method Ping() which successfully pings, if the connection is established between your application and the SAP. If it’s not able to establish the connection, it will throw an exception.
- Now is the time to call SAP RFC to fetch data of the customers. Before stepping into this process, connect to you SAP Team Members and get the RFC’s that are available and written by them. Also check if they return the data back. RFC returns the data in parameters only, just like when you use ‘out’ keyword in a method call. Input Params in SAP is known as IMPORT and the values are returned as EXPORT params.
- The RFC destination has one Repository. This repository contains complex RFC Functions and RFC structures. Sometimes you will find that, on SAP Side, they have a complex IRfcStructure which has IRfcFunction and viceversa. There functions returns a complex table structure called IRfcTable that you need to convert to ADO.Net Table. Unfortunately SAP.Net connector does not have any default method that can be used. Use the following method to convert the IRfcTable to DataTable
- Create a method called GetCustomers which has two input parameters, country and destination name.
Create an instance of RFCRepository which has all the functions into it.
rfcRepository = rfcDestination.Repository;
Using this repository call the method CreateFunction and provide the RFC name of SAP that you need to call. You need to make sure that the exact function name is available in SAP.
rfcFunction = rfcRepository.CreateFunction("RFC_CUSTOMER_DATA");
The input parameters to the RFC Functions are passed like this. It’s kind of Key Value Pair. COUNTRY should be available as a parameter in the RFC in SAP Side.
The function is called using this line
You will notice here that rfcDestination is being called, which contains all the needed information that is required by the SAP Connector to call SAP RFC.
Sometimes you will find that the RFC is returning a structure which has many RFC functions and each has different RFC tables. This needs to extracted like this
This is a very simple example. You can send number of parameters to a RFC using rfcFunction.SetValue method. RfcFunction.Invoke(rfcDestination) is the line where the .Net application actually calls the SAP Function.
You will notice here, if you place a debug mark, that if you are sending big data to SAP and if SAP function is complex one and do many things at its end, the debug will get paused for some time and then it will move to the next debug line. This line is actually waiting for the call to come back to your application.
RegisterDestinationManager classs has one more method RfcDestinationManager.UnregisterDestinationConfiguration(destinationConfig), but you rarely would need to use it.
Points of Interest
I am .Net developer working for a consulting firm.
I specialize in Microsoft.net technologies.