I'm connecting to SAP from C#, through SAP .NET Connector 3.0. From time to time, when the server has some troubles I suppose, my application returns the error:
LOCATION SAP-Gateway on host xxx /sapgw00
ERROR timeout during accept
I'm not able so far to catch the error in the connection class and it is very annoying, because I'm displaying in a timer, from time to time, information read from SAP.
Any ideas how to catch it?
Here is the class that i use to connect to SAP
public class InMemoryDestinationConfiguration : IDestinationConfiguration
{
Dictionary<string, RfcConfigParameters> availableDestinations;
RfcDestinationManager.ConfigurationChangeHandler changeHandler;
public InMemoryDestinationConfiguration()
{
availableDestinations = new Dictionary<string, RfcConfigParameters>();
}
public RfcConfigParameters GetParameters(string destinationName)
{
RfcConfigParameters foundDestination;
availableDestinations.TryGetValue(destinationName, out foundDestination);
return foundDestination;
}
//our configuration supports events
public bool ChangeEventsSupported()
{
return true;
}
public event RfcDestinationManager.ConfigurationChangeHandler ConfigurationChanged
{
add
{
changeHandler = value;
}
remove
{
//do nothing
}
}
//removes the destination that is known under the given name
public void RemoveDestination(string name)
{
if (name != null && availableDestinations.Remove(name))
{
Console.WriteLine("Successfully removed destination " + name);
Console.WriteLine("Fire deletion event for destination " + name);
changeHandler(name, new RfcConfigurationEventArgs(RfcConfigParameters.EventType.DELETED));
}
}
//allows adding or modifying a destination for a specific application server
public void AddOrEditDestination(string name, int poolSize, string user, string password, string language, string client, string applicationServer, string systemNumber)
{
RfcConfigParameters parms = new RfcConfigParameters();
parms.Add(RfcConfigParameters.Name, name);
parms.Add(RfcConfigParameters.AppServerHost, applicationServer);//server ip address
parms.Add(RfcConfigParameters.SystemNumber, "00");
parms.Add(RfcConfigParameters.SystemID, "AAA"); //just a short name.
parms.Add(RfcConfigParameters.User, user); //SAP user name
parms.Add(RfcConfigParameters.Password, password);//_Password);
parms.Add(RfcConfigParameters.Client, "777");
parms.Add(RfcConfigParameters.Language, "EN");
parms.Add(RfcConfigParameters.PoolSize, "10");
parms.Add(RfcConfigParameters.IdleTimeout, "60");
RfcConfigParameters existingConfiguration;
//if a destination of that name existed before, we need to fire a change event
if (availableDestinations.TryGetValue(name, out existingConfiguration))
{
availableDestinations[name] = parms;
RfcConfigurationEventArgs eventArgs = new RfcConfigurationEventArgs(RfcConfigParameters.EventType.CHANGED, parms);
changeHandler(name, eventArgs);
}
else
{
availableDestinations[name] = parms;
}
}
}
I've tried even to verify in from the connection, by running a telnet to the port of the server and if I do not connect, to avoid getting the error message.
Here is the code for retrieving data (from a Z function module):
public List<clTRANSMON> GetTransMon(string plant, string project, string proces)
{
lsTransMon = new List<clTRANSMON>();
clTRANSMON tp = new clTRANSMON();
int result = RemoteTelnet();
if (result == 4)
{
tp.STATUS = "OFFLINE";
lsTransMon.Add(tp);
return lsTransMon;
}
tp.STATUS = project.Trim() + " - " + proces.Trim();
lsTransMon.Add(tp);
RfcDestinationManager.RegisterDestinationConfiguration(objDestConfig);
objDestConfig.AddOrEditDestination("mycon", 1, "myuser", "mypass", "EN", "777", _AppServerHost, "00");
RfcDestination rfc = RfcDestinationManager.GetDestination("mycon");
if (rfc != null)
{
rfc.Ping();
RfcRepository repo = rfc.Repository;
IRfcFunction readTable;
try
{
IRfcFunction rTable = repo.CreateFunction("Z_MY_FUNCTION_MODULE");
rTable.SetValue("I_PLANT", plant);
....
rTable.Invoke(rfc);
IRfcTable dtValues = rTable.GetTable("T_ALV");
foreach (var dataRow in dtValues)
{
//get all the data
}
} catch(Exception e) {}
}
objDestConfig.RemoveDestination("mycon");
return lsTransMon;
}
Is there a way to catch the error and avoid displaying the message? As I said, by telnet it isn't working:
public int RemoteTelnet()
{
try
{
TcpClient client = new TcpClient(_AppServerHost, 3300);
if (client.Connected == true)
{
client.Close();
return 0;
}
else return 4;
}
catch
{
return 4;
}
}
Thank you in advance.
PS This is the part where i'm calling the method from my class, where i had a MessageBox... I've commented it out, i have to see now if the popup appears anymore
try
{
lstTM=mySAP.GetTransMon(lsPlant,pj,lsProces[i]);
lsTM_Total.AddRange(lstTM);
}
catch (Exception eLoad)
{
//MessageBox.Show(eLoad.Message);
}