microsoft / microsoft/aspire

Add IResourceWithServiceDiscovery to ContainerResource

Open
#10,286 6 comments 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.

Intuitively I'd think that this should be possible:

```cs
var test = builder.AddContainer("test", "caddy")
.WithHttpEndpoint(targetPort: 80);
var api = builder.AddProject("apiservice")
.WithReference(test);
```
but it is not. Leaving out the reference compiles, but the ApiService will not be able to call the container like
```cs
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

```cs
public static class AspireBuilderExtensions
{
public static IResourceBuilder 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("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:

```cs
var test = builder.AddContainer("test", "caddy")
.WithHttpEndpoint(targetPort: 80);
var api = builder.AddProject("apiservice")
.WithReference(test);
```

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