Skip to main content

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.

Prerequisite Reading
  • 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.

CategoryPropertyTypeAccessDescription
PropertiesCountintRNumber of namespaces
this[int i]stringRIndex-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 -1
  • Reload()→ 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:

NodeNodeIdNote
Rooti=84Root
Objectsi=85Business Data Root
Typesi=86Type Definition Root
Serveri=2253The Server Object Itself
ServerStatusi=2256Server Status Structure
ServerStatus.CurrentTimei=2258Current server time
NamespaceArrayi=2255Namespace Array
ServerArrayi=2254Server-side array

For more information, see WellKnownNodes.

Next Step​