Constructor
DarraOpcUa Two overloads are provided: a simplified constructor (common parameters + default inferences) and a fully configured constructor (all fields passed explicitly).
- For encryption parameters (SecurityMode / SecurityPolicyUri / certificate), see Security Encryption.
- See "Server-Side Pre-Scan" Discovery.
- For instructions on what to do after connecting, see Connection Management.
Simplified Construction (Recommended)
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
| Category | Property | Type | Access | Description |
|---|---|---|---|---|
| Required | endpointUrl | string | in | OPC UA Endpoint, in the format opc.tcp://host:port |
| Security | securityMode | MessageSecurityMode | in | None / Sign / SignAndEncrypt, Default: None |
securityPolicyUri | string | in | If not specified, the mode is selected automatically (None → #None, others → #Basic256Sha256) | |
clientCertPath | string | in | Client PFX path (Sign / SignAndEncrypt required; if missing, Connect fails immediately) | |
clientKeyPath | string | in | ⚠ **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 | |
serverCertPath | string | in | Server certificate **PFX** file path (PKCS#12, and must be passwordless — see "Server Certificate Validation" below). ⚠ Not providing it **is not** TOFU | |
| Login | username | string | in | null = Anonymous, non-null = UsernameIdentityToken |
password | string | in | Used with the username; if not provided, treated as an empty string | |
| Timeout | sessionTimeoutMs | uint | in | Server-side session timeout; default is 60 000 ms (60 seconds) |
requestTimeoutMs | uint | in | Single RPC timeout, default 10 000 ms (10 seconds) | |
connectTimeoutMs | uint | in | Connection 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:
| Configuration | Actual Behavior | Has the server's identity been verified? |
|---|---|---|
securityMode != None And so the story goes... serverCertPath | Load 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 serverCertPath | First, 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 == None | Plaintext throughout | No |
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:
- 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.
serverCertPathrecognizes only PFX (PKCS#12) — bare DER / PEM files cannot be imported. TheEndpointDescription.ServerCertificatereturned byOpcUaDiscovery.GetEndpointsis DER bytes; if you want to pin it, you must first package it into a PFX yourself.- 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.
securityMode == NoneIn 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(...)):
| Exception | Reason |
|---|---|
ArgumentNullException | endpointUrl null / empty string |
OpcUaException | Session 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:
| StatusCode | Reason |
|---|---|
BadCertificateInvalid | Client 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.