Introduction
Modern .NET applications often contain multiple services that communicate with each other to complete business operations. For example, an e-commerce application might have an Order API, a Payment API, a Notification Service, and a database. When a customer places an order, these components work together to process the request.
Although this architecture provides flexibility, debugging communication problems between services can become challenging. A request may reach one service successfully but fail when another service is unavailable, responds slowly, or returns an unexpected error.
Developers often need to check application logs, inspect HTTP requests, identify connection problems, and understand where a request failed.
.NET Aspire simplifies this process through its dashboard and built-in observability capabilities. Developers can inspect application resources, view structured logs, examine distributed traces, and identify problems across connected services.
In this article, we will build a small distributed application using .NET Aspire and explore how to debug service-to-service communication problems through the Aspire dashboard.
Understanding Service-to-Service Communication
Service-to-service communication happens when one application component sends a request to another component.
For example, consider an application with two ASP.NET Core services:
- Order API: Receives customer orders.
- Inventory API: Checks whether products are available.
When a customer submits an order, the Order API sends an HTTP request to the Inventory API to verify product availability.
The communication flow looks like this:
Client Application
|
|
Order API
|
| HTTP Request
|
Inventory API
|
|
API Response
|
|
Order API
|
|
Client Response
If the Inventory API becomes unavailable, the Order API may fail to complete the operation.
Without centralized observability, developers might need to inspect multiple terminal windows to understand what happened.
.NET Aspire helps bring this information together.
What Is the .NET Aspire Dashboard?
The .NET Aspire dashboard is a development tool that provides a centralized view of distributed application resources and their operational information.
It helps developers understand what is happening across an application without switching between multiple tools.
The dashboard provides several useful capabilities:
| Feature | Purpose |
|---|
| Resources | Displays application services and infrastructure resources |
| Console Logs | Shows application output and diagnostic messages |
| Structured Logs | Displays application log events with searchable properties |
| Traces | Shows how requests move across services |
| Metrics | Displays collected application and runtime measurements |
| Resource Endpoints | Helps access application endpoints |
The dashboard is particularly useful when multiple services communicate with each other because it helps developers connect individual operations to the complete request flow.
Step 1. Create a .NET Aspire Application
First, make sure the required .NET SDK and Aspire templates are installed.
Check the available SDK versions:
dotnet --list-sdks
Install the Aspire project templates if required:
dotnet new install Aspire.ProjectTemplates
Create a new Aspire application:
dotnet new aspire-starter -n AspireDebugging
Navigate to the solution directory:
cd AspireDebugging
We will use two ASP.NET Core projects:
- AspireDebugging.OrderApi
- AspireDebugging.InventoryApi
The AppHost project will manage both services.
The generated project structure may vary depending on the installed Aspire template version.
Step 2. Configure Both Services in AppHost
Open the Program.cs file inside the AppHost project.
Add the following configuration:
var builder = DistributedApplication.CreateBuilder(args);
var inventoryApi = builder.AddProject<
Projects.AspireDebugging_InventoryApi>("inventory-api");
builder.AddProject<
Projects.AspireDebugging_OrderApi>("order-api")
.WithReference(inventoryApi);
builder.Build().Run();
The AddProject method registers each application as a resource.
The WithReference method establishes a reference from the Order API to the Inventory API.
This also enables service discovery configuration for the referenced service.
Aspire provides the necessary endpoint information to the consuming application, reducing the need to maintain hardcoded service URLs.
Step 3. Create the Inventory API
The Inventory API will check whether a product is available.
Open the Program.cs file inside the Inventory API project.
Add the following code:
var builder = WebApplication.CreateBuilder(args);
builder.AddServiceDefaults();
var app = builder.Build();
app.MapDefaultEndpoints();
app.MapGet("/api/inventory/{productId:int}",
(int productId) =>
{
if (productId <= 0)
{
return Results.BadRequest(
new { Message = "Invalid product ID." });
}
return Results.Ok(new
{
ProductId = productId,
Available = true,
Quantity = 25
});
});
app.Run();
The endpoint accepts a product ID and returns its availability information.
For this example, the inventory data is static. A production application would normally retrieve inventory information from a database or another data source.
The AddServiceDefaults method configures common Aspire application features, including health checks and telemetry.
Step 4. Configure HTTP Communication in the Order API
Now we need to configure the Order API to communicate with the Inventory API.
First, register an HTTP client in the Order API.
Update its Program.cs file:
var builder = WebApplication.CreateBuilder(args);
builder.AddServiceDefaults();
builder.Services.AddHttpClient("InventoryApi", client =>
{
client.BaseAddress = new Uri(
"http://inventory-api");
});
var app = builder.Build();
app.MapDefaultEndpoints();
app.MapPost("/api/orders/{productId:int}",
async (
int productId,
IHttpClientFactory httpClientFactory) =>
{
var client = httpClientFactory
.CreateClient("InventoryApi");
var response = await client.GetAsync(
$"/api/inventory/{productId}");
if (!response.IsSuccessStatusCode)
{
return Results.Problem(
"Unable to verify product availability.",
statusCode: StatusCodes.Status502BadGateway);
}
var inventory = await response.Content
.ReadFromJsonAsync<InventoryResponse>();
if (inventory is null || !inventory.Available)
{
return Results.BadRequest(
"Product is not available.");
}
return Results.Ok(new
{
Message = "Order processed successfully.",
ProductId = productId
});
});
app.Run();
public record InventoryResponse(
int ProductId,
bool Available,
int Quantity);
Here, the Order API uses IHttpClientFactory to create an HTTP client.
The client sends a request to the Inventory API using its Aspire service name.
The service discovery integration resolves the service endpoint instead of requiring developers to maintain a fixed localhost URL and port.
For a production application, configure appropriate HTTP timeouts, cancellation tokens, and resilience policies.
Step 5. Run the Application
Now run the AppHost project:
dotnet run --project AspireDebugging.AppHost
Aspire starts both services and displays the dashboard address in the terminal.
Open the dashboard in your browser.
You should see the following resources:
Check that both resources have started successfully.
If a resource fails to start, select it and inspect the available diagnostic information.
Now send a POST request to the Order API:
POST /api/orders/101
The Order API sends a request to the Inventory API and receives the product availability information.
If everything works correctly, the API returns:
{
"message": "Order processed successfully.",
"productId": 101
}
This is a simple example of communication between two independent ASP.NET Core services.
Step 6. Debug Service Calls Using Distributed Tracing
Distributed tracing is one of the most useful capabilities when debugging service-to-service communication.
A distributed trace represents the complete journey of an operation across different application components.
For example, when a customer places an order, the trace may contain the following operations:
- The Order API receives an HTTP request.
- The Order API sends an HTTP request to the Inventory API.
- The Inventory API processes the request.
- The Inventory API returns a response.
- The Order API completes the original request.
Each operation is represented by a span.
A trace contains one or more related spans connected through trace and parent identifiers.
When OpenTelemetry instrumentation is configured correctly, Aspire can collect this information and display it in the dashboard.
To inspect a request:
- Open the Aspire dashboard.
- Navigate to the Traces section.
- Find the trace associated with the Order API request.
- Open the trace details.
- Examine the spans generated by the Order API and Inventory API.
- Check the duration and status of each operation.
If the Inventory API takes too long to respond, its span can help identify where the delay occurred.
The trace also helps distinguish between problems in the calling service and problems in the downstream service.
Step 7. Debug Failed Service Calls
Let's introduce a failure to understand how the dashboard helps with troubleshooting.
Update the Inventory API endpoint:
app.MapGet("/api/inventory/{productId:int}",
(int productId) =>
{
if (productId == 500)
{
return Results.StatusCode(
StatusCodes.Status500InternalServerError);
}
return Results.Ok(new
{
ProductId = productId,
Available = true,
Quantity = 25
});
});
Now send a request using product ID 500:
POST /api/orders/500
The Inventory API returns an HTTP 500 response.
The Order API detects the unsuccessful response and returns a gateway error.
Open the Aspire dashboard and inspect the corresponding trace and logs.
Look for:
- The incoming request to the Order API.
- The outgoing HTTP request to the Inventory API.
- The HTTP response status.
- The failed operation.
- The duration of the downstream request.
This helps identify whether the original request failed inside the Order API or because of a downstream service response.
One important point is that an HTTP 500 response does not automatically mean a complete exception stack trace will appear in the dashboard. The application must log relevant exception details, and the required telemetry instrumentation must be configured.
Step 8. Use Structured Logging for Better Debugging
Logs provide additional information that distributed traces may not contain.
For example, we can add structured logging to the Order API:
app.MapPost("/api/orders/{productId:int}",
async (
int productId,
IHttpClientFactory httpClientFactory,
ILogger<Program> logger) =>
{
logger.LogInformation(
"Processing order for product {ProductId}",
productId);
var client = httpClientFactory
.CreateClient("InventoryApi");
try
{
var response = await client.GetAsync(
$"/api/inventory/{productId}");
logger.LogInformation(
"Inventory API returned status code {StatusCode}",
response.StatusCode);
response.EnsureSuccessStatusCode();
return Results.Ok(new
{
Message = "Order processed successfully.",
ProductId = productId
});
}
catch (HttpRequestException ex)
{
logger.LogError(
ex,
"Inventory API request failed for product {ProductId}",
productId);
return Results.Problem(
"Inventory service is currently unavailable.",
statusCode: StatusCodes.Status502BadGateway);
}
});
Structured logging stores values as separate properties rather than combining everything into a single text message.
For example, ProductId and StatusCode become searchable properties in supported log viewers.
This makes it easier to identify failures associated with specific requests or application resources.
Step 9. Troubleshooting Common Service-to-Service Problems
The Aspire dashboard can help investigate several common communication problems.
| Problem | What to Check | Possible Solution |
|---|
| Service cannot be reached | Resource status and HTTP request logs | Verify service discovery configuration |
| HTTP 500 response | Downstream service logs and trace | Investigate the failing endpoint |
| Slow API response | Trace span duration | Check database calls and external dependencies |
| Missing trace information | Instrumentation configuration | Verify OpenTelemetry setup |
| Connection refused | Service availability and endpoint | Check resource startup and readiness |
| Unexpected HTTP status | Request and response details | Review API validation and error handling |
A common mistake is assuming that a service is unavailable simply because its resource appears in the dashboard.
A running resource does not always mean that every application endpoint is healthy or that all dependencies are available.
Use health checks and actual request traces to investigate the problem.
Best Practices for Debugging Distributed Applications
Use Distributed Tracing
Enable HTTP client and ASP.NET Core instrumentation so requests can be followed across service boundaries.
Avoid relying only on individual application logs when investigating distributed failures.
Use Structured Logs
Include meaningful properties such as order ID, product ID, operation name, and status code.
Avoid logging passwords, access tokens, or sensitive customer information.
Configure Health Checks
Use readiness checks to verify whether a service is prepared to handle requests.
Also monitor important dependencies such as databases and external services.
Set Appropriate HTTP Timeouts
A downstream service should not be allowed to block an incoming request indefinitely.
Configure timeouts according to the operation's expected behavior.
Propagate Trace Context
Use standard OpenTelemetry and W3C Trace Context support to maintain trace relationships across HTTP calls.
Avoid Exposing Internal Errors
Return meaningful API error responses without exposing internal stack traces or infrastructure details to clients.
Keep detailed diagnostic information in protected application logs.
Advantages and Disadvantages of the Aspire Dashboard
Advantages
- Provides a centralized view of distributed application resources.
- Helps developers inspect service-to-service communication.
- Makes logs and distributed traces easier to investigate.
- Helps identify slow downstream requests.
- Reduces the need to switch between multiple development tools.
- Supports faster troubleshooting during local development.
Disadvantages
- Requires proper telemetry instrumentation to provide useful traces.
- Does not automatically identify the root cause of every failure.
- Trace collection can introduce additional resource overhead.
- Production observability still requires appropriate deployment and monitoring infrastructure.
- Large distributed systems may require additional observability tools and trace storage solutions.
Summary
Debugging service-to-service communication becomes more complicated as an application grows and multiple services depend on each other.
.NET Aspire simplifies local development observability through its dashboard, which provides access to resource information, structured logs, distributed traces, and metrics.
In this article, we created two ASP.NET Core services and configured them to communicate through service discovery. We also introduced a downstream failure and explored how distributed tracing and structured logging help identify communication problems.
The most important lesson is that successful debugging requires more than checking whether services are running. Developers should understand the complete request flow, inspect individual operations, and correlate logs with distributed traces.
By using the .NET Aspire dashboard effectively, developers can investigate service communication problems more easily and build more reliable distributed applications.
Join the conversation! Your thoughts help the community grow.