From 4c901e2f11e46ec6f6d02f998e3c842b83198130 Mon Sep 17 00:00:00 2001
From: Tom Longhurst <30480171+thomhurst@users.noreply.github.com>
Date: Wed, 18 Nov 2026 12:25:00 +0000
Subject: [PATCH] fix(aspire): wire OTLP for AddDotnetProject resources
AddDotnetProject creates a DotnetProjectResource (an executable resource), not a
ProjectResource, so AspireFixture skipped it: no OTLP env vars and no missing-telemetry
hint. Match on IDotnetProgramResource, which both share and which exists in Aspire 13.x
(experimental, ASPIREPROJECTS001) and is stable in Aspire 17. No new package dependency,
so net8.0 support stays.
---
Directory.Packages.props | 1 +
src/TUnit.Aspire.Core/AspireFixture.cs | 21 ++++--
.../TUnit.Aspire.Tests.ApiService.csproj | 5 +-
.../DotnetProjectResourceTests.cs | 74 +++++++++++++++++++
.../TUnit.Aspire.Tests.csproj | 2 +
5 files changed, 97 insertions(+), 6 deletions(-)
create mode 100644 tests/TUnit.Aspire.Tests/DotnetProjectResourceTests.cs
diff --git a/Directory.Packages.props b/Directory.Packages.props
index 3995bdae72d..808f1f49064 100644
--- a/Directory.Packages.props
+++ b/Directory.Packages.props
@@ -71,6 +71,7 @@
+
diff --git a/src/TUnit.Aspire.Core/AspireFixture.cs b/src/TUnit.Aspire.Core/AspireFixture.cs
index 0e857be86e6..9716e744a20 100644
--- a/src/TUnit.Aspire.Core/AspireFixture.cs
+++ b/src/TUnit.Aspire.Core/AspireFixture.cs
@@ -1037,11 +1037,11 @@ private void ConfigureOtlpEndpoints(IDistributedApplicationTestingBuilder builde
foreach (var resource in builder.Resources)
{
- if (resource is ProjectResource projectResource)
+ if (IsDotnetProgramResource(resource))
{
- var resourceName = projectResource.Name;
+ var resourceName = resource.Name;
- projectResource.Annotations.Add(
+ resource.Annotations.Add(
new EnvironmentCallbackAnnotation(context =>
{
// Forward to user's dashboard if they set OTEL_EXPORTER_OTLP_ENDPOINT
@@ -1070,6 +1070,17 @@ private void ConfigureOtlpEndpoints(IDistributedApplicationTestingBuilder builde
}
}
+ // IDotnetProgramResource is experimental in Aspire 13.x and stable from Aspire 17.
+#pragma warning disable ASPIREPROJECTS001
+ ///
+ /// Matches .NET project resources from both AddProject () and
+ /// AddDotnetProject (DotnetProjectResource, an executable resource), which share
+ /// .
+ ///
+ private static bool IsDotnetProgramResource(IResource resource)
+ => resource is IDotnetProgramResource and IResourceWithEnvironment;
+#pragma warning restore ASPIREPROJECTS001
+
///
/// Emits a one-time hint for any project resource that produced console output but never sent
/// correlated OpenTelemetry logs to TUnit — the typical symptom of an SUT missing OTLP log
@@ -1087,8 +1098,8 @@ private async Task EmitTelemetryWiringHintsAsync(DistributedApplication app, Otl
// The fixture injects OTEL_SERVICE_NAME = resource name (see ConfigureOtlpEndpoints), so
// an SUT using the default service name matches by resource name here; one that overrides
// its service name may produce a false hint — hence the soft wording below.
- var candidates = model.Resources.OfType()
- .Where(p => ShouldWaitForResource(p) && !receiver.HasSeenLogsFrom(p.Name))
+ var candidates = model.Resources
+ .Where(p => IsDotnetProgramResource(p) && ShouldWaitForResource(p) && !receiver.HasSeenLogsFrom(p.Name))
.ToList();
if (candidates.Count == 0)
{
diff --git a/tests/TUnit.Aspire.Tests.ApiService/TUnit.Aspire.Tests.ApiService.csproj b/tests/TUnit.Aspire.Tests.ApiService/TUnit.Aspire.Tests.ApiService.csproj
index d548c9f61c1..1736528159b 100644
--- a/tests/TUnit.Aspire.Tests.ApiService/TUnit.Aspire.Tests.ApiService.csproj
+++ b/tests/TUnit.Aspire.Tests.ApiService/TUnit.Aspire.Tests.ApiService.csproj
@@ -1,7 +1,10 @@
- net10.0
+
+ net10.0
+
false
diff --git a/tests/TUnit.Aspire.Tests/DotnetProjectResourceTests.cs b/tests/TUnit.Aspire.Tests/DotnetProjectResourceTests.cs
new file mode 100644
index 00000000000..375d9faa3be
--- /dev/null
+++ b/tests/TUnit.Aspire.Tests/DotnetProjectResourceTests.cs
@@ -0,0 +1,74 @@
+using System.Diagnostics;
+using Aspire.Hosting;
+using Aspire.Hosting.ApplicationModel;
+using Aspire.Hosting.Testing;
+using TUnit.Assertions;
+using TUnit.Assertions.Extensions;
+using TUnit.Core;
+
+namespace TUnit.Aspire.Tests;
+
+///
+/// Runs the API service through AddDotnetProject, which creates a DotnetProjectResource
+/// (an executable resource) rather than a .
+///
+public class DotnetProjectResourceFixture : AspireFixture
+{
+ public const string ServiceName = "dotnet-api-service";
+
+ protected override TimeSpan ResourceTimeout => TimeSpan.FromSeconds(120);
+
+ protected override ResourceWaitBehavior WaitBehavior => ResourceWaitBehavior.Named;
+
+ protected override IEnumerable ResourcesToWaitFor() => [ServiceName];
+
+ protected override IEnumerable ResourcesToRemove() => ["nginx-no-healthcheck", "api-service", "my-secret"];
+
+ protected override void ConfigureBuilder(IDistributedApplicationTestingBuilder builder)
+ {
+ // Relative paths resolve against the AppHost directory.
+ builder.AddDotnetProject(ServiceName, "../TUnit.Aspire.Tests.ApiService/TUnit.Aspire.Tests.ApiService.csproj")
+ .WithHttpEndpoint(env: "ASPNETCORE_HTTP_PORTS")
+ .WithCertificateTrustScope(CertificateTrustScope.None);
+ }
+}
+
+[ClassDataSource(Shared = SharedType.PerClass)]
+[Category("Docker")]
+[Category("Integration")]
+public class DotnetProjectResourceTests(DotnetProjectResourceFixture fixture)
+{
+ [Test]
+ public async Task Logs_FromDotnetProjectResource_AppearInTestOutput()
+ {
+ var marker = $"dotnet-project-marker-{Guid.NewGuid():N}";
+
+ var client = fixture.CreateHttpClient(DotnetProjectResourceFixture.ServiceName);
+ using var response = await client.GetAsync($"/log?message={Uri.EscapeDataString(marker)}");
+
+ await Assert.That((int)response.StatusCode).IsEqualTo(200);
+
+ var output = await PollForOutput(marker);
+ await Assert.That(output).Contains($"[{DotnetProjectResourceFixture.ServiceName}]");
+ await Assert.That(output).Contains(marker);
+ }
+
+ private static async Task PollForOutput(string expectedMarker)
+ {
+ var testContext = TestContext.Current!;
+ var sw = Stopwatch.StartNew();
+
+ while (sw.Elapsed < TimeSpan.FromSeconds(30))
+ {
+ var output = testContext.GetStandardOutput();
+ if (output.Contains(expectedMarker))
+ {
+ return output;
+ }
+
+ await Task.Delay(250);
+ }
+
+ return testContext.GetStandardOutput();
+ }
+}
diff --git a/tests/TUnit.Aspire.Tests/TUnit.Aspire.Tests.csproj b/tests/TUnit.Aspire.Tests/TUnit.Aspire.Tests.csproj
index 4fd89e03519..4aec73e2622 100644
--- a/tests/TUnit.Aspire.Tests/TUnit.Aspire.Tests.csproj
+++ b/tests/TUnit.Aspire.Tests/TUnit.Aspire.Tests.csproj
@@ -15,6 +15,8 @@
+
+