Skip to content
Start free evaluation

PDComm.OpcUa.Client

Class

An OPC UA client session driven by the caller: send UA Binary service requests and poll for their responses.

public sealed class Client : IDisposable

The client creates no background task and no callbacks. Client.Connect opens the secure channel and creates a session; activate it with Client.ActivateAnonymous or Client.ActivateUserName. Then send requests with Client.Begin, call Client.Poll regularly while requests are pending, and collect each response with Client.Take. Connection, discovery, activation and Client.Close block for up to the configured timeout per network exchange.

Calls on one client are serialized by an internal lock. For an orderly shutdown, call Client.Close after your requests complete, then Client.Dispose; disposing without closing aborts the connection. Responses and buffers stay valid after the client is closed or disposed.

The first use of the API checks that the native library implements OPC UA ABI 0.2. If not, the call throws TypeInitializationException whose inner exception is PlatformNotSupportedException, and the binding stays unusable in this process.

using (var endpoints = Client.Discover(endpoint, 5000))
    Console.WriteLine($"{endpoints.Copy().Length} bytes of GetEndpointsResponse");
using var client = Client.Connect(endpoint, new ClientOptions
{
    Policy = policy, Mode = mode, AllowNone = policy == SecurityPolicy.None, TimeoutMs = 5000,
    CertificateDer = Load(certificatePath), PrivateKeyPem = key, TrustedServerDer = Load(serverCertificatePath),
});
client.ActivateAnonymous();
uint id = client.Begin(readRequest); // a UA Binary ReadRequest with its encoding NodeId
var clock = Stopwatch.StartNew();
while (clock.Elapsed < TimeSpan.FromSeconds(5))
{
    client.Poll();
    using var response = client.Take(id);
    if (response is not null) { Console.WriteLine($"0x{response.Info.ServiceStatus:x8}"); break; }
    Thread.Sleep(1);
}
client.Close();

Members

ActivateAnonymous

Method

Activates the session with an anonymous identity.

public void ActivateAnonymous()

Notes

  • Throws ObjectDisposedException: The client has been disposed.
  • Throws OpcUaException: The server rejected the activation, or the exchange failed or timed out.

ActivateUserName

Method

Activates the session with a user name and password identity.

public void ActivateUserName(string name, byte[] secret)

Parameters

NameDescription
nameThe user name.
secretThe password bytes, at most 16 KiB. They are encrypted for the server and never logged; you own the array and should clear it after the call.

Notes

  • Throws ObjectDisposedException: The client has been disposed.
  • Throws ArgumentNullException: name or secret is null.
  • Throws OpcUaException: The server rejected the identity, or the exchange failed or timed out.

Begin

Method

Sends a service request.

public uint Begin(byte[] request)

Parameters

NameDescription
requestThe complete UA Binary request, starting with its encoding NodeId, at most 4 MiB. The client replaces the authenticationToken, timestamp and requestHandle of the request header. Session and channel lifecycle requests are rejected; use the dedicated methods instead.

Returns

The client-local request ID to pass to Client.Take.

Notes

  • Throws ObjectDisposedException: The client has been disposed.
  • Throws ArgumentNullException: request is null.
  • Throws OpcUaException: The request is malformed or unsupported, 64 requests are already outstanding, or the session is not usable.

Close

Method

Closes the session and the secure channel in an orderly way.

public void Close()

Notes

  • Throws ObjectDisposedException: The client has been disposed.
  • Throws OpcUaException: The close exchange failed or timed out.

Connect

Method

Opens a secure channel to a server and creates a session.

public static Client Connect(string endpoint, ClientOptions options)

Parameters

NameDescription
endpointThe server URL, for example opc.tcp://localhost:4840/pdcomm.
optionsThe security policy, mode, identity, trust and timeout.

Returns

A new client, which the caller owns and must dispose. Activate its session before sending requests.

Notes

  • Throws ArgumentNullException: endpoint or options is null.
  • Throws OpcUaException: The options are invalid, the server is not trusted, no endpoint matches the requested security, the connection failed or timed out, or no valid license grants the opcua-client module (OpcUaException.LicenseRequired).
  • Throws TypeInitializationException: The native library is missing or does not implement OPC UA ABI 0.2 (the inner exception says which).

Discover

Method

Requests the endpoints of a server without creating a session.

public static BinaryBuffer Discover(string endpoint, uint timeoutMs = 10000)

Discovery is unauthenticated; do not trust the result without verifying the server certificate.

Parameters

NameDescription
endpointThe server URL, for example opc.tcp://localhost:4840/pdcomm.
timeoutMsThe timeout of each network exchange, in milliseconds.

Returns

A new buffer, which the caller must dispose, holding the UA Binary GetEndpointsResponse with its encoding NodeId prefix.

Notes

  • Throws ArgumentNullException: endpoint is null.
  • Throws OpcUaException: The URL is invalid, discovery failed or timed out, or no valid license grants the opcua-client module (OpcUaException.LicenseRequired).
  • Throws TypeInitializationException: The native library is missing or does not implement OPC UA ABI 0.2 (the inner exception says which).

Dispose

Method

Releases the client; an open connection is aborted without a protocol close.

public void Dispose()

A call running on another thread keeps the native client alive until it returns. Calling this method more than once has no effect.

Poll

Method

Sends queued request data and reads the responses that have arrived, without waiting for new data.

public void Poll()

Notes

  • Throws ObjectDisposedException: The client has been disposed.
  • Throws OpcUaException: The connection failed or a pending request exceeded ClientOptions.TimeoutMs. Such a failure closes the client and discards its pending requests; later calls fail with status 10 (closed).

Take

Method

Takes the response to a request if it has arrived.

public Response? Take(uint request)

Parameters

NameDescription
requestThe request ID returned by Client.Begin.

Returns

The validated response, which the caller owns and must dispose, or null if it has not arrived yet. A ServiceFault or Bad service result is still returned as a response.

Notes

  • Throws ObjectDisposedException: The client has been disposed.
  • Throws OpcUaException: The request ID is unknown, the response failed validation, or the client's buffer registry is full (release buffers, then retry before the next poll).

AbiVersion

Property

Gets the ABI version reported by the native library, as major in the upper 16 bits and minor in the lower 16 bits.

public static uint AbiVersion { get; }

Notes

  • Throws TypeInitializationException: The native library is missing or does not implement OPC UA ABI 0.2 (the inner exception says which).