{"record":{"id":"034f6165131d002b","repo":"microsoft/aspire","slug":"referenceexpression-instances-can-t-be-used-in-interpolated","errorCode":null,"errorMessage":"ReferenceExpression instances can't be used in interpolated string with a custom format. Duplicate the inner expression in-place.","messagePattern":"ReferenceExpression instances can't be used in interpolated string with a custom format\\. Duplicate the inner expression in-place\\.","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"src/Aspire.Hosting/ApplicationModel/ReferenceExpression.cs","lineNumber":389,"sourceCode":"        /// Appends a formatted value to the expression. The value must implement <see cref=\"IValueProvider\"/> and <see cref=\"IManifestExpressionProvider\"/>.\n        /// </summary>\n        /// <param name=\"valueProvider\">An instance of an object which implements <see cref=\"IValueProvider\"/> and <see cref=\"IManifestExpressionProvider\"/>.</param>\n        /// <exception cref=\"InvalidOperationException\"></exception>\n        public void AppendFormatted<T>(T valueProvider) where T : IValueProvider, IManifestExpressionProvider\n        {\n            AppendFormatted(valueProvider, format: null);\n        }\n\n        /// <summary>\n        /// Appends the formatted value provided by the specified reference expression to the output.\n        /// </summary>\n        /// <param name=\"valueProvider\">A reference expression that supplies the value to be formatted and appended.</param>\n        /// <param name=\"format\">A composite format string that specifies how the value should be formatted, or null to use the default format.</param>\n        /// <remarks>This method is marked obsolete only to prevent usages of this type explicitly.</remarks>\n        [Obsolete(\"ReferenceExpression instances can't be used in interpolated string with a custom format. Duplicate the inner expression in-place.\", error: true)]\n        public void AppendFormatted(ReferenceExpression valueProvider, string format)\n        {\n            throw new InvalidOperationException(\"ReferenceExpression instances can't be used in interpolated string with a custom format. Duplicate the inner expression in-place.\");\n        }\n\n        /// <summary>\n        /// Appends a formatted value to the expression. The value must implement <see cref=\"IValueProvider\"/> and <see cref=\"IManifestExpressionProvider\"/>.\n        /// </summary>\n        /// <param name=\"valueProvider\">An instance of an object which implements <see cref=\"IValueProvider\"/> and <see cref=\"IManifestExpressionProvider\"/>.</param>\n        /// <param name=\"format\">The format to be applied to the value. e.g., \"uri\"</param>\n        /// <exception cref=\"InvalidOperationException\"></exception>\n        public void AppendFormatted<T>(T valueProvider, string? format = null) where T : IValueProvider, IManifestExpressionProvider\n        {\n            var index = _valueProviders.Count;\n            _builder.Append(CultureInfo.InvariantCulture, $\"{{{index}}}\");\n\n            _valueProviders.Add(valueProvider);\n            _manifestExpressions.Add(valueProvider.ValueExpression);\n            _stringFormats.Add(format);\n        }\n","sourceCodeStart":371,"sourceCodeEnd":407,"githubUrl":"https://github.com/microsoft/aspire/blob/25830f84bd145686607ad00c057b3f84e2e51d43/src/Aspire.Hosting/ApplicationModel/ReferenceExpression.cs#L371-L407","documentation":"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.","triggerScenarios":"An interpolated string handler invocation like $\"{expression:N2}\" where expression is a ReferenceExpression and a format specifier follows the colon.","commonSituations":"Migrating from string interpolation to ReferenceExpressionBuilder while keeping existing format specifiers; assuming ReferenceExpression supports IFormattable-style formats.","solutions":["Remove the format specifier and interpolate the ReferenceExpression plain: {expression}","Format the raw value before building the expression, or append a separate expression produced with the desired format","Use ReferenceExpression.Create with the formatted value embedded"],"exampleFix":"// before\nvar expr = ReferenceExpression.Create($\"{timestampExpr:yyyy-MM-dd}\");\n// after\nvar expr = ReferenceExpression.Create($\"{timestampExpr}\");\n// or pre-format the value:\nvar expr = ReferenceExpression.Create($\"{timestamp:yyyy-MM-dd}\"); // timestamp is DateTime","handlingStrategy":"validation","validationCode":"// compile-time: the [Obsolete error] fails the build; keep interpolation format-free\n// runtime check not applicable","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Never add ':format' specifiers after ReferenceExpression arguments in interpolated strings","Format raw values before creating the expression"],"tags":["reference-expression","interpolation","aspire-hosting"],"backgroundTag":"deprecated-api-usage","analyzedSha":"25830f84bd145686607ad00c057b3f84e2e51d43","analyzedAt":"2026-09-16T11:10:06.193Z","contentChangedAt":"2026-09-16T11:10:06.193Z","schemaVersion":2},"datasetVersion":"2026-09-21T04:17:39.646Z"}