diff --git a/Aspire.sln b/Aspire.sln
index 98c27115e6..c614ce3dda 100644
--- a/Aspire.sln
+++ b/Aspire.sln
@@ -637,6 +637,10 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ServiceBusWorker", "playgro
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ServiceBus.AppHost", "playground\AzureServiceBus\ServiceBus.AppHost\ServiceBus.AppHost.csproj", "{A7EC9111-F3CC-46E8-B95E-3768481D67B4}"
EndProject
+Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Aspire.MongoDB.Driver.v3", "src\Components\Aspire.MongoDB.Driver.v3\Aspire.MongoDB.Driver.v3.csproj", "{FD53B608-138D-8FB1-AA57-9B8CD42CF13E}"
+EndProject
+Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Aspire.MongoDB.Driver.v3.Tests", "tests\Aspire.MongoDB.Driver.v3.Tests\Aspire.MongoDB.Driver.v3.Tests.csproj", "{223AF8EB-3A4E-E778-4EBD-6E4876C308B6}"
+EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
@@ -1667,6 +1671,14 @@ Global
{A7EC9111-F3CC-46E8-B95E-3768481D67B4}.Debug|Any CPU.Build.0 = Debug|Any CPU
{A7EC9111-F3CC-46E8-B95E-3768481D67B4}.Release|Any CPU.ActiveCfg = Release|Any CPU
{A7EC9111-F3CC-46E8-B95E-3768481D67B4}.Release|Any CPU.Build.0 = Release|Any CPU
+ {FD53B608-138D-8FB1-AA57-9B8CD42CF13E}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
+ {FD53B608-138D-8FB1-AA57-9B8CD42CF13E}.Debug|Any CPU.Build.0 = Debug|Any CPU
+ {FD53B608-138D-8FB1-AA57-9B8CD42CF13E}.Release|Any CPU.ActiveCfg = Release|Any CPU
+ {FD53B608-138D-8FB1-AA57-9B8CD42CF13E}.Release|Any CPU.Build.0 = Release|Any CPU
+ {223AF8EB-3A4E-E778-4EBD-6E4876C308B6}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
+ {223AF8EB-3A4E-E778-4EBD-6E4876C308B6}.Debug|Any CPU.Build.0 = Debug|Any CPU
+ {223AF8EB-3A4E-E778-4EBD-6E4876C308B6}.Release|Any CPU.ActiveCfg = Release|Any CPU
+ {223AF8EB-3A4E-E778-4EBD-6E4876C308B6}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
@@ -1973,6 +1985,8 @@ Global
{D2938171-1DBB-4E8D-AF16-97F75F1AE6DE} = {D173887B-AF42-4576-B9C1-96B9E9B3D9C0}
{162F0B66-E88F-4735-8CE0-BE8950F74CC6} = {D2938171-1DBB-4E8D-AF16-97F75F1AE6DE}
{A7EC9111-F3CC-46E8-B95E-3768481D67B4} = {D2938171-1DBB-4E8D-AF16-97F75F1AE6DE}
+ {FD53B608-138D-8FB1-AA57-9B8CD42CF13E} = {27381127-6C45-4B4C-8F18-41FF48DFE4B2}
+ {223AF8EB-3A4E-E778-4EBD-6E4876C308B6} = {C424395C-1235-41A4-BF55-07880A04368C}
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {6DCEDFEC-988E-4CB3-B45B-191EB5086E0C}
diff --git a/Directory.Packages.props b/Directory.Packages.props
index 294d2ba7d8..173977c051 100644
--- a/Directory.Packages.props
+++ b/Directory.Packages.props
@@ -58,7 +58,7 @@
-
+
@@ -90,8 +90,8 @@
-
-
+
+
diff --git a/playground/mongo/Mongo.ApiService/Mongo.ApiService.csproj b/playground/mongo/Mongo.ApiService/Mongo.ApiService.csproj
index 83ba592918..fa252be52b 100644
--- a/playground/mongo/Mongo.ApiService/Mongo.ApiService.csproj
+++ b/playground/mongo/Mongo.ApiService/Mongo.ApiService.csproj
@@ -8,7 +8,7 @@
-
+
diff --git a/src/Aspire.Hosting.MongoDB/MongoDBBuilderExtensions.cs b/src/Aspire.Hosting.MongoDB/MongoDBBuilderExtensions.cs
index e6170e672f..03eddf2d92 100644
--- a/src/Aspire.Hosting.MongoDB/MongoDBBuilderExtensions.cs
+++ b/src/Aspire.Hosting.MongoDB/MongoDBBuilderExtensions.cs
@@ -7,6 +7,7 @@
using Aspire.Hosting.MongoDB;
using Aspire.Hosting.Utils;
using Microsoft.Extensions.DependencyInjection;
+using MongoDB.Driver;
namespace Aspire.Hosting;
@@ -71,7 +72,12 @@ public static IResourceBuilder AddMongoDB(this IDistribut
});
var healthCheckKey = $"{name}_check";
- builder.Services.AddHealthChecks().AddMongoDb(sp => connectionString ?? throw new InvalidOperationException("Connection string is unavailable"), name: healthCheckKey);
+ // cache the client so it is reused on subsequent calls to the health check
+ IMongoClient? client = null;
+ builder.Services.AddHealthChecks()
+ .AddMongoDb(
+ sp => client ??= new MongoClient(connectionString ?? throw new InvalidOperationException("Connection string is unavailable")),
+ name: healthCheckKey);
return builder
.AddResource(mongoDBContainer)
@@ -117,7 +123,14 @@ public static IResourceBuilder AddDatabase(this IResour
});
var healthCheckKey = $"{name}_check";
- builder.ApplicationBuilder.Services.AddHealthChecks().AddMongoDb(sp => connectionString ?? throw new InvalidOperationException("Connection string is unavailable"), name: healthCheckKey);
+ // cache the database client so it is reused on subsequent calls to the health check
+ IMongoDatabase? database = null;
+ builder.ApplicationBuilder.Services.AddHealthChecks()
+ .AddMongoDb(
+ sp => database ??=
+ new MongoClient(connectionString ?? throw new InvalidOperationException("Connection string is unavailable"))
+ .GetDatabase(databaseName),
+ name: healthCheckKey);
return builder.ApplicationBuilder
.AddResource(mongoDBDatabase);
diff --git a/src/Components/Aspire.MongoDB.Driver.v3/Aspire.MongoDB.Driver.v3.csproj b/src/Components/Aspire.MongoDB.Driver.v3/Aspire.MongoDB.Driver.v3.csproj
new file mode 100644
index 0000000000..5664621518
--- /dev/null
+++ b/src/Components/Aspire.MongoDB.Driver.v3/Aspire.MongoDB.Driver.v3.csproj
@@ -0,0 +1,42 @@
+
+
+
+ $(DefaultTargetFramework)
+ true
+ $(ComponentDatabasePackageTags) MongoDB
+ $(SharedDir)MongoDB_300px.png
+ A generic MongoDB client that integrates with Aspire.
+ 86
+
+ $(NoWarn);CS8002
+
+ false
+
+
+ Aspire.MongoDB.Driver
+
+ $(MSBuildProjectName)
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
diff --git a/src/Components/Aspire.MongoDB.Driver.v3/ConfigurationSchema.json b/src/Components/Aspire.MongoDB.Driver.v3/ConfigurationSchema.json
new file mode 100644
index 0000000000..42f195ca68
--- /dev/null
+++ b/src/Components/Aspire.MongoDB.Driver.v3/ConfigurationSchema.json
@@ -0,0 +1,63 @@
+{
+ "definitions": {
+ "logLevel": {
+ "properties": {
+ "MongoDB": {
+ "$ref": "#/definitions/logLevelThreshold"
+ },
+ "MongoDB.Command": {
+ "$ref": "#/definitions/logLevelThreshold"
+ },
+ "MongoDB.Connection": {
+ "$ref": "#/definitions/logLevelThreshold"
+ },
+ "MongoDB.Internal": {
+ "$ref": "#/definitions/logLevelThreshold"
+ },
+ "MongoDB.SDAM": {
+ "$ref": "#/definitions/logLevelThreshold"
+ },
+ "MongoDB.ServerSelection": {
+ "$ref": "#/definitions/logLevelThreshold"
+ }
+ }
+ }
+ },
+ "type": "object",
+ "properties": {
+ "Aspire": {
+ "type": "object",
+ "properties": {
+ "MongoDB": {
+ "type": "object",
+ "properties": {
+ "Driver": {
+ "type": "object",
+ "properties": {
+ "ConnectionString": {
+ "type": "string",
+ "description": "Gets or sets the connection string of the MongoDB database to connect to."
+ },
+ "DisableHealthChecks": {
+ "type": "boolean",
+ "description": "Gets or sets a boolean value that indicates whether the MongoDB health check is disabled or not.",
+ "default": false
+ },
+ "DisableTracing": {
+ "type": "boolean",
+ "description": "Gets or sets a boolean value that indicates whether the OpenTelemetry tracing is disabled or not.",
+ "default": false
+ },
+ "HealthCheckTimeout": {
+ "type": "integer",
+ "description": "Gets or sets a integer value that indicates the MongoDB health check timeout in milliseconds."
+ }
+ },
+ "description": "Provides the client configuration settings for connecting to a MongoDB database using MongoDB driver."
+ }
+ }
+ }
+ }
+ }
+ }
+}
diff --git a/src/Components/Aspire.MongoDB.Driver.v3/PublicAPI.Shipped.txt b/src/Components/Aspire.MongoDB.Driver.v3/PublicAPI.Shipped.txt
new file mode 100644
index 0000000000..7dc5c58110
--- /dev/null
+++ b/src/Components/Aspire.MongoDB.Driver.v3/PublicAPI.Shipped.txt
@@ -0,0 +1 @@
+#nullable enable
diff --git a/src/Components/Aspire.MongoDB.Driver.v3/PublicAPI.Unshipped.txt b/src/Components/Aspire.MongoDB.Driver.v3/PublicAPI.Unshipped.txt
new file mode 100644
index 0000000000..d54b7e17c0
--- /dev/null
+++ b/src/Components/Aspire.MongoDB.Driver.v3/PublicAPI.Unshipped.txt
@@ -0,0 +1,14 @@
+#nullable enable
+Aspire.MongoDB.Driver.MongoDBSettings
+Aspire.MongoDB.Driver.MongoDBSettings.ConnectionString.get -> string?
+Aspire.MongoDB.Driver.MongoDBSettings.ConnectionString.set -> void
+Aspire.MongoDB.Driver.MongoDBSettings.DisableHealthChecks.get -> bool
+Aspire.MongoDB.Driver.MongoDBSettings.DisableHealthChecks.set -> void
+Aspire.MongoDB.Driver.MongoDBSettings.DisableTracing.get -> bool
+Aspire.MongoDB.Driver.MongoDBSettings.DisableTracing.set -> void
+Aspire.MongoDB.Driver.MongoDBSettings.HealthCheckTimeout.get -> int?
+Aspire.MongoDB.Driver.MongoDBSettings.HealthCheckTimeout.set -> void
+Aspire.MongoDB.Driver.MongoDBSettings.MongoDBSettings() -> void
+Microsoft.Extensions.Hosting.AspireMongoDBDriverExtensions
+static Microsoft.Extensions.Hosting.AspireMongoDBDriverExtensions.AddKeyedMongoDBClient(this Microsoft.Extensions.Hosting.IHostApplicationBuilder! builder, string! name, System.Action? configureSettings = null, System.Action? configureClientSettings = null) -> void
+static Microsoft.Extensions.Hosting.AspireMongoDBDriverExtensions.AddMongoDBClient(this Microsoft.Extensions.Hosting.IHostApplicationBuilder! builder, string! connectionName, System.Action? configureSettings = null, System.Action? configureClientSettings = null) -> void
diff --git a/src/Components/Aspire.MongoDB.Driver/Aspire.MongoDB.Driver.csproj b/src/Components/Aspire.MongoDB.Driver/Aspire.MongoDB.Driver.csproj
index 351b1508c3..d9eab37c8b 100644
--- a/src/Components/Aspire.MongoDB.Driver/Aspire.MongoDB.Driver.csproj
+++ b/src/Components/Aspire.MongoDB.Driver/Aspire.MongoDB.Driver.csproj
@@ -6,7 +6,9 @@
$(ComponentDatabasePackageTags) MongoDB
$(SharedDir)MongoDB_300px.png
A generic MongoDB client that integrates with Aspire.
- $(NoWarn);CS8002
+
+ $(NoWarn);CS8002
+ $(DefineConstants);MONGODB_V2
@@ -20,14 +22,13 @@
-
-
+
+
-
+
-
diff --git a/src/Components/Aspire.MongoDB.Driver/AspireMongoDBDriverExtensions.cs b/src/Components/Aspire.MongoDB.Driver/AspireMongoDBDriverExtensions.cs
index 0c8a1c2e15..abd26ef8ae 100644
--- a/src/Components/Aspire.MongoDB.Driver/AspireMongoDBDriverExtensions.cs
+++ b/src/Components/Aspire.MongoDB.Driver/AspireMongoDBDriverExtensions.cs
@@ -95,7 +95,8 @@ private static void AddMongoDBClient(
builder.AddMongoDatabase(settings.ConnectionString, serviceKey);
builder.AddHealthCheck(
- serviceKey is null ? "MongoDB.Driver" : $"MongoDB.Driver_{connectionName}",
+ connectionName,
+ serviceKey,
settings);
}
@@ -158,7 +159,8 @@ private static void AddMongoDatabase(
private static void AddHealthCheck(
this IHostApplicationBuilder builder,
- string healthCheckName,
+ string connectionName,
+ object? serviceKey,
MongoDBSettings settings)
{
if (settings.DisableHealthChecks || string.IsNullOrWhiteSpace(settings.ConnectionString))
@@ -166,10 +168,18 @@ private static void AddHealthCheck(
return;
}
+ var healthCheckName = serviceKey is null ? "MongoDB.Driver" : $"MongoDB.Driver_{connectionName}";
builder.TryAddHealthCheck(
healthCheckName,
healthCheck => healthCheck.AddMongoDb(
+#if MONGODB_V2
settings.ConnectionString,
+#else
+ serviceKey is null
+ ? sp => sp.GetRequiredService()
+ : sp => sp.GetRequiredKeyedService(serviceKey),
+ _ => MongoUrl.Create(settings.ConnectionString).DatabaseName,
+#endif
healthCheckName,
null,
null,
@@ -194,7 +204,7 @@ private static MongoClient CreateMongoClient(
configureClientSettings?.Invoke(clientSettings);
clientSettings.LoggingSettings ??= new LoggingSettings(serviceProvider.GetService());
-
+
var aspireVersion = typeof(MongoDBSettings).Assembly.GetName().Version?.ToString();
if (clientSettings.LibraryInfo != null)
{
@@ -204,7 +214,7 @@ private static MongoClient CreateMongoClient(
{
clientSettings.LibraryInfo = new LibraryInfo("aspire", aspireVersion);
}
-
+
return new MongoClient(clientSettings);
}
diff --git a/tests/Aspire.Hosting.MongoDB.Tests/Aspire.Hosting.MongoDB.Tests.csproj b/tests/Aspire.Hosting.MongoDB.Tests/Aspire.Hosting.MongoDB.Tests.csproj
index ff78511994..895bf53fe8 100644
--- a/tests/Aspire.Hosting.MongoDB.Tests/Aspire.Hosting.MongoDB.Tests.csproj
+++ b/tests/Aspire.Hosting.MongoDB.Tests/Aspire.Hosting.MongoDB.Tests.csproj
@@ -6,7 +6,7 @@
-
+
diff --git a/tests/Aspire.MongoDB.Driver.Tests/Aspire.MongoDB.Driver.Tests.csproj b/tests/Aspire.MongoDB.Driver.Tests/Aspire.MongoDB.Driver.Tests.csproj
index 110c1c7a32..23d7cf45ae 100644
--- a/tests/Aspire.MongoDB.Driver.Tests/Aspire.MongoDB.Driver.Tests.csproj
+++ b/tests/Aspire.MongoDB.Driver.Tests/Aspire.MongoDB.Driver.Tests.csproj
@@ -10,6 +10,10 @@
+
+
+
+
diff --git a/tests/Aspire.MongoDB.Driver.v3.Tests/Aspire.MongoDB.Driver.v3.Tests.csproj b/tests/Aspire.MongoDB.Driver.v3.Tests/Aspire.MongoDB.Driver.v3.Tests.csproj
new file mode 100644
index 0000000000..70e9b24096
--- /dev/null
+++ b/tests/Aspire.MongoDB.Driver.v3.Tests/Aspire.MongoDB.Driver.v3.Tests.csproj
@@ -0,0 +1,22 @@
+
+
+
+ $(DefaultTargetFramework)
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
diff --git a/tests/Shared/RepoTesting/Directory.Packages.Helix.props b/tests/Shared/RepoTesting/Directory.Packages.Helix.props
index edf588c1a4..a335c0177f 100644
--- a/tests/Shared/RepoTesting/Directory.Packages.Helix.props
+++ b/tests/Shared/RepoTesting/Directory.Packages.Helix.props
@@ -63,6 +63,7 @@
+