Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion playground/mongo/Mongo.AppHost/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

builder.AddProject<Projects.Mongo_ApiService>("api")
.WithExternalHttpEndpoints()
.WithReference(db);
.WithReference(db).WaitFor(db);

#if !SKIP_DASHBOARD_REFERENCE
// This project is only added in playground projects to support development/debugging
Expand Down
5 changes: 5 additions & 0 deletions src/Aspire.Hosting.MongoDB/Aspire.Hosting.MongoDB.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
<PackageTags>aspire integration hosting MongoDB</PackageTags>
<PackageIconFullPath>$(SharedDir)MongoDB_300px.png</PackageIconFullPath>
<Description>MongoDB support for .NET Aspire.</Description>
<NoWarn>$(NoWarn);CS8002</NoWarn> <!-- MongoDB.Driver.Core.Extensions.DiagnosticSources and AspNetCore.HealthChecks.MongoDb packages are not signed -->
</PropertyGroup>

<PropertyGroup>
Expand All @@ -20,4 +21,8 @@
<ItemGroup>
<ProjectReference Include="..\Aspire.Hosting\Aspire.Hosting.csproj" />
</ItemGroup>

<ItemGroup>
<PackageReference Include="AspNetCore.HealthChecks.MongoDb" />
</ItemGroup>
</Project>
19 changes: 18 additions & 1 deletion src/Aspire.Hosting.MongoDB/MongoDBBuilderExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using Aspire.Hosting.ApplicationModel;
using Aspire.Hosting.MongoDB;
using Aspire.Hosting.Utils;
using Microsoft.Extensions.DependencyInjection;

namespace Aspire.Hosting;

Expand All @@ -29,11 +30,27 @@ public static IResourceBuilder<MongoDBServerResource> AddMongoDB(this IDistribut

var mongoDBContainer = new MongoDBServerResource(name);

string? connectionString = null;

builder.Eventing.Subscribe<ConnectionStringAvailableEvent>(mongoDBContainer, async (@event, ct) =>
{
connectionString = await mongoDBContainer.ConnectionStringExpression.GetValueAsync(ct).ConfigureAwait(false);

if (connectionString == null)
{
throw new DistributedApplicationException($"ConnectionStringAvailableEvent was published for the '{mongoDBContainer.Name}' resource but the connection string was null.");
}
});

var healthCheckKey = $"{name}_check";
builder.Services.AddHealthChecks().AddMongoDb(sp => connectionString ?? throw new InvalidOperationException("Connection string is unavailable"), name: healthCheckKey);

return builder
.AddResource(mongoDBContainer)
.WithEndpoint(port: port, targetPort: DefaultContainerPort, name: MongoDBServerResource.PrimaryEndpointName)
.WithImage(MongoDBContainerImageTags.Image, MongoDBContainerImageTags.Tag)
.WithImageRegistry(MongoDBContainerImageTags.Registry);
.WithImageRegistry(MongoDBContainerImageTags.Registry)
.WithHealthCheck(healthCheckKey);
}

/// <summary>
Expand Down
2 changes: 1 addition & 1 deletion src/Aspire.Hosting.PostgreSQL/PostgresBuilderExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ public static IResourceBuilder<PostgresServerResource> AddPostgres(this IDistrib

if (connectionString == null)
{
throw new DistributedApplicationException($"ConnectionStringAvailableEvent was published for the '{postgresServer}' resource but the connection string was null.");
throw new DistributedApplicationException($"ConnectionStringAvailableEvent was published for the '{postgresServer.Name}' resource but the connection string was null.");
}

var lookup = builder.Resources.OfType<PostgresDatabaseResource>().ToDictionary(d => d.Name);
Expand Down
2 changes: 1 addition & 1 deletion src/Aspire.Hosting.Redis/RedisBuilderExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ public static IResourceBuilder<RedisResource> AddRedis(this IDistributedApplicat

if (connectionString == null)
{
throw new DistributedApplicationException($"ConnectionStringAvailableEvent was published for the '{redis}' resource but the connection string was null.");
throw new DistributedApplicationException($"ConnectionStringAvailableEvent was published for the '{redis.Name}' resource but the connection string was null.");
}
});

Expand Down
2 changes: 1 addition & 1 deletion src/Aspire.Hosting.SqlServer/SqlServerBuilderExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ public static IResourceBuilder<SqlServerServerResource> AddSqlServer(this IDistr

if (connectionString == null)
{
throw new DistributedApplicationException($"ConnectionStringAvailableEvent was published for the '{sqlServer}' resource but the connection string was null.");
throw new DistributedApplicationException($"ConnectionStringAvailableEvent was published for the '{sqlServer.Name}' resource but the connection string was null.");
}

var lookup = builder.Resources.OfType<SqlServerDatabaseResource>().ToDictionary(d => d.Name);
Expand Down
41 changes: 41 additions & 0 deletions tests/Aspire.Hosting.MongoDB.Tests/MongoDbFunctionalTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@
using Xunit;
using Xunit.Abstractions;
using Polly;
using Aspire.Hosting.ApplicationModel;
using Microsoft.Extensions.Diagnostics.HealthChecks;

namespace Aspire.Hosting.MongoDB.Tests;

Expand All @@ -26,6 +28,45 @@ public class MongoDbFunctionalTests(ITestOutputHelper testOutputHelper)
new() { Name = "Schindler's List"},
];

[Fact]
[RequiresDocker]
public async Task VerifyWaitForOnMongoBlocksDependentResources()
{
var cts = new CancellationTokenSource(TimeSpan.FromMinutes(3));
using var builder = TestDistributedApplicationBuilder.CreateWithTestContainerRegistry(testOutputHelper);

var healthCheckTcs = new TaskCompletionSource<HealthCheckResult>();
builder.Services.AddHealthChecks().AddAsyncCheck("blocking_check", () =>
{
return healthCheckTcs.Task;
});

var resource = builder.AddMongoDB("resource")
.WithHealthCheck("blocking_check");

var dependentResource = builder.AddMongoDB("dependentresource")
.WaitFor(resource);

using var app = builder.Build();

var pendingStart = app.StartAsync(cts.Token);

var rns = app.Services.GetRequiredService<ResourceNotificationService>();

await rns.WaitForResourceAsync(resource.Resource.Name, KnownResourceStates.Running, cts.Token);

await rns.WaitForResourceAsync(dependentResource.Resource.Name, KnownResourceStates.Waiting, cts.Token);

healthCheckTcs.SetResult(HealthCheckResult.Healthy());

await rns.WaitForResourceAsync(resource.Resource.Name, (re => re.Snapshot.HealthStatus == HealthStatus.Healthy), cts.Token);

await rns.WaitForResourceAsync(dependentResource.Resource.Name, KnownResourceStates.Running, cts.Token);

await pendingStart;
await app.StopAsync();
}

[Fact]
[RequiresDocker]
public async Task VerifyMongoDBResource()
Expand Down