microsoft / microsoft/aspire

Default .NET web API template does not play well with Aspire service discovery

Open
#10,549 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

### Describe the bug

The default .NET web API template includes the call to `UseHttpsRedirection()` as part of the initialization. This does not seem to be handled well by Aspire service discovery. Observed problems with YARP proxy integration.

### Expected Behavior

_No response_

### Steps To Reproduce

1. Create a new Aspire starter application.
2. Add another web API project (let's call it "auxapi1") and hook it up to Aspire application host.
3. To be able to easily distinguish between the API services in the app, I added an "info" request handler to both:

```csharp
app.MapGet("/info", () =>
{
return new
{
Name = "ApiService",
Version = "1.0.0",
Description = "This is the main API service." // Change as necessary
};
}).WithName("GetInfo");
```

4. Add a reference to Aspire.Hosting.Yarp package and add the following code to app host:

```csharp
builder.AddYarp("yarp")
.WithExternalHttpEndpoints()
.WithConfiguration(yc =>
{
var e1 = auxapi1.GetEndpoint("http");
yc.AddRoute("/api1/info", e1).WithTransformPathSet("/info");
var e3 = apiService.GetEndpoint("http");
yc.AddRoute("/api/info", e3).WithTransformPathSet("/info");
});
```

5. Run the app, open YARP endpoint and try hitting `/api/info` and `/api1/info`

Observe that the original API can be reached, but the new API cannot, because YARP is rewriting `localhost` to `host.docker.internal` (default host for container-based services, which YARP is).

6. Remove the call to `UseHttpsRedirection()` from the code of the new API service. Repeat step 5 and observe that YARP redirection now works for both API services.

### Exceptions (if any)

_No response_

### .NET Version info

Used Aspire 9.4.0-preview.1.25368.7 (newest today).

### Anything else?

_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.