microsoft / microsoft/aspire

AddUvcornApp with HTTP endpoint does not take static port

Open
#14,408 6 comments 0 reactions 0 assignees View on GitHub
needs-area-label python
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

I have a feeling I'm doing something very simple and the answer should be quite obvious, but I can't seem to figure it out.

One of the services I'm hosting locally, via Aspire, is a Python Uvicorn app exposing FastAPI endpoints.
The relevant piece of code in the AppHost looks like this.

```csharp
var pythonDir = Path.GetFullPath("../../python");
var pythonBackend = builder.AddUvicornApp("backend", pythonDir + "/src", "aspire_backend_service.main:app")
.WithExternalHttpEndpoints() // <-- This one makes sure I've got a random port and exposed via HTTPS(!?)
.WithUv()
.WithVirtualEnvironment(pythonDir + "/.venv")
.WithEnvironment("DEBUG", "true")
.WithEnvironment("LOG_LEVEL", "info")
.WithEnvironment(context =>
{
// Dynamic environment variables
context.EnvironmentVariables["START_TIME"] = DateTimeOffset.UtcNow.ToString();
})
// Should be the format of `https://.services.ai.azure.com/api/projects/`
.WithEnvironment("AZURE_AI_PROJECT_ENDPOINT", aiFoundryProjectEndpoint)
.WithEnvironment("AZURE_AI_MODEL_DEPLOYMENT_NAME", "gpt-4o-mini")
.WithHttpEndpoint(port: 9000, env: "UVICORN_PORT", name: "uvicorn-http")
// .WithHttpsEndpoint(port: 9443, env: "UVICORN_PORT", name: "uvicorn-https")
// .WithHttpEndpoint(port: 9000, env: "PORT", name: "uvicorn-http")
// .WithHttpsEndpoint(port: 9443, env: "PORT", name: "uvicorn-https")
;
```

As you can see, I've been trying multiple approaches. I've also tried with the `IsProxied`-parameter with no avail.
This yields in a working application with 2 ports. One randomly assigned and one with `9443`.

Aspire dashboard showing the Python Uvicorn app with 2 ports

When invoking the endpoints via the randomly assigned port, my endpoints work.
When invoking the endpoints via port `9443` I get a timeout (connection reset). I'm thinking the application isn't really listening on this 9443 port and it's only exposed but not used.

The docs are still referring to the Community Toolkit package, which is deprecated as of Aspire 13.0 (https://aspire.dev/integrations/frameworks/python/). The suggestions there I think I've all tried. Thoughts?

I've also setup a test project, which does some more stuff (https://github.com/Jandev/trial-and-error/). The bare minimum would be to have an AppHost project and a Python project using Uvicorn and a FastAPI implementation to test.

### Expected Behavior

Able to work with a static port on the Python application, like `9443` and not rely on the randomly assigned port.

### Steps To Reproduce

_No response_

### Exceptions (if any)

_No response_

### .NET Version info

_No response_

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