Method | Resource type | Description |
---|---|---|
AddProject | ProjectResource | A .NET project, for example ASP.NET Core web apps. |
AddContainer | ContainerResource | A container image, such as a Docker image. |
AddExecutable | ExecutableResource | An executable file. |
Aspire provides three resource types: Project, Container, and Executable. However, service discovery is only enabled by default for Project resources.
To build web API microservices in languages other than .NET, we can utilize the IResourceWithServiceDiscovery
interface.
Here, we demonstrate Aspire's non-.NET Web API service discovery approach using a Bun web API (similar to Node.js). Three code examples illustrate different methods.
Project File Location: .. /runner
1. Container Resource with Service Discovery
We'll inherit from both the container resource and the service discovery interface:
public class BunContainerResource(string name) : ContainerResource(name), IResourceWithServiceDiscovery
{
}
In AppHost
, we add the container resource with details:
var bun = builder.AddResource(new BunContainerResource("runner"))
.WithImage("oven/bun", "distroless")
.WithBindMount(".. /runner", "/home/bun/app/")
.WithEndpoint(targetPort: 3000, scheme: "http", env: "PORT")
.WithArgs("--hot", "/home/bun/app/src/index.ts");
// Also, Replace WithImage to WithDockerfile
.WithDockerfile("../runner")
To interact with the Bun service, register RunnerApiClient
with a base URL dynamically constructed using service discovery:
builder.Services.AddHttpClient<RunnerApiClient>(client => client.BaseAddress = new Uri($"http://runner"));
2. Executable Resource with Service Discovery
Define a class for discoverable executable resources:
public class BunExecuteableResource(string name, string executablePath, string projectDirectory) : ExecutableResource(name, executablePath, projectDirectory), IResourceWithServiceDiscovery
{
}
In AppHost
, register the executable resource:
var bunProject = new BunExecuteableResource("runner", "bun", ".. /runner");
var bun = builder.AddResource(bunProject)
.WithArgs("run", "dev")
.WithEndpoint(targetPort: 3000, scheme: "http", env: "PORT");
3. Yarp Proxy Integration with .NET Web API
If you have an existing .NET Web API project, it might already have a built-in Yarp proxy for service exposure. Here's how to configure Yarp:
Yarp Configuration (JSON):
"destination1": {
"Address": "http://localhost:3000/"
}
Alternatively, you can start the Bun service directly within AppHost
:
var bun = builder.AddExecutable("runner", "bun", ".. /runner");
Then, reference the service's endpoint:
.WithReference(bun.GetEndpoint("runner"))
Top comments (0)