Skip to main content

Connect / Disconnect

Prerequisite Reading
  • Please review the construction parameters first Constructor.
  • For encryption settings, see Security.
  • For information on event subscriptions after connecting, see Events.

Connect()​

public void Connect();

Establish a connection to the endpoint and perform the following steps sequentially:

  1. Hello — Negotiate BufferSize / MaxMessageSize
  2. OpenSecureChannel — Negotiated Encryption Channel (None / Sign / SignAndEncrypt)
  3. GetEndpoints — Retrieve the list of server endpoints (for validation)
  4. CreateSession — Establish a session, negotiate SessionTimeout
  5. ActivateSession — Activate UserToken with
  6. Loading NamespaceArray to Namespaces

Once you've successfully completed the 6 steps above, follow these two SDK additional steps:

  1. AutoPublish = true — Start the Publish thread in the background
  2. Enable KeepAlive (see for interval KeepAliveIntervalMs)

After successful completion:

  • IsConnected == true
  • State == SessionState.Connected
  • Events.Connected Event Trigger

"Failure OpcUaException" ex.StatusCode is a specific status code. Common examples include:

StatusCodeTrigger Points
BadCertificateInvalidClient Failed to load PFX (incorrect password / corrupted file)
raw 0x80AB0000 (BadInvalidArgument)Sign / SignAndEncrypt But it wasn't passed clientCertPath
BadIdentityTokenRejectedUsername and password were rejected by the server
BadCommunicationErrorUnable to connect / Transfer failed

Examples​

using System;
using DarraOpcUa_Client;

using var ua = new DarraOpcUa("opc.tcp://localhost:4840");
ua.Events.Connected += (s, e) => Console.WriteLine($"Connected to {e.EndpointUrl}");
ua.Events.StateChanged += (s, e) => Console.WriteLine($"{e.OldState} → {e.NewState} (reason={e.Reason})");

try
{
ua.Connect();
}
catch (OpcUaException ex)
{
Console.WriteLine($"Connect failed: {ex.StatusCode} ({ex.Message})");
}

Disconnect()​

public void Disconnect();

The connection is actively disconnected, but the instance remains available DarraOpcUa for reconnection. The following steps occur in sequence:

  1. Stop KeepAlive Heartbeat
  2. Close Session + SecureChannel (AutoPublish Nonstop, (to be handled by the subsequent Connect/Dispose methods)
  3. If it was previously in Connected, push Events.StateChanged and Events.Disconnected (parameters Message = "主动断开", StatusCode = Good)

Dispose() Do not callDisconnect() — It closes the session immediately and releases resources (see Constructor ("Resource Management"). To ensure you receive the "active disconnect" event Disconnected, first explicitly dispose of the resource, then Disconnect() call Dispose.

Reconnect to the same instance​

ua.Disconnect();
// ... Waiting / Processing ...
ua.Connect(); // A single instance can be connected to multiple times

Automatic Reconnection (Built-in)​

Read / Write / Browse / Call / ReadHistory These 5 will automatically reconnect once and retry once if a session-level failure occurs RPC. For trigger conditions, scope, and limitations, see Automatic Reconnection — Please note that subscription-based calls andBrowseNext file transfers are not covered by automatic reconnection.

Status Inquiry​

SessionState s = ua.State;
// Disconnected / Connecting / Connected / Reconnecting / Closing / Failed

bool ok = ua.IsConnected; // == (State == SessionState.Connected)

SessionState Enumeration:

ValueMeaning
DisconnectedNot connected / Disconnected
ConnectingCurrently on Connect (Hello / OpenSecureChannel)
ConnectedConnected; Read, Write, and Subscribe functions are working normally.
ReconnectingA disconnection has been detected; automatically reconnecting
ClosingDisconnecting / Disposing
FailedFinal State: Reconnection Failed

Next Step​