Connect / Disconnect
- 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:
- Hello — Negotiate BufferSize / MaxMessageSize
- OpenSecureChannel — Negotiated Encryption Channel (None / Sign / SignAndEncrypt)
- GetEndpoints — Retrieve the list of server endpoints (for validation)
- CreateSession — Establish a session, negotiate SessionTimeout
- ActivateSession — Activate UserToken with
- Loading NamespaceArray to
Namespaces
Once you've successfully completed the 6 steps above, follow these two SDK additional steps:
AutoPublish = true— Start the Publish thread in the background- Enable KeepAlive (see for interval
KeepAliveIntervalMs)
After successful completion:
IsConnected == trueState == SessionState.ConnectedEvents.ConnectedEvent Trigger
"Failure OpcUaException" ex.StatusCode is a specific status code. Common examples include:
| StatusCode | Trigger Points |
|---|---|
BadCertificateInvalid | Client Failed to load PFX (incorrect password / corrupted file) |
raw 0x80AB0000 (BadInvalidArgument) | Sign / SignAndEncrypt But it wasn't passed clientCertPath |
BadIdentityTokenRejected | Username and password were rejected by the server |
BadCommunicationError | Unable 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:
- Stop KeepAlive Heartbeat
- Close Session + SecureChannel (AutoPublish Nonstop, (to be handled by the subsequent Connect/Dispose methods)
- If it was previously in
Connected, pushEvents.StateChangedandEvents.Disconnected(parametersMessage = "主动断开",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:
| Value | Meaning |
|---|---|
| Disconnected | Not connected / Disconnected |
| Connecting | Currently on Connect (Hello / OpenSecureChannel) |
| Connected | Connected; Read, Write, and Subscribe functions are working normally. |
| Reconnecting | A disconnection has been detected; automatically reconnecting |
| Closing | Disconnecting / Disposing |
| Failed | Final State: Reconnection Failed |