Skip to main content

MethodBuilder — Method: Calling a Stream Constructor

Overview​

OPC UA Call The service (Part 4 §5.11) requires that the input parameters be packaged into Variant[], with the order corresponding exactly to the server-side description InputArguments. Writing this by hand can Variant[] easily lead to mistakes:

  • Order Misalignment (speed and accel Reversal)
  • Type Mismatch (The server expects Int32, but the client wrote Double)
  • Missing Arguments / Extra Arguments

MethodBuilder Provide a list API, string them together in a single line LoadInputArgs / Arg(name, value) / Invoke, and automatically sort them by type .NET Variant.Set*.

Corresponding section of the specification: Part 4 §5.11.2 (Call) / Part 5 §12.3 (Argument).

API​

MethodsCategoryReading and WritingNote
new MethodBuilder(ua, objectId, methodId)Structure—Bound to a method
.LoadInputArgs()Chain-typeReadCheck whether the InputArguments property exists on the server (do not throw an exception on failure)
.Arg(string name, object value)Chain-type—Set Values by Parameter Name
.Arg(int index, object value)Chain-type—Set values by index (starting at 0)
.Invoke()TerminalWriteCall the method, return MethodCallResult
.InputArgsPropertiesReadList of Input Parameter Descriptions
.InputArgsLoadedPropertiesReadWhether the InputArguments property is detected

MethodCallResult Three members: Status (StatusCode) / Outputs (IReadOnlyList<Variant>, the caller is responsible for Dispose) / IsGood (bool).

Automatic wrapping of .NET Types: bool / sbyte / byte / short / ushort / int / uint / long / ulong / float / double / string / DateTime, and arrays bool[] / int[] / uint[] / long[] / float[] / double[] / string[]; Passed Variant as-is; all other types are ToString() converted to strings.

Code Example​

using System;
using System.Threading;
using DarraOpcUa_Client;

using var ua = new DarraOpcUa("opc.tcp://localhost:4840");
ua.Connect();

// 1) Set parameters by index (recommended)
var r = new MethodBuilder(ua, "ns=2;s=Robot", "ns=2;s=Robot.MoveTo")
.LoadInputArgs()
.Arg(0, 100.0)
.Arg(1, 200.0)
.Arg(2, 50)
.Invoke();
foreach (var v in r.Outputs) v.Dispose(); // Output: Must be disposed of by Variant the caller

if (!r.IsGood)
Console.WriteLine($"MoveTo 失败: {r.Status}");

// 2) Read the output
var add = new MethodBuilder(ua, "ns=2;s=Calculator", "ns=2;s=Calculator.Add")
.Arg(0, 3.14)
.Arg(1, 2.71)
.Invoke();
if (add.IsGood && add.Outputs.Count > 0)
Console.WriteLine($"Add → {add.Outputs[0].AsDouble}"); // AsDouble Is it a double?
foreach (var v in add.Outputs) v.Dispose();

// 3) Reuse the builder for multiple calls
var b = new MethodBuilder(ua, "ns=2;s=Pump", "ns=2;s=Pump.SetSpeed")
.LoadInputArgs();

foreach (var rpm in new[] { 100, 500, 1000, 1500 })
{
var s = b.Arg(0, rpm).Invoke();
foreach (var v in s.Outputs) v.Dispose();
if (!s.IsGood) break;
Thread.Sleep(500);
}

Comparison with Call​

// Hand-assembled Variant[]
var inputs = new Variant[3];
inputs[0] = new Variant(); inputs[0].Double = 100.0;
inputs[1] = new Variant(); inputs[1].Double = 200.0;
inputs[2] = new Variant(); inputs[2].Int32 = 50;

var outs = ua.Call("ns=2;s=Robot", "ns=2;s=Robot.MoveTo", inputs);

// MethodBuilder
var r = new MethodBuilder(ua, "ns=2;s=Robot", "ns=2;s=Robot.MoveTo")
.Arg(0, 100.0)
.Arg(1, 200.0)
.Arg(2, 50)
.Invoke();
// The inputs and outputs in both paths Variant belong to the caller; they must be disposed of after use.

Implementation Constraints​

  • LoadInputArgs() Perform existence checks only: The elements InputArgs in are placeholders (Name in the form of arg0 / arg1, Description / DataType is empty, BuiltInType is Null, ValueRank is -1), Unable to obtain the actual parameter names and types from the server. Indexing is not Arg(int, value) affected.
  • Alignment with the placeholder names Arg(string name, value) in and InputArgs is required for this to work; if they do not match, values are filled in based on the Arg insertion order in.
  • Complex structures are not supported as input parameters; only basic types and the arrays listed in the table above are supported.
  • Failed server calls will be collapsed into MethodCallResult.Status + empty Outputs, Do not throw exceptions; However, if an input parameter conversion error occurs (a custom type ToString() throws an exception), the exception will still be propagated outward, requiring the caller to try/catch catch it.
  • Construction and Arg Throws parameter exceptions: ua / objectId / methodId is null → ArgumentNullException; Arg(int) passes a negative index → ArgumentOutOfRangeException; Arg(string) passes an empty name → ArgumentNullException

Best Practices​

  • Set Parameters by Index — The actual parameter name cannot be obtained InputArgs here; Arg(int, value) this is a reliable path
  • LoadInputArgs before the call — Let Invoke know the total number of parameters to avoid missing or extra parameters
  • Reusing the builder — To call the same method multiple times, simply create a builder and modify only the values Arg.
  • Judgment IsGood — Don't assume that a call will always succeed
  • Do not use thread UI synchronization — Invoke Yes RPC, use await Task.Run(() => builder.Invoke())

Cross-Language Comparison​

C#PythonJavaC++RustC
MethodBuilderMethodBuilderMethodBuilderMethodBuilderMethodBuilderDarraUa_MethodBuilder_*
.Arg(name, value).arg(name, value).arg(name, value).Arg(name, value).arg(name, value)DarraUa_MethodBuilder_ArgDouble, etc. (by type)
.Invoke().invoke().invoke().Invoke().invoke()DarraUa_MethodBuilder_Invoke

Next Step​