Skip to content

Add IResourceWithServiceDiscovery to ContainerResource #10286

@msexxeta

Description

@msexxeta

Is there an existing issue for this?

  • I have searched the existing issues

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

Intuitively I'd think that this should be possible:

var test = builder.AddContainer("test", "caddy")
    .WithHttpEndpoint(targetPort: 80);
var api = builder.AddProject<Projects.ApiService>("apiservice")
    .WithReference(test);

but it is not. Leaving out the reference compiles, but the ApiService will not be able to call the container like

builder.Services.AddHttpClient("test", client =>
    {
        client.BaseAddress = new("http://test");
    });
//...
await client.GetStringAsync("/"); //cannot connect to host "test"

I need to do a workaround like

public static class AspireBuilderExtensions
{
    public static IResourceBuilder<ContainerServiceResource> AddContainerService(this IDistributedApplicationBuilder builder, [ResourceName] string name, string? image = null, string? tag = null)
    {
        var res = new ContainerServiceResource(name);
        var service = builder.AddResource(res);
        if (image is not null)
            service.WithImage(image, tag);
        return service;
    }
}

public class ContainerServiceResource : ContainerResource, IResourceWithServiceDiscovery
{
    public ContainerServiceResource(string name, string? entrypoint = null) : base(name, entrypoint)
    {
    }
}

// ...

var test = builder.AddContainerService("test", "caddy")
    .WithHttpEndpoint(targetPort: 80);
var api = builder.AddProject<Projects.ApiService>("apiservice")
    .WithReference(test);

and then I can successfully call the container.

Describe the solution you'd like

ContainerResource should inherit IResourceWithServiceDiscovery by default, or make it so after adding an endpoint this works:

var test = builder.AddContainer("test", "caddy")
    .WithHttpEndpoint(targetPort: 80);
var api = builder.AddProject<Projects.ApiService>("apiservice")
    .WithReference(test);

Additional context

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    area-app-modelIssues pertaining to the APIs in Aspire.Hosting, e.g. DistributedApplication

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions