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

  1. Use StreamBinding only inside a CompiledBinding lambda, e.g. CompiledBinding.For(x => x.Loader.StreamBinding()).
  2. For reflection bindings, bind the Task directly and handle its completion in the view-model.
  3. 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

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


AI-assisted analysis of AvaloniaUI/Avalonia@11c5427268 (2026-08-13). Data as JSON: /api/errors/4fdced3caa75a6cd. Report an issue: GitHub.