Skip to Content
author's profile photo Former Member
Former Member

Sap .Nco 3.0 connector, Connection not closed/Logoff

I am using Tuturiol downloaded from Connectors download link.

Connection created using RfcDestinationManager.GetDestination() is not closed.

I am calling method Test() on button click, in test1(). I am not able to close the connection by setting the desitination object to null even.

In SAP transaction SM04, session list still shows the RFC connection with 'myusername' after the call to the method test() is finished.

  • Connection is closed only when i close my windows application.

C# code

-


private void btnLogon_Click(object sender, EventArgs e)
{
  Test();
}

void Test()
{
RfcConfigParameters parameters = new RfcConfigParameters();

            parameters[RfcConfigParameters.Name] = "Test1";
            parameters[RfcConfigParameters.User] = "myusername";
            parameters[RfcConfigParameters.Password] = "mypassword";
            parameters[RfcConfigParameters.Client] = "800";
            parameters[RfcConfigParameters.Language] = "EN";
            parameters[RfcConfigParameters.AppServerHost] = "192.168.101.15";
            parameters[RfcConfigParameters.SystemNumber] = "00";

            RfcDestination destination = RfcDestinationManager.GetDestination(parameters);
            RfcSessionManager.BeginContext(destination);
            
            destination.Ping();
            IRfcFunction function = null;

            try
            {
             // read table from RFC            
              function = destination.Repository.CreateFunction("BDC_RECORD_TRANSACTION");
                IRfcTable obj1 = function.GetTable("DYNPROTAB");
            }
            catch (Exception ex)
            {
                          System.Windows.Forms.MessageBox.Show("Error: " + ex.Message);
            }
                       
            RfcSessionManager.EndContext(destination);
            destination  = null;
}

Edited by: HarminderSingh on Mar 10, 2011 6:14 AM

Add a comment
10|10000 characters needed characters exceeded

Assigned Tags

Related questions

8 Answers

  • author's profile photo Former Member
    Former Member
    Posted on Mar 25, 2011 at 07:19 PM

    I'm having the same problem. It does that because the NCO 3.0 uses a connection pool. I just dont know how to clear the pool everytime so I can have different connections for every user.

    Add a comment
    10|10000 characters needed characters exceeded

  • author's profile photo Former Member
    Former Member
    Posted on Mar 29, 2011 at 10:35 AM

    Issue is resolved.

    Following method mentioned in the Sap .Nco 3.0 tutorial, helped me in resolving the issue.

    File: CustomDestinationConfiguration.cs

    Class: InMemoryDestinationConfiguration

    Method: RemoveDestination()

    public class Form1(
    
    private void btnLogon_Click(object sender, EventArgs e)
            {
    
                objDestConfig = new InMemoryDestinationConfiguration();
    
                //RfcDestinationManager destination = RfcDestinationManager.GetDestination("TEST1");
    
                RfcDestinationManager.RegisterDestinationConfiguration(objDestConfig);
                Console.WriteLine("Registered own configuration");
                objDestConfig.AddOrEditDestination("TESTW47", 1, "MyUsername", "MyPassword", "EN", "800", "192.168.101.15", "00");
    
                RfcDestination destination1 = RfcDestinationManager.GetDestination("TEST1");
                destination1.Ping();
    
                MessageBox.Show("Wait here");
    
                objDestConfig.RemoveDestination("TESTW47");
                //SimpleLogon();
            }
    }
    
    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)
            {
                //in productive code the given parameters should be checked for validity, e.g. that name is not null
                //as this is not relevant for the example, we omit it here
                RfcConfigParameters parameters = new RfcConfigParameters();
                parameters[RfcConfigParameters.Name] = name;
                parameters[RfcConfigParameters.MaxPoolSize] = Convert.ToString(poolSize);
                parameters[RfcConfigParameters.IdleTimeout] = Convert.ToString(1); // we keep connections for 10 minutes
                parameters[RfcConfigParameters.User] = user;
                parameters[RfcConfigParameters.Password] = password;
                parameters[RfcConfigParameters.Client] = client;
                parameters[RfcConfigParameters.Language] = language;
                parameters[RfcConfigParameters.AppServerHost] = applicationServer;
                parameters[RfcConfigParameters.SystemNumber] = systemNumber;
                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] = parameters;
                    RfcConfigurationEventArgs eventArgs = new RfcConfigurationEventArgs(RfcConfigParameters.EventType.CHANGED, parameters);
                    MessageBox.Show("Fire change event " + eventArgs.ToString() + " for destination " + name);
                    changeHandler(name, eventArgs);
                }
                else
                {
                    availableDestinations[name] = parameters;
                }
    
                MessageBox.Show("Added application server destination " + name);
                
            }
    

    Edited by: HarminderSingh on Mar 29, 2011 12:36 PM

    Add a comment
    10|10000 characters needed characters exceeded

  • author's profile photo Former Member
    Former Member
    Posted on Apr 12, 2011 at 12:11 PM

    Solved by reading the tutorials and testing them with my requirements.

    Add a comment
    10|10000 characters needed characters exceeded

    • Former Member Former Member

      I have used RemoveDestination call to logoff explicitly.

      Set one property to true in your class after doing all the Rfc communications. explicitly. So whenever the value in the property is set to TRUE, call the Logoff.

      In case my answer is not upto mark, then please paste the your sample code so that i can do some modification in it and help you.

  • author's profile photo Former Member
    Former Member
    Posted on Jan 11, 2012 at 07:39 PM

    Try running the garbage collector on the object to release unused object.

    GC.Collect();

    GC.WaitForPendingFinalizers();

    Add a comment
    10|10000 characters needed characters exceeded

  • author's profile photo Former Member
    Former Member
    Posted on Apr 09, 2012 at 12:55 PM

    To register a destination use: RfcDestinationManager.RegisterDestinationConfiguration(objConnectionConfig);

    //where objConnectionConfig is the implementation of interface IDestinationConfiguration

    I also faced this issue finally use Unregister destination like this.

    RfcDestinationManager.UnregisterDestinationConfiguration(objConnectionConfig);

    Hope this helps.

    Add a comment
    10|10000 characters needed characters exceeded

  • author's profile photo Former Member
    Former Member
    Posted on Apr 08, 2014 at 11:47 PM

    I have a requirement where I need to make calls to SAP. For that I am registering --> getting the data from SAP --> and un-registering the the destination.

    But Some times I am receiving the error message saying:

    "cannot unregister the given destination configuration" . I know that since I am doing it frequently its occurring. Has anyone faced this earlier.

    Add a comment
    10|10000 characters needed characters exceeded

  • author's profile photo Former Member
    Former Member
    Posted on Jun 27, 2014 at 11:13 AM

    When i am doing Ping and checking in SM04 it shows me only 1 user.

    But when i am calling my Function Module using .Net Connector i can see 2 users immediately in SM04. Now from this 2 sessions in SM04 1 is active and another remains inactive all the time. After certain time of idle that 1 session goes out(terminated) automatically.

    But i can't understand why it shows me 2 sessions when even i am calling only 1 function at that point of time.

    If i forcefully terminate that inactive session from SAP GUI from SM04 , then also it does not cause any issue, because that is actually in non of use.

    Please give me solution how can i avoid such circumstances.

    Add a comment
    10|10000 characters needed characters exceeded

  • Posted on Aug 31, 2016 at 04:44 AM

    Hi Markus,

    In above code when below line is getting executed it is throwing null reference exception.

    changeHandler(name, eventArgs);

    Please help.

    Regards,

    Milan

    Add a comment
    10|10000 characters needed characters exceeded

Before answering

You should only submit an answer when you are proposing a solution to the poster's problem. If you want the poster to clarify the question or provide more information, please leave a comment instead, requesting additional details. When answering, please include specifics, such as step-by-step instructions, context for the solution, and links to useful resources. Also, please make sure that you answer complies with our Rules of Engagement.
You must be Logged in to submit an answer.

Up to 10 attachments (including images) can be used with a maximum of 1.0 MB each and 10.5 MB total.