Modern applications often use multiple programming languages to handle different business requirements. A development team might use ASP.NET Core for its main API, Go for background processing, Redis for caching, and PostgreSQL for data storage.
Managing these services separately can make local development complicated. Developers may need to open multiple terminal windows, configure ports manually, maintain separate startup scripts, and manage service connections.
.NET Aspire simplifies this process by providing a central application model through the AppHost. It allows developers to manage .NET applications, Go services, databases, containers, and other resources from a single place.
With the official Go hosting integration, developers can add Go applications to an Aspire solution, configure their endpoints, manage service dependencies, and monitor them through the Aspire dashboard.
This article explains how to host Go applications with .NET Aspire AppHost, connect Go services with ASP.NET Core applications, and prepare the solution for container-based deployment.
What Is .NET Aspire AppHost?
.NET Aspire AppHost is the central project used to define and orchestrate the resources that make up a distributed application.
Instead of starting each service manually, developers describe their application resources in the AppHost. Aspire uses this information to start and manage the services during local development.
The AppHost can manage several types of resources, including:
- ASP.NET Core applications and other .NET projects.
- Go applications and external processes.
- Databases such as PostgreSQL and SQL Server.
- Redis and other caching services.
- Containerized applications.
- Message brokers and other infrastructure services.
Aspire also provides a dashboard where developers can inspect resource status, view application logs, and access configured endpoints.
For example, consider an application with the following components:
- An ASP.NET Core API that handles business operations.
- A Go service that processes background workloads.
- PostgreSQL for persistent data storage.
- Redis for caching.
The AppHost can define these resources and manage their local development lifecycle.
Why Host Go Applications With .NET Aspire?
Go is commonly used for network services, background workers, infrastructure tools, and performance-sensitive applications. .NET is widely used for enterprise APIs, business applications, and backend systems.
Combining these technologies allows teams to select the language that best fits each service. However, using multiple languages can introduce additional development and configuration work.
.NET Aspire provides a common orchestration model for managing these services.
Centralized application management
Without an orchestration tool, developers may need separate terminal windows and startup scripts for each service. The AppHost provides a central location to define and start the application resources.
Local development without containers for every service
The Go hosting integration can run Go applications using the local Go toolchain. Developers can work directly with Go source files without building a container image for every development change.
Endpoint and configuration management
Aspire can configure service endpoints and provide connection information to dependent resources. This reduces the need to maintain hardcoded URLs and ports in application code.
Dashboard visibility
Go applications can appear in the Aspire dashboard alongside .NET applications and other resources. Developers can inspect their status, review logs, and access configured endpoints from one interface.
Container publishing support
The Go hosting integration also provides container publishing support. This helps teams move from local development to container-based deployment workflows.
Setting Up the Go Hosting Integration
The official Go hosting integration is provided through the Aspire.Hosting.Go NuGet package.
The official integration is available in Aspire 13.4 and later. Older solutions may use the CommunityToolkit Aspire hosting package for Go, but new applications should use the official integration when their Aspire version supports it.
Before starting, make sure the following tools are installed:
- A compatible .NET SDK.
- Aspire CLI.
- Go toolchain.
- Visual Studio or Visual Studio Code.
- Docker, if you plan to build or run containerized resources.
The Go command must be available through the system PATH so Aspire can execute the application.
Create an Aspire solution
If you already have an Aspire solution, open its AppHost project. Otherwise, create a new Aspire application using the available project templates or Aspire CLI.
A possible solution structure is shown below:
AspireGoDemo/
│
├── AspireGoDemo.AppHost/
│ └── AppHost.cs
│
├── AspireGoDemo.Api/
│ └── Program.cs
│
└── go-api/
├── go.mod
├── main.go
└── handlers.go
The AppHost project manages the application resources. The ASP.NET Core project contains the .NET API, while the go-api directory contains the Go application.
Install the Go hosting integration
From the Aspire solution directory, run:
aspire add go
This command adds the Go hosting integration to the Aspire application.
You can also add the package manually to the AppHost project:
<ItemGroup>
<PackageReference Include="Aspire.Hosting.Go" Version="13.6.0" />
</ItemGroup>
The version shown is an example. Check the current package version and select one that is compatible with your Aspire solution.
Creating a Go HTTP Application
Before registering the Go service with Aspire, create a simple HTTP application.
Open a terminal in the go-api directory and initialize a Go module:
go mod init example.com/go-api
Create a file named main.go:
package main
import (
"encoding/json"
"log"
"net/http"
"os"
)
type HealthResponse struct {
Status string `json:"status"`
Service string `json:"service"`
}
func healthHandler(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json")
response := HealthResponse{
Status: "Healthy",
Service: "Go API",
}
json.NewEncoder(w).Encode(response)
}
func main() {
port := os.Getenv("PORT")
if port == "" {
port = "8080"
}
http.HandleFunc("/health", healthHandler)
log.Printf("Go API listening on port %s", port)
log.Fatal(http.ListenAndServe(":"+port, nil))
}
This application creates a simple HTTP endpoint that returns the health status of the Go service.
The application reads its listening port from the PORT environment variable. If the variable is not configured, it uses port 8080.
Using environment variables makes the application easier to configure across local development, testing, and production environments.
You can test the Go application independently:
cd go-api
go run .
Open the following address in a browser or API client:
http://localhost:8080/health
The application should return a response similar to this:
{
"status": "Healthy",
"service": "Go API"
}
Adding the Go Application to Aspire AppHost
The official Go hosting integration provides the AddGoApp method for registering a Go application as an Aspire resource.
Open the AppHost.cs file and add the following configuration:
var builder = DistributedApplication.CreateBuilder(args);
var goApi = builder.AddGoApp("go-api", "../go-api")
.WithHttpEndpoint(
port: 8080,
env: "PORT")
.WithExternalHttpEndpoints();
builder.Build().Run();
The AddGoApp method registers the Go application with Aspire. The first argument defines its logical resource name, while the second identifies the application directory.
The WithHttpEndpoint method configures the HTTP endpoint and provides the PORT environment variable to the Go process. This allows the Go application to use the port configured by Aspire.
The WithExternalHttpEndpoints method marks the endpoint for external access. This is useful when you want to access the Go service from outside the local application network.
The directory path in this example is relative to the AppHost project. Adjust it according to your solution structure.
When you run the AppHost, Aspire starts the Go application using the local Go toolchain. The service also appears in the Aspire dashboard, where you can inspect its status and logs.
Hosting Go and ASP.NET Core Applications Together
One of the main advantages of Aspire is that it can manage applications written in different programming languages through the same AppHost.
For example, you can register an ASP.NET Core API alongside the Go service:
var builder = DistributedApplication.CreateBuilder(args);
var goApi = builder.AddGoApp("go-api", "../go-api")
.WithHttpEndpoint(port: 8080, env: "PORT")
.WithExternalHttpEndpoints();
var dotnetApi = builder.AddProject<Projects.AspireGoDemo_Api>(
"dotnet-api")
.WithReference(goApi);
builder.Build().Run();
The AddProject method registers the ASP.NET Core application, while WithReference establishes a resource relationship between the .NET API and the Go service.
Aspire makes the referenced service's endpoint information available to the dependent application through its configuration.
This allows the .NET API to discover the Go service without depending on a hardcoded localhost address.
The two applications still run as separate processes. Aspire manages their orchestration and configuration, but each application remains responsible for its own runtime and business logic.
Calling a Go Service From ASP.NET Core
When an ASP.NET Core application references a Go service, it can use Aspire service discovery to locate the configured endpoint.
First, make sure the ASP.NET Core project has the required Aspire service defaults and service discovery configuration.
The following example shows how to register an HTTP client and call the Go health endpoint:
var builder = WebApplication.CreateBuilder(args);
builder.AddServiceDefaults();
builder.Services.AddHttpClient("GoApi", client =>
{
client.BaseAddress = new Uri("http://go-api");
});
var app = builder.Build();
app.MapGet("/go-health", async (
IHttpClientFactory httpClientFactory) =>
{
var client = httpClientFactory.CreateClient("GoApi");
var response = await client.GetStringAsync("/health");
return Results.Content(
response,
"application/json");
});
app.Run();
The address uses the logical resource name declared in the AppHost. Aspire service discovery resolves the configured endpoint at runtime.
The AddServiceDefaults method is commonly included in Aspire service projects to configure shared capabilities such as service discovery, telemetry, and health checks, depending on the project template.
For production applications, add suitable HTTP timeouts, error handling, response validation, and retry policies where appropriate. Service discovery helps locate a service, but it does not guarantee that the service is healthy or that every request will succeed.
Configuring Dependencies and Environment Variables
Go applications often depend on external resources such as databases, caches, and message brokers.
Aspire allows developers to define these resources in the AppHost and establish relationships between them.
For example, the following configuration adds Redis to the application:
var builder = DistributedApplication.CreateBuilder(args);
var redis = builder.AddRedis("cache");
var goApi = builder.AddGoApp("go-api", "../go-api")
.WithHttpEndpoint(port: 8080, env: "PORT")
.WithReference(redis);
builder.Build().Run();
The WithReference method makes the Redis connection information available to the Go resource through its configured environment.
However, Aspire does not automatically add a Redis client library to the Go project or implement Redis operations. The Go application must read the supplied configuration and use a compatible Go Redis client.
For example, the Go service can read the connection information from an environment variable and use it to initialize its Redis connection.
This separation keeps the responsibilities clear. Aspire manages application orchestration and resource configuration, while the Go application manages its own business logic and integrations.
Preparing Go Applications for Container Deployment
Running a Go application through the local Go toolchain is convenient during development. Production environments often use containers to package and deploy applications consistently.
The official Go hosting integration supports container publishing workflows. Depending on the integration configuration and deployment target, it can use an existing Dockerfile or generate a multi-stage Dockerfile for the Go application.
A typical Dockerfile for a Go HTTP service might look like this:
FROM golang:1.25-alpine AS build
WORKDIR /src
COPY go.mod go.sum ./
RUN go mod download
COPY . .
RUN CGO_ENABLED=0 GOOS=linux \
go build -o /app/go-api .
FROM alpine:3.22
WORKDIR /app
RUN adduser -D appuser
COPY --from=build /app/go-api ./go-api
USER appuser
EXPOSE 8080
ENTRYPOINT ["./go-api"]
The first stage compiles the Go application. The second stage contains the compiled executable and a smaller runtime environment.
The Go version and base image should match your project requirements. Applications that use CGO or native libraries may require additional build dependencies and runtime packages.
If you use the Go hosting integration's container publishing support, review its generated Dockerfile and publishing configuration before maintaining a separate custom Dockerfile.
AddGoApp vs AddExecutable vs AddDockerfile
Aspire provides different ways to include non-.NET applications in an AppHost. The appropriate method depends on the application type and how you want to run it.
| Approach | Best use case | Main benefit |
|---|
| AddGoApp | Go applications | Go-specific configuration and publishing support |
| AddExecutable | General host processes | Runs an external executable or command |
| AddDockerfile | Containerized applications | Builds a resource using an existing Dockerfile |
AddGoApp is the recommended approach for Go applications when using the official Go hosting integration. It provides Go-specific behavior, including local Go toolchain execution and container publishing support.
AddExecutable is useful when you need to run an external command or application that does not have a dedicated Aspire integration. It provides a general process-hosting model.
AddDockerfile is appropriate when you already have a containerized application and want Aspire to build and manage it as a container resource.
Best Practices for Hosting Go Applications With Aspire
Use the official Go integration
For new applications, use the Aspire.Hosting.Go package and AddGoApp method with a compatible Aspire version. This provides Go-specific integration without requiring a general-purpose process configuration.
Keep service configuration environment-based
Avoid hardcoding ports, database addresses, and other environment-specific values in Go source files. Read configuration from environment variables and provide suitable defaults for local development.
Use logical service names
Use Aspire service discovery when application resources need to communicate with each other. This reduces dependencies on fixed localhost addresses and makes configuration easier to maintain.
Keep application responsibilities separate
Aspire manages resource orchestration and configuration. Go applications should remain responsible for their own HTTP handlers, database operations, logging, error handling, and business logic.
Prepare production deployment separately
Local AppHost orchestration does not automatically provide a production hosting environment. Review container images, secrets, network policies, health checks, resource limits, and deployment configuration before releasing the application.
Keep toolchain versions compatible
Use compatible versions of the .NET SDK, Aspire packages, and Go toolchain. Test both local execution and container publishing in your CI pipeline to identify environment-related problems early.
Common Mistakes and Troubleshooting
| Problem | Possible cause | What to check |
|---|
| Go application does not start | Go is missing from PATH | Run go version in the environment used to start Aspire |
| Endpoint is unavailable | Go service listens on a different port | Check the PORT environment variable and endpoint configuration |
| Service discovery fails | Missing resource reference or configuration | Verify WithReference and service discovery setup |
| Container build fails | Incorrect module or Dockerfile configuration | Check go.mod, dependencies, and build settings |
| Application works locally but fails in a container | Operating system, architecture, or native dependency differences | Review build target, CGO settings, and runtime libraries |
| Redis connection fails | Go application does not consume the supplied configuration | Check the injected environment variables and Redis client setup |
When troubleshooting, start with the Aspire dashboard and the Go application's logs. Confirm that the resource starts successfully before investigating service discovery or external dependencies.
Advantages and Disadvantages
Advantages
- Manages Go and .NET services through one AppHost.
- Supports local execution using the Go toolchain.
- Provides endpoint and environment configuration.
- Displays Go resources in the Aspire dashboard.
- Supports service discovery and resource relationships.
- Provides a path toward container-based deployment.
Disadvantages
- Developers still need to install and maintain the Go toolchain.
- Aspire does not automatically implement Go business logic or third-party client integrations.
- Production deployment requires additional infrastructure and security configuration.
- Different languages may require separate debugging, testing, and build workflows.
- Integration capabilities depend on the Aspire version and deployment target.
Conclusion
.NET Aspire makes it easier to manage Go applications alongside ASP.NET Core services and other infrastructure resources. With the official Go hosting integration, developers can register Go services through AddGoApp, configure HTTP endpoints, establish resource relationships, and monitor applications through the Aspire dashboard.
The main benefit is having a consistent application model for distributed systems that use multiple programming languages. Teams can continue using Go where it fits best and .NET for enterprise application requirements without maintaining completely separate local orchestration workflows.
For teams building applications with both Go and .NET, Aspire provides a practical foundation for local development and a path toward container-based deployment.
Join the conversation! Your thoughts help the community grow.