构造函数
DarraOpcUa 提供两个重载: 简化构造 (常用参数 + 默认推导) 和全配置构造 (显式传所有字段).
前置阅读
- 加密参数 (SecurityMode / SecurityPolicyUri / 证书) 见 Security 加密.
- 服务端预扫描见 Discovery.
- 连上后的操作见 连接管理.
简化构造 (推荐)
public DarraOpcUa(
string endpointUrl,
MessageSecurityMode securityMode = MessageSecurityMode.None,
string username = null,
string password = null,
uint sessionTimeoutMs = 600_000,
uint requestTimeoutMs = 10_000,
uint connectTimeoutMs = 10_000,
string clientCertPath = null,
string clientKeyPath = null,
string serverCertPath = null,
string securityPolicyUri = null);
参数
| 类别 | 属性 | 类型 | 访问 | 说明 |
|---|---|---|---|---|
| 必填 | endpointUrl | string | in | OPC UA endpoint, 形如 opc.tcp://host:port |
| 安全 | securityMode | MessageSecurityMode | in | None / Sign / SignAndEncrypt, 默认 None |
securityPolicyUri | string | in | 不传按 mode 自动选 (None → #None, 其他 → #Basic256Sha256) | |
clientCertPath | string | in | 客户端 PFX 路径 (Sign / SignAndEncrypt 必填) | |
clientKeyPath | string | in | PFX 密码 (字段复用) | |
serverCertPath | string | in | 服务端 DER, 不传则 TOFU (Trust On First Use) | |
| 登录 | username | string | in | null = Anonymous, 非空 = UsernameIdentityToken |
password | string | in | 配 username 用 | |
| 超时 | sessionTimeoutMs | uint | in | 服务端 Session 超时, 默认 600 000 ms (10 分钟) |
requestTimeoutMs | uint | in | 单 RPC 超时, 默认 10 000 ms (10 秒) | |
connectTimeoutMs | uint | in | 建链超时, 默认 10 000 ms (10 秒) |
例子
// 1. 匿名 + 明文 (开发期)
using var ua = new DarraOpcUa("opc.tcp://localhost:4840");
// 2. 匿名 + Sign 加密
using var ua = new DarraOpcUa("opc.tcp://server:4840",
securityMode: MessageSecurityMode.Sign,
clientCertPath: @"C:\certs\client.pfx",
clientKeyPath: "123456");
// 3. 用户名 + SignAndEncrypt (典型生产)
using var ua = new DarraOpcUa("opc.tcp://server:4840",
securityMode: MessageSecurityMode.SignAndEncrypt,
username: "operator",
password: "secret",
clientCertPath: @"C:\certs\client.pfx",
clientKeyPath: "123456");
全配置构造
public DarraOpcUa(
string endpointUrl,
MessageSecurityMode securityMode,
string securityPolicyUri,
string clientCertificatePfxPath,
string clientCertificatePassword,
string serverCertificatePath,
string username,
string password,
uint sessionTimeoutMs = 600_000,
uint requestTimeoutMs = 10_000,
uint connectTimeoutMs = 10_000);
显式传所有字段, 没有默认推导. 适合"按 GetEndpoints 结果工厂构造"的场景:
var endpoints = Discovery.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",
serverCertificatePath: null, // TOFU
username: "operator",
password: "secret");
异常
| 异常 | 原因 |
|---|---|
ArgumentNullException | endpointUrl 为空 |
OpcUaException(BadConfigurationError) | Stack 初始化失败 |
OpcUaException(BadCertificateInvalid) | 客户端证书加载失败 |
资源管理
DarraOpcUa 实现 IDisposable, 内部维护引用计数的 native Stack 初始化. 必须用 using 或显式 Dispose:
// 推荐: using 自动 Dispose
using var ua = new DarraOpcUa(...);
// 或显式
var ua = new DarraOpcUa(...);
try { /* ... */ }
finally { ua.Dispose(); }
Dispose() 内部自动调用 Disconnect(), 一般 using 即可.