AvaloniaUI/Avalonia · error · InvalidOperationException
This should be used only in a binding expression
Error message
This should be used only in a binding expression
What it means
StreamBinding is a marker extension method: the compiled-binding visitor intercepts calls to StreamBinding<T>(Task<T>) inside a CompiledBinding lambda and replaces them with a stream node (StreamTask). If the method actually executes at runtime — i.e. it is called in plain code or in a reflection binding that the visitor never processes — it throws InvalidOperationException.
Source
Thrown at src/Avalonia.Base/Data/Core/StreamBindingExtensions.cs:12
using System;
using System.Threading.Tasks;
namespace Avalonia
{
public static class StreamBindingExtensions
{
internal static string StreamBindingName = "StreamBinding";
public static T StreamBinding<T>(this Task<T> @this)
{
throw new InvalidOperationException("This should be used only in a binding expression");
}
public static object StreamBinding(this Task @this)
{
throw new InvalidOperationException("This should be used only in a binding expression");
}
public static T StreamBinding<T>(this IObservable<T> @this)
{
throw new InvalidOperationException("This should be used only in a binding expression");
}
}
}
View on GitHub (pinned to 11c5427268)
Solutions
- Use StreamBinding only inside a CompiledBinding lambda, e.g. CompiledBinding.For(x => x.Loader.StreamBinding()).
- For reflection bindings, bind the Task directly and handle its completion in the view-model.
- Never invoke StreamBinding as a normal method.
Example fix
// before (runtime call - throws) var v = myTask.StreamBinding(); // after - only inside a compiled binding CompiledBinding.For(x => x.Loader.StreamBinding());
Defensive patterns
Strategy: validation
Validate before calling
// Ensure StreamBinding is only used inside a CompiledBinding lambda // (the visitor intercepts it). In plain code, drop the call: // bad: var v = task.StreamBinding(); // good: only CompiledBinding.For(x => x.Loader.StreamBinding());
Prevention
- Treat StreamBinding as a compile-time marker, not a runtime method.
- Use it only inside CompiledBinding lambdas.
- For reflection bindings, bind the Task and handle completion in the view-model.
When it happens
Trigger: Calling task.StreamBinding() directly in code-behind; using StreamBinding inside a reflection (string) Binding whose expression is not walked by BindingExpressionVisitor.
Common situations: Confusing the compiled-binding-only marker with a real runtime helper; using StreamBinding in a ReflectionBinding or plain C# and expecting it to return a value.
Related errors
- Invalid expression type in binding expression: {node.NodeTyp
- Invalid indexer in binding expression: {node.NodeType}.
- Invalid method call in binding expression: '{node.Method.Dec
- Catch blocks are not allowed in binding expressions.
- Dynamic expressions are not allowed in binding expressions.
AI-assisted analysis of AvaloniaUI/Avalonia@11c5427268 (2026-08-13).
Data as JSON: /api/errors/4fdced3caa75a6cd.
Report an issue: GitHub.