Skip to main content

Call

Preliminary / Supporting
  • Data Types Variant It is a container for input and output parameters.
  • Find a way to use NodeId Browse Pair with filter: NodeClass.Method.
  • Don't want to type it out by hand? Variant[] Use MethodBuilder Chain-like structure.

Signature​

public IReadOnlyList<Variant> Call(
string objectNodeId,
string methodNodeId,
params Variant[] inputs);
ParametersNote
objectNodeIdThe method is located in the Object node
methodNodeIdThe Method Itself NodeId
inputsInput: Variant Array (optional)

Returns List of output IReadOnlyList<Variant> parameters.

Memory Ownership (Important)​

In the returned list, Each Variant one implements IDisposable, You must dispose of them one by one; otherwise, a memory leak will occur.

Opposite input parameters: Call No takeoverinputs Ownership Variant of the object remains Variant with the caller, and the caller is responsible for calling Dispose.

Recommended wording:

var outputs = ua.Call(objId, mtdId, args);
foreach (var v in outputs) using (v) { /* Using v.AsXxx etc. */ }

Or explicitly:

var outputs = ua.Call(objId, mtdId, args);
try { /* Use outputs[i] */ }
finally { foreach (var v in outputs) v.Dispose(); }

Examples​

// Adjust Calculator.Add(3, 4)
using var x = new Variant(); x.Int32 = 3;
using var y = new Variant(); y.Int32 = 4;

var outputs = ua.Call(
objectNodeId: "ns=2;s=Calculator",
methodNodeId: "ns=2;s=Calculator.Add",
x, y);

foreach (var v in outputs) using (v)
Console.WriteLine($"sum = {v.AsInt32 ?? 0}"); // AsInt32 Is it an int? The return value is null Int32 when it is not an int.

Exception​

ExceptionMeaning
ArgumentNullExceptionobjectNodeId / methodNodeId is null or an empty string
OpcUaException(BadNodeIdInvalid)objectNodeId / methodNodeId cannot be resolved locally
OpcUaException (Transmission Return Code)Transmission layer failure—this ex.StatusCode is the transmission status code returned by this call (common: BadCommunicationError / BadTimeout)
OpcUaException(Business Status Code)The transmission was successful, but the method did not return "Good," ex.StatusCode meaning the business code provided by the server

Session-level failures (BadSessionClosed / BadSessionIdInvalid / BadServerNotConnected / BadConnectionClosed / BadSecureChannelClosed) will automatically attempt one reconnection and one retry; if they still fail, an exception is thrown. Node-level errors are not retried; an exception is thrown immediately.

Business-level failures (when a method returns a value other than "Good") also throw exceptions— ex.StatusCode specifically, business codes. We do not want to use these in the event of an exception. CallSafe, It wraps the exception into a tuple (StatusCode Status, IReadOnlyList<Variant> Results): in case of failure, Status is the status code from the exception, and Results is an empty list.

Implementation Constraints​

  • Synchronous, blocking call, once RPC; the output parameters can hold up to max(入参个数, 32) items; if the server returns more, the excess items are not included in the return list.
  • Objects are all Variant created SDK and then handed over to you; there is no such thing as "automatic release after use," so failing to call Dispose will result in a memory leak.
  • Output parameters have no names and can only be referenced by order; parameter names and types depend on InputArguments / OutputArguments attributes, but SDK currently cannot decode them (see the next section).

Looking for a way to... NodeId​

// 1. Browse Object Child Node Filtering Method
var methods = ua.Browse("ns=2;s=Calculator", filter: NodeClass.Method);
foreach (var m in methods)
Console.WriteLine($" {m.BrowseName} -> {m.NodeId}");

// 2. Check the properties of the method node (InputArguments / This is only available OutputArguments if provided by the server)
var props = ua.Browse(methods[0].NodeId);
foreach (var p in props) Console.WriteLine($" prop {p.BrowseName} -> {p.NodeId}");

InputArguments / OutputArguments is a property on the method node, and its content is Argument[]; I can't decipher this encoding right SDK now, soUnable to retrieve parameter name / data type, Parameters must be passed in order Variant. To use the "pass parameters by name" syntax, use MethodBuilder, It accepts Arg(int index, ...) index-based assignments.

Input Parameter Construction​

Variant Only parameterless constructors; assign values using type setters based on method signatures:

// Add(Int32, Int32) -> Int32
using var a = new Variant(); a.Int32 = 3;
using var b = new Variant(); b.Int32 = 4;

// SetTemperature(string deviceId, double value)
using var id = new Variant(); id.String = "Boiler1";
using var val = new Variant(); val.Double = 85.5;

// With array parameters
using var arr = new Variant(); arr.SetDoubleArray(new[] { 1.0, 2.0, 3.0 });

Complex Structures (ExtensionObject) No input parameters are currently provided.

Synchronous Call​

Call This is synchronous and blocking (send a request + wait for a response); asynchronous entry points are not currently available. If you need asynchronous behavior, you'll need to wrap it yourself:

var result = await Task.Run(() => ua.Call(objId, mtdId, args));

Next Step​