Skip to content

How to properly implement the reconnection to the server #3092

Closed

Description

@GaetanKorpys

Type of issue

  • Bug
  • Enhancement
  • Compliance
  • Question
  • Help wanted

Current Behavior

Hello, I have several questions about reconnection mecanism :

Doc about TransferSubscription :
"Transfer after session reconnect: The client SessionReconnectHandler can not reconnect to the existing session, it creates a new session and tries to transfer a subscription before recreating the full subscriptions."

  • Does it mean that BeginReconnect will handle the creation of the subscription and monitored Item (not only the session) ? Even after a restart of the server (server is empty, subscriptions and monitored items are not persisted afer the restart) ?

  • Is it possible to reconnect (subscription and monitored working) using BeginReconnect method when the session is closed on the server (du to timeout for example) ?
    If so, should the server implement DurableSubscription and TransferSubscription ?

In the sample, they use :
// support transfer
m_session.DeleteSubscriptionsOnClose = false;
m_session.TransferSubscriptionsOnReconnect = true;

I know sometimes the SDK send a new session with a different id from the previous one.
And in that case, my mind tell me that the sdk will transfer the subscriptions.

  • When I call BeginReconnect, should I disable Keep Alive (e.CancelKeepAlive = True) even if the state is not ReconnectState.Triggered ?

That the case in the sample, but it means that BeginReconnect will moves to that state on her own ?
If so, then the client can not really log when the reconnect is starting (not completed) if i'm correct.

  • When I set e.CancelKeepAlive at True, my method Session_KeepAlive continues to be trigger. I confirm that with logs. Do I missunderstand something in the use case ?

  • Does ReconnectPeriod means that BeginReconnect will try to reconnect each ReconnectPeriod ?

Expected Behavior

No response

Steps To Reproduce

No response

Environment

- OS: Windows 11
- Environment: VS 2022
- Runtime: .Net 8
- Nuget Version: 1.5.376.213
- Component: /
- Server: /
- Client: my own implementation

Anything else?

Sample code from ConsoleReferenceClient :

///


/// Handles a keep alive event from a session and triggers a reconnect if necessary.
///

private void Session_KeepAlive(ISession session, KeepAliveEventArgs e)
{
try
{
// check for events from discarded sessions.
if (m_session == null || !m_session.Equals(session))
{
return;
}

    // start reconnect sequence on communication error.
    if (ServiceResult.IsBad(e.Status))
    {
        if (ReconnectPeriod <= 0)
        {
            Utils.LogWarning("KeepAlive status {0}, but reconnect is disabled.", e.Status);
            return;
        }

        var state = m_reconnectHandler.BeginReconnect(m_session, m_reverseConnectManager, ReconnectPeriod, Client_ReconnectComplete);
        if (state == SessionReconnectHandler.ReconnectState.Triggered)
        {
            Utils.LogInfo("KeepAlive status {0}, reconnect status {1}, reconnect period {2}ms.", e.Status, state, ReconnectPeriod);
        }
        else
        {
            Utils.LogInfo("KeepAlive status {0}, reconnect status {1}.", e.Status, state);
        }

        // cancel sending a new keep alive request, because reconnect is triggered.
        e.CancelKeepAlive = true;

        return;
    }
}
catch (Exception exception)
{
    Utils.LogError(exception, "Error in OnKeepAlive.");
}

}

Activity

  1. locked and limited conversation to collaborators on Jun 26, 2025
  2. converted this issue into a discussion #3097 on Jun 26, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions