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
Pat WimberlyPat Wimberly 

Restoring session after timeout

I have a program running as a service; it handles custom syncronization, polling and notification tasks. When initially run it logs into SF via the API and runs fine until the session times out. I've tried catching the exception and logging in again, as well as preemptively refreshing the session every few hours, in each case simply calling the login code again. This does not do the trick.

The login routine borrows heavily from stock code and is a method of a custom class, of the form MyClass.loginSalesForceServer(Username, Password).

What do I need to do to refresh/reestablish an API connection to prevent timeout problems?

I hope I stated my problem clearly enough.

    Thanks

Code Follows:

        Public Function loginSalesForceServer(ByVal UserName As String, ByVal Password As String) As Boolean
        Dim loginRes As sforce.LoginResult

        Dim un As String = UserName
        If un.Length = 0 Then
            Return False
        End If

        Dim pw As String = Password
        If pw.Length = 0 Then
            Return False
        End If

        '/*
        '* Create the sfbinding to the sforce servics
        '*/
        sfbinding = New sforce.SforceService

        '// Time out after a minute
        sfbinding.Timeout = 60000

        Try
            loginRes = sfbinding.login(un, pw)
        Catch e As System.Web.Services.Protocols.SoapException
            '// This is likley to be caused by bad username or password
            Return False
        Catch ex As System.Exception
            '// This is something else, probably comminication
            Return False
        End Try

        sfSessionID = loginRes.sessionId
        sfServerURL = loginRes.serverUrl

        '//Change the sfbinding to the new endpoint
        sfbinding.Url = loginRes.serverUrl

        '//Create a new session header object and set the session id to that returned by the login
        sfbinding.SessionHeaderValue = New sforce.SessionHeader
        sfbinding.SessionHeaderValue.sessionId = loginRes.sessionId

        prvSalesForceLoginState = "Logged In"

        Return True
    End Function
werewolfwerewolf
Re-logging in should do the trick.  Are you sure your binding is sufficiently scoped so that you're not accidentally using the old binding object when doing subsequent queries?