Namespaces — ua.Namespaces
OPC UA A node consists NamespaceIndex + Identifier of NodeId, but NamespaceIndex Unstable — A server restart may cause changes. Production code must use NamespaceUri reverse lookup indexes; do not hard-code them ns=2.
- Standard = ns0; please refer to the node constants WellKnownNodes.
- For information on node and Browse path resolution, please refer to Nodes.
NamespaceCollection Public Members
DarraOpcUa.Namespaces is NamespaceCollection, it is automatically loaded from the server NamespaceArray (i=2255) when connecting.
| Category | Property | Type | Access | Description |
|---|---|---|---|---|
| Properties | Count | int | R | Number of namespaces |
this[int i] | string | R | Index-based access; returns the URI corresponding to this Index |
NamespaceCollection Implement it IEnumerable<string>; sorting by Index covers foreach everything URI.
Methods
IndexOf(uri)→ int / -1— Reverse lookup URI → Index; if not found, return -1Reload()→ StatusCode— Proactively re-read the server NamespaceArray (when the server changes dynamically), return a status code
Common Uses
Reverse Lookup: Index Spelling NodeId
using System;
using DarraOpcUa_Client;
ua.Connect();
int idx = ua.Namespaces.IndexOf("urn:my-company:plc1");
if (idx < 0) throw new InvalidOperationException("Server does not have my namespace");
string nodeId = $"ns={idx};s=Temperature";
using var dv = ua.Read(nodeId);
List all namespaces (debug)
int i = 0;
foreach (var uri in ua.Namespaces) // NamespaceCollection Implementation IEnumerable<string>
Console.WriteLine($" [{i++}] {uri}");
Output example:
[0] http://opcfoundation.org/UA/
[1] urn:server-host
[2] urn:my-company:plc1
[3] http://my-company.com/UA/
Dynamic Namespace Changes
Some servers register their namespaces only after they start up (plug-in style), so Connect does not receive the complete NamespaceArray set. In this case, call the re-read Reload() method:
if (ua.Namespaces.Reload() != StatusCode.Good) // Reload returns StatusCode
Console.WriteLine("重读 NamespaceArray 失败");
int idx = ua.Namespaces.IndexOf("urn:plugin:loaded-after-startup");
Standard namespace (ns=0)
OPC Foundation It is specified that Index 0 is always http://opcfoundation.org/UA/, and all standard nodes have ns=0:
| Node | NodeId | Note |
|---|---|---|
| Root | i=84 | Root |
| Objects | i=85 | Business Data Root |
| Types | i=86 | Type Definition Root |
| Server | i=2253 | The Server Object Itself |
| ServerStatus | i=2256 | Server Status Structure |
| ServerStatus.CurrentTime | i=2258 | Current server time |
| NamespaceArray | i=2255 | Namespace Array |
| ServerArray | i=2254 | Server-side array |
For more information, see WellKnownNodes.