Discovery 发现
OpcUaDiscovery 是静态类, 不需要 Session 即可调用. 用于"连接前先探测":
子页跳转
- 列出某 Server 全部 Endpoint 请参考 GetEndpoints.
- 通过 LDS 列出域内全部 Server 请参考 FindServers.
API
两个方法都是 OpcUaDiscovery 的 public static 方法, 不需要 Session、不需要实例:
public static IReadOnlyList<EndpointDescription> GetEndpoints(
string endpointUrl, uint timeoutMs = 10_000);
public static IReadOnlyList<ApplicationDescription> FindServers(
string endpointUrl, uint timeoutMs = 10_000);
| 方法 | 类别 | 读写 | 说明 |
|---|---|---|---|
| OpcUaDiscovery.GetEndpoints(endpointUrl, timeoutMs) | 发现 | 读 | 列出该 endpoint 公布的全部 EndpointDescription |
| OpcUaDiscovery.FindServers(endpointUrl, timeoutMs) | 发现 | 读 | 通过 LDS 列出域内全部 ApplicationDescription |
两者行为一致的部分:
endpointUrl为 null / 空串 →ArgumentNullException;- 只走一次 TCP → Hello/Ack → OPN(None) → 请求 → CLO, 不建立 Session;
- 返回非
StatusCode.Good→ 抛OpcUaException,ex.StatusCode即服务端返回值; - 结果列表可能为空 (
Array.Empty), 不抛异常。
用法模式
using System;
using System.Linq;
using DarraOpcUa_Client;
// 1. 先 GetEndpoints 看服务端支持哪些组合
var endpoints = OpcUaDiscovery.GetEndpoints("opc.tcp://server:4840", timeoutMs: 10_000);
// 2. 挑一个匹配的 (例如 SignAndEncrypt + Username)
var ep = endpoints.First(e =>
e.SecurityMode == MessageSecurityMode.SignAndEncrypt &&
e.UserTokens.Any(t => t.TokenType == UserTokenType.Username));
// 3. 用挑出来的 Endpoint 真正连接
var (pfxPath, pfxPassword) = CertificateManager.EnsureClientCertificate();
using var ua = new DarraOpcUa(
endpointUrl: ep.EndpointUrl,
securityMode: ep.SecurityMode,
securityPolicyUri: ep.SecurityPolicyUri,
username: "operator", password: "secret",
clientCertPath: pfxPath,
clientKeyPath: pfxPassword, // 这里放 PFX 的密码, 不是私钥文件路径
serverCertPath: null); // 留空 = 从 GetEndpoints 自动取服务端证书 (见 Security 页)
ua.Connect();