microsoft/aspire · error · InvalidOperationException

ReferenceExpression instances can't be used in interpolated…

Error message

ReferenceExpression instances can't be used in interpolated string with a custom format. Duplicate the inner expression in-place.

What it means

ReferenceExpression is an opaque value that already embeds its own formatting; using it as an interpolation argument with a custom format string (e.g. {expr:yyyy}) is meaningless, so the compiler-targeted AppendFormatted(ReferenceExpression, string) overload is marked [Obsolete(error: true)] and always throws. The real failure is a compile error from the obsolete attribute; the runtime throw is a backstop.

Solutions

  1. Remove the format specifier and interpolate the ReferenceExpression plain: {expression}
  2. Format the raw value before building the expression, or append a separate expression produced with the desired format
  3. Use ReferenceExpression.Create with the formatted value embedded

Example fix

// before
var expr = ReferenceExpression.Create($"{timestampExpr:yyyy-MM-dd}");
// after
var expr = ReferenceExpression.Create($"{timestampExpr}");
// or pre-format the value:
var expr = ReferenceExpression.Create($"{timestamp:yyyy-MM-dd}"); // timestamp is DateTime
Defensive patterns

Strategy: validation

Validate before calling

// compile-time: the [Obsolete error] fails the build; keep interpolation format-free
// runtime check not applicable

Prevention

When it happens

Trigger: An interpolated string handler invocation like $"{expression:N2}" where expression is a ReferenceExpression and a format specifier follows the colon.

Common situations: Migrating from string interpolation to ReferenceExpressionBuilder while keeping existing format specifiers; assuming ReferenceExpression supports IFormattable-style formats.

Understand the failure class

Background: "is deprecated and will be removed" — deprecation warnings for old API names, keywords, and options, and how to migrate before the removal release — this error's family across 29 libraries.

Related errors


AI-assisted analysis of microsoft/aspire@25830f84bd (2026-09-16). Data as JSON: /api/errors/034f6165131d002b. Report an issue: GitHub.

Appendix: source

Thrown at src/Aspire.Hosting/ApplicationModel/ReferenceExpression.cs:389

        /// Appends a formatted value to the expression. The value must implement <see cref="IValueProvider"/> and <see cref="IManifestExpressionProvider"/>.
        /// </summary>
        /// <param name="valueProvider">An instance of an object which implements <see cref="IValueProvider"/> and <see cref="IManifestExpressionProvider"/>.</param>
        /// <exception cref="InvalidOperationException"></exception>
        public void AppendFormatted<T>(T valueProvider) where T : IValueProvider, IManifestExpressionProvider
        {
            AppendFormatted(valueProvider, format: null);
        }

        /// <summary>
        /// Appends the formatted value provided by the specified reference expression to the output.
        /// </summary>
        /// <param name="valueProvider">A reference expression that supplies the value to be formatted and appended.</param>
        /// <param name="format">A composite format string that specifies how the value should be formatted, or null to use the default format.</param>
        /// <remarks>This method is marked obsolete only to prevent usages of this type explicitly.</remarks>
        [Obsolete("ReferenceExpression instances can't be used in interpolated string with a custom format. Duplicate the inner expression in-place.", error: true)]
        public void AppendFormatted(ReferenceExpression valueProvider, string format)
        {
            throw new InvalidOperationException("ReferenceExpression instances can't be used in interpolated string with a custom format. Duplicate the inner expression in-place.");
        }

        /// <summary>
        /// Appends a formatted value to the expression. The value must implement <see cref="IValueProvider"/> and <see cref="IManifestExpressionProvider"/>.
        /// </summary>
        /// <param name="valueProvider">An instance of an object which implements <see cref="IValueProvider"/> and <see cref="IManifestExpressionProvider"/>.</param>
        /// <param name="format">The format to be applied to the value. e.g., "uri"</param>
        /// <exception cref="InvalidOperationException"></exception>
        public void AppendFormatted<T>(T valueProvider, string? format = null) where T : IValueProvider, IManifestExpressionProvider
        {
            var index = _valueProviders.Count;
            _builder.Append(CultureInfo.InvariantCulture, $"{{{index}}}");

            _valueProviders.Add(valueProvider);
            _manifestExpressions.Add(valueProvider.ValueExpression);
            _stringFormats.Add(format);
        }

View on GitHub (pinned to 25830f84bd)