Skip to main content

Constructor

DarraOpcUa Two overloads are provided: a simplified constructor (common parameters + default inferences) and a fully configured constructor (all fields passed explicitly).

Prerequisite Reading
public DarraOpcUa(
string endpointUrl,
MessageSecurityMode securityMode = MessageSecurityMode.None,
string username = null,
string password = null,
uint sessionTimeoutMs = 60_000,
uint requestTimeoutMs = 10_000,
uint connectTimeoutMs = 10_000,
string clientCertPath = null,
string clientKeyPath = null,
string serverCertPath = null,
string securityPolicyUri = null);

Parameters​

CategoryPropertyTypeAccessDescription
RequiredendpointUrlstringinOPC UA Endpoint, in the format opc.tcp://host:port
SecuritysecurityModeMessageSecurityModeinNone / Sign / SignAndEncrypt, Default: None
securityPolicyUristringinIf not specified, the mode is selected automatically (None → #None, others → #Basic256Sha256)
clientCertPathstringinClient PFX path (Sign / SignAndEncrypt required; if missing, Connect fails immediately)
clientKeyPathstringin⚠ **Not a private key path** — it is the **password** for the PFX above. The name contains Key, but what is passed is a password string (such as "123456"); passing a file path will not unlock the PFX
serverCertPathstringinServer certificate **PFX** file path (PKCS#12, and must be passwordless — see "Server Certificate Validation" below). ⚠ Not providing it **is not** TOFU
Loginusernamestringinnull = Anonymous, non-null = UsernameIdentityToken
passwordstringinUsed with the username; if not provided, treated as an empty string
TimeoutsessionTimeoutMsuintinServer-side session timeout; default is 60 000 ms (60 seconds)
requestTimeoutMsuintinSingle RPC timeout, default 10 000 ms (10 seconds)
connectTimeoutMsuintinConnection timeout; default 10 000 ms (10 seconds)

Examples​

// 1. Anonymous + Plaintext (Development Phase)
using var ua = new DarraOpcUa("opc.tcp://localhost:4840");

// 2. Anonymous + Sign Encryption (Pinned Server Certificate)
using var ua2 = new DarraOpcUa("opc.tcp://server:4840",
securityMode: MessageSecurityMode.Sign,
clientCertPath: @"C:\certs\client.pfx",
clientKeyPath: "123456", // PFX Password
serverCertPath: @"C:\certs\server.pfx");

// 3. Username + SignAndEncrypt (Typical Production)
using var ua3 = new DarraOpcUa("opc.tcp://server:4840",
securityMode: MessageSecurityMode.SignAndEncrypt,
username: "operator",
password: "secret",
clientCertPath: @"C:\certs\client.pfx",
clientKeyPath: "123456",
serverCertPath: @"C:\certs\server.pfx");

Server Certificate Validation (Must Read)​

When serverCertPath is not passed there is no TOFU / fingerprint pinning at all. The SDK's actual behavior takes two paths:

ConfigurationActual BehaviorHas the server's identity been verified?
securityMode != None And so the story goes... serverCertPathLoad the certificate file you provided directly and use it to establish an encrypted connection.Yes — The channel is created based on the certificate you uploaded.
securityMode != None And not passed serverCertPathFirst, use None Plaintext Channel to call GetEndpoints to retrieve the certificate reported by the server, then use it to establish an encrypted connection.No — The certificate comes from the same untrusted plaintext link; a man-in-the-middle could replace it
securityMode == NonePlaintext throughoutNo

In other words: not passing serverCertPath means the server's identity is never verified — it is not "remembered after the first use". The SDK provides no ability to "save the first certificate and compare fingerprints later".

A few other pitfalls:

  1. If it's passed but fails to load, it will silently fall back. — If the certificate fails to load and only a single WARN message is displayed, the connection falls back to the plaintext path shown in the table above, and the connection is still successful. To ensure the certificate is actually being used, verify it yourself after connecting.
  2. serverCertPath recognizes only PFX (PKCS#12) — bare DER / PEM files cannot be imported. The EndpointDescription.ServerCertificate returned by OpcUaDiscovery.GetEndpoints is DER bytes; if you want to pin it, you must first package it into a PFX yourself.
  3. The server PFX must be passwordless. — Those with a password will PFX fail to load and then fall back to the silent degradation described in item 1.
  4. securityMode == None In this case, neither of these certificate parameters is involved., Usernames and passwords are also transmitted over the network in plain text.

For security: securityMode at least Sign + pass serverCertPath (a passwordless PFX obtained in advance from the administrator).

Full Configuration Structure​

public DarraOpcUa(
string endpointUrl,
MessageSecurityMode securityMode,
string securityPolicyUri,
string clientCertificatePfxPath,
string clientCertificatePassword,
string serverCertificatePath,
string username,
string password,
uint sessionTimeoutMs = 60_000,
uint requestTimeoutMs = 10_000,
uint connectTimeoutMs = 10_000);

Explicitly pass all fields; the difference from the simplified constructor is that it does not securityPolicyUri follow automatic securityMode inference—it only falls back to empty when passing an empty string #None. Suitable for scenarios where "construction is based on a result GetEndpoints factory":

var endpoints = OpcUaDiscovery.GetEndpoints("opc.tcp://server:4840");
var ep = endpoints.First(e =>
e.SecurityMode == MessageSecurityMode.SignAndEncrypt);

using var ua = new DarraOpcUa(
endpointUrl: ep.EndpointUrl,
securityMode: ep.SecurityMode,
securityPolicyUri: ep.SecurityPolicyUri,
clientCertificatePfxPath: @"C:\certs\client.pfx",
clientCertificatePassword: "123456", // It's the password PFX , not the private key file.
serverCertificatePath: @"C:\certs\server.pfx", // Pin the server certificate; pass null = do not verify the server's identity
username: "operator",
password: "secret");

Exception​

Construction Period (new DarraOpcUa(...)):

ExceptionReason
ArgumentNullExceptionendpointUrl null / empty string
OpcUaExceptionSession creation failed; StatusCode for specific status codes

Construction phase does not load or validate certificates — the certificate path is simply noted down first; the actual reading of the certificate file occurs at connection time.

Connection phase (ua.Connect()) performs the certificate loading; on failure an OpcUaException is thrown:

StatusCodeReason
BadCertificateInvalidClient Failed to load PFX (incorrect password / corrupted file)
raw 0x80AB0000 (BadInvalidArgument)Sign / SignAndEncrypt But it wasn't passed clientCertPath

Resource Management​

DarraOpcUa Implement IDisposable.You must use using or explicitly call Dispose:

// Recommendation: Use AutoDispose
using var ua = new DarraOpcUa(...);

// or explicitly
var ua = new DarraOpcUa(...);
try { /* ... */ }
finally { ua.Dispose(); }

Dispose() It is idempotent (safe to call multiple times). It immediately closes the session and releases all resources; it does not resend the "active disconnect" event Disconnected mentioned earlier Disconnect(). To ensure you receive this event, first explicitly close the session, then ua.Disconnect() call Dispose.

Next Step​