aspire icon indicating copy to clipboard operation
aspire copied to clipboard

Cannot configure a predictable exposed port for Vite, which is required for authentication

Open hansmbakker opened this issue 2 months ago • 5 comments

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.

This issue is related to https://github.com/CommunityToolkit/Aspire/discussions/269 This is moved from https://github.com/CommunityToolkit/Aspire/issues/962 since AddViteApp is now part of vanilla Aspire.

The desired Http port cannot be configured like other resources can.

The consequences of this is that Aspire assigns a random exposed port every time the AppHost is started.

Having a port that changes on every run breaks the authentication setup because in Entra ID, app registrations are required to have a predictable redirect URL.

Describe the solution you'd like

Either one of these options:

  • being able to call WithHttpEndpoint (currently it throws an exception since the Aspire code does this internally already)
  • being able to specify it using an optional parameter in AddViteApp

Additional context

No response

hansmbakker avatar Nov 12 '25 22:11 hansmbakker

The workaround is:

builder.AddViteApp(
    name: "projectname",
    appDirectory: "../projectfolder"
    )
    .WithEndpoint("http", (endpointAnnotation) =>
                {
                    endpointAnnotation.Port = 5173;
                })
                .WithEnvironment("PORT", "5173")
    .WithReference(apiService)
    .WaitFor(apiService)
    .WithExternalHttpEndpoints();

This somehow does not trigger the duplicate http endpoint Exception.

Thanks to @nik-yo for the idea at https://www.linkedin.com/pulse/override-default-endpoint-net-aspire-nikki-yodo-z8icc/

hansmbakker avatar Nov 12 '25 23:11 hansmbakker

builder.AddViteApp(
    name: "projectname",
    appDirectory: "../projectfolder"
    )
    .WithEndpoint("http", (endpointAnnotation) =>
                {
                    endpointAnnotation.Port = 5173;
                })
    .WithReference(apiService)
    .WaitFor(apiService)
    .WithExternalHttpEndpoints();

This should be enough. There's a proxy in front of the vite port with the address 5173 (vite will listen on a random port but it should not matter). If it does matter, then you can set IsProxied to false.

davidfowl avatar Nov 13 '25 07:11 davidfowl

@davidfowl thank you for the response!

In my opinion this is still a workaround because the more intuitive WithHttpEndpoint throws an exception.

It is possible that my understanding of the difference in meaning between WithHttpEndpoint(5173) and

.WithEndpoint("http", (endpointAnnotation) =>
                {
                    endpointAnnotation.Port = 5173;
                })

is incomplete or wrong, but I think a lot of users will first try the WithHttpEndpoint method first. I was only lucky to find @nik-yo's post.

hansmbakker avatar Nov 13 '25 12:11 hansmbakker

That's correct, WithEndpoint adds an endpoint. That's why it complained about duplicate endpoints. AddViteApp already adds the endpoint.

davidfowl avatar Nov 14 '25 15:11 davidfowl

WithEndpoint adds an endpoint. That's why it complained about duplicate endpoints

@davidfowl I'm not sure we're talking about the same.

  • WithHttpEndpoint(...) throws an exception because of duplicate endpoints
  • WithEndpoint("http", ...) does not throw an exception even though it looks like WithHttpEndpoint is just a shorthand for WithEndpoint("http"

This is super counter-intuitive and using .WithEndpoint("http" instead of WithHttpEndpoint feels like a workaround.

AddViteApp already adds the endpoint.

Yes, and this GitHub issue is an ask to either make that endpoint configurable or to be able to setup an endpoint ourselves.

hansmbakker avatar Nov 14 '25 17:11 hansmbakker