microsoft / microsoft/aspire

Protect against using `ReferenceExpression` in a regular interpoalted string

Open
#7,612 1 comment 0 reactions 0 assignees View on GitHub
area-app-model
Dominant language
C#
Stars
6.3k
Forks
991
Avg merge
2d 15h
Merged PRs (30d)
196

Description

### Is there an existing issue for this?

- [x] I have searched the existing issues

### Is your feature request related to a problem? Please describe the problem.

I've had a few collegues needing to do things with `ReferenceExpression` which don't work because they unintentionally worked the ReferenceExpression into a regular interpolated string e.g.

```cs
kafka.WithEnvironment("KAFKA_ADVERTISED_LISTENERS",
$"PLAINTEXT://localhost:29092," +
$"PLAINTEXT_HOST://localhost:{kafka.GetEndpoint("primary").Property(EndpointProperty.Port)}")}," +
```

Which fails because the reference expression ultimately ends up being serialized as the literal string `Aspire.Hosting.ApplicationModel.ReferenceExpression` rather than the desired port

### Describe the solution you'd like

It would be nice if we could do something to make these errors more obvious. A few possibiliteis off the top of my head

1. Have `ReferenceExpression.ToString()` throw an error - it seems like you should only ever stringify a reference expression with `.GetValueAsync()` or `.ValueExpression`, and never `.ToString()`. Would probaby require adding `[DebuggerDisplay("{ValueExpression}")]` to stop the debugger complaining

2. An analyser that detects when `ReferenceExpression` is used inside an interpolated string that isn't itself used in an interpolated expression, and produce a compiler warning.

First option is the easier implementation, but having `.ToString()` throw could feels sketchy - e.g. what if a `ReferenceExpression` ever ends up being passed to a logging framework.

### Additional context

_No response_

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.