C# / .NET · recipe
Call an OPC UA method in C#
session.Call takes the owning object's node id, the method's node id and the input arguments. It returns the output arguments, and throws a ServiceResultException carrying the status code when the call is refused.
| Stack | OPCFoundation/UA-.NETStandard |
|---|---|
| Version | 1.5.378.156 stable, 2.0.0-preview.2 |
The code
var objectId = new NodeId("Methods", (ushort)nsIndex);
var methodId = new NodeId("Methods/sqrt(x)", (ushort)nsIndex);
try
{
IList<object> outputs = session.Call(objectId, methodId, 16.0);
Console.WriteLine($"outputs: {outputs.Count}");
foreach (var o in outputs) Console.WriteLine($" {o}");
}
catch (ServiceResultException ex)
{
Console.WriteLine($"call refused: {ex.StatusCode} - {ex.Message}");
}
What it prints
outputs: 1
4
Notes
- session.Call is the convenience wrapper; the underlying CallRequest lets you batch several method calls in one round trip if you need to.
- A refused call raises ServiceResultException. Read ex.StatusCode rather than the message: the code is what tells you whether it was arguments, permissions or state.
- Argument types must match the method's declared InputArguments. Passing 16 rather than 16.0 sends an Int32 where a Double is expected.
- Check the Executable attribute if a method exists but will not run: some methods are only callable in certain machine states.
What breaks first
The method exists but the call is rejected
Read the method's InputArguments property for the declared count, order and types, and match them exactly including scalar versus array.
BadInvalidArgumentRead the method's InputArguments property for the declared count, order and types, and match them exactly including scalar versus array.
Other recipes in this stack
Concepts on this page
Last verified 7 September 2026 against OPC UA 1.05.07. OPC UA Connect is an educational resource and is not affiliated with the OPC Foundation.