Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles / productivity / SAP

SAP .NET RFC Server with SAP.NET Connector 2.0

4.70/5 (10 votes)
26 Apr 2009CPOL5 min read 103.2K   2.5K  
This article explains how to connect from SAP to .NET application

Introduction 

People think that SAP.NET connector is used only for connecting to SAP from .NET. So, they know that SAP.NET connector is useful while getting SAP's data to our development environment. But, SAP.NET connector helps us in connecting to our .NET application from SAP, not only connecting to SAP from .NET application. 

I have published another article about connection to SAP from Visual Studio 2008. You can examine this article from this link. In this article, we will examine how to connect to our .NET application from SAP.

On the other hand, SAP .NET Connector is an add on for Visual Studio .NET 2003. SAP has not developed a connector API for Visual Studio 2005 and Visual Studio 2008 yet. I think the reason of not developing a connector API depends on some marketing strategies and the feasibility that can be developed with the web service that comes with the SAP 6.0. 

As known, there are some third party programs that can connect to SAP Application Servers with Visual Studio 2005 or Visual Studio 2008. But, there is reality for enterprise firms; ‘If we are using SAP for ERP solution, and if I need to connect to SAP via other systems, I should take care of the suggestions of SAP prior’. The meaning of this is that ‘if we need connector, we should use SAP connector’. This approach may be relative for each person. But in my opinion, this approach is true.  

I don't want to discuss why SAP does not support a connector for Visual Studio 2005 & 2008. But it should be known that by creating a library project in Visual Studio .NET 2003 and using this project in Visual Studio 2005 or 2008, the needed desire can be solved. I also published an article for this desire. You can examine this article from this link.

In this article, we will create a .NET Server for connection from SAP to .NET side. In some cases, we need this solution. Although SAP is a comprehensive solution center for all enterprise needs, sometimes SAP cannot give an answer for all of our needs. And if our need can be solved in Visual Studio environment, we need a solution as I will explain below.  

We will illustrate a sending mail operation in .NET 2003 with Gmail POP3 and we will call this program from SAP.  

Background 

Required 

  • Visual Studio .NET 2003  
  • SAP Application Server [Any acceptable version for RFC]  
  • SAP .NET Connector 2.0  
  • Gmail Account  

Using the Code 

At first, let’s look at the SAP side. Firstly, we need to create an RFC function in SAP.  

rfc_listener_1.jpg

As seen above, your function should be an RFC type.

rfc_listener_2.jpg

And we have declared the import parameters of our function.

rfc_listener_12.jpg

As seen above, there is one export parameter for our function. This will show the result of sending mail.  

rfc_listener_3.jpg

There is one table parameter of our function and that is the body of our mail. The signature of this table is shown below:

rfc_listener_5.jpg

rfc_listener_4.jpg

And as seen, no need to code, we only use this function module’s signature while connecting to .NET.

The next step is creating a .NET project. This project can be a library application, Windows service application, Windows application or console application or any another suitable application. I have just created a console application. Later on, I have created a SAPProxy class.

Then, I have defined my YSEND_MAIL function in SAPProxy1 class.

rfc_listener_6.jpg

Because we are applying this application as a server, you have to change the SAPProxy1 class properties as I have shown.

rfc_listener_7.jpg

After changing the proxy type as Server, you will see SAPProxy1Impl.cs created under Solution Explorer. Then open SAPProxy1Impl.cs file and add an additional constructor as:

C#
public SAPProxy1Impl(string programId, string gwhost, 
	string sapgwxx, string codepage, SAP.Connector.SAPServer host) 
: base(programId, gwhost, sapgwxx, codepage, host) 
{
}

You will also see, the YSEND_MAIL method as:

C#
protected override void YSEND_MAIL(string IM_BCC, string IM_CC, string IM_SUBJECT, 
string IM_TO, out string EX_RESULT, ref ZMAIL_BODYTable T_BODY)

When SAP function has called this method will be called. We will change this method as below:

C#
string body = "";
System.Data.DataTable dt = T_BODY.ToADODataTable();
for (int i = 0; i < dt.Rows.Count; i++ )
{
   body += dt.Rows[i][0].ToString();
}
string result;
ISMTP gMail = new GMailSMTP();
gMail.send(IM_TO, IM_CC, IM_BCC, "","", IM_SUBJECT, body, out result);

I have deleted the Main method of mSAPProxy1Impl class and placed the required code into the Main method of Class1.

C#
SAPServerHost host = new SAPServerHost();
SAPProxy1Impl impl = new SAPProxy1Impl("SENDING_MAIL", "", "sapgw00", "", host);

While creating SAPProxy1Impl, we are declaring the program name parameter as “SENDING MAIL”, gateway host is your application server’s IP address or name, gateway service as “sapgw00?, codepage as “” and last the host object.

And the important point, if your application server has a load balancing property, you should declare load balance application server’s IP address or name for gateway host parameter.

Later on, we are starting our RFC listener as:

C#
host.Start();

Our server will remain while .NET application is live. Otherwise it will be terminated. So, you need to insert a loop code after starting the server !!

At this point, we have completed the required operations on the .NET side. The last operation again is in SAP. Now, let’s go on SM59 t-code in SAP. You need to create a TCP/IP RFC Connection. I have shown below:

rfc_listener_8.jpg

While creating the screen should be as below:

rfc_listener_9.jpg

I have just taken care of a point; if your application server has load balancing, you need to give load balance application server’s name into the gateway host and “sapgw00? for gateway service. Then save this RFC connection. Later on press “Test connection” button. You should see the below screen:

rfc_listener_101.jpg

A general error is being done while testing the connection before .NET application is running. Firstly, run .NET application and then test connection.

All of the operations are ok. But how do we call a .NET application? How do we use it? The solution is very simple. In any program or function, you should call the YSEND_MAIL function as below:

rfc_listener_11.jpg

As understood, the hint point is declaring DESTINATION as “ZSENDMAIL”. So, you are calling this function module in .NET application side, not in SAP side.

You can also visit my technical blog site for my other articles.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)