function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
BobRoyAceBobRoyAce 

Determining if connection has timed out.

I have code like follows, to login/connect to Salesforce web services...

 

    _SalsesforceService = New SforceService

    _SalsesforceService.Timeout = 60000
    _LoginResult = _SalsesforceService.login(UserName, Password + SecurityToken)

 

This, of course, works great. 

 

However, if a lot of time goes by without communicating with Salesforce, the "connection" is no longer valid, and, if I try to make calls, I get errors.

 

So, the question is, how can I determine if the connection to the service has timed out, so that I can know if I need to reestablish the connection? Secondly, if it has timed out, do I just run _SalsesforceService.login(UserName, Password + SecurityToken) again to reestablish the connection?

 

AlwaysConfusedAlwaysConfused

This works fine for me ...

 

        /// <summary>

        /// Login script for sales force...

        /// </summary>

        /// <param>SF username</param>

        /// <param>SF password + security token</param>

        bool Login(string Username, string Password)

        {

            service.Timeout = 60000;

            LoginResult lr;

            string username = (string)Username;

            string password = (string)Password;

 

            try

            {

                lr = service.login(username, password);

 

                service.Url = lr.serverUrl;

 

                service.SessionHeaderValue = new SessionHeader();

                service.SessionHeaderValue.sessionId = lr.sessionId;

               

                return true;

            }

            catch (SoapException e)

            {

                string Error = e.ToString();

            }

            return false;

        }

 

        /// <summary>

        /// Checks the session is still open

        /// </summary>

        /// <returns></returns>

        bool VerifySession()

        {

            bool isActive;

           

            if (service.SessionHeaderValue == null)

            {

                isActive = false;

            }

            else

            {

                isActive = true;

            }

 

            return isActive;

        }

 

 

Then whenever you are about to make a call to the sf service use ...

 

if(VerifySession())

{

     // do stuff
}