Skip to content

Commit 88547a4

Browse files
authored
Add MongoDB.Driver v3 client integration package (#7034)
* Add MongoDB.Driver v3 client integration package MongoDB.Driver shipped a version `3.0.0` that has binary breaking changes such that we can't build a single library that can load in both v2.x and v3.x. To continue supporting new versions of MongoDB.Driver, we need to add a new component Aspire.MongoDB.Driver.v3 that will work with the new version of MongoDB.Driver. See #3956 for the strategy to deal with these breaking changes. Fix #6380 * Fix health check when using keyed service.
1 parent 2cf951a commit 88547a4

File tree

14 files changed

+201
-16
lines changed

14 files changed

+201
-16
lines changed

Aspire.sln

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -641,6 +641,10 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ServiceBusWorker", "playgro
641641
EndProject
642642
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ServiceBus.AppHost", "playground\AzureServiceBus\ServiceBus.AppHost\ServiceBus.AppHost.csproj", "{A7EC9111-F3CC-46E8-B95E-3768481D67B4}"
643643
EndProject
644+
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}"
645+
EndProject
646+
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}"
647+
EndProject
644648
Global
645649
GlobalSection(SolutionConfigurationPlatforms) = preSolution
646650
Debug|Any CPU = Debug|Any CPU
@@ -1679,6 +1683,14 @@ Global
16791683
{A7EC9111-F3CC-46E8-B95E-3768481D67B4}.Debug|Any CPU.Build.0 = Debug|Any CPU
16801684
{A7EC9111-F3CC-46E8-B95E-3768481D67B4}.Release|Any CPU.ActiveCfg = Release|Any CPU
16811685
{A7EC9111-F3CC-46E8-B95E-3768481D67B4}.Release|Any CPU.Build.0 = Release|Any CPU
1686+
{FD53B608-138D-8FB1-AA57-9B8CD42CF13E}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
1687+
{FD53B608-138D-8FB1-AA57-9B8CD42CF13E}.Debug|Any CPU.Build.0 = Debug|Any CPU
1688+
{FD53B608-138D-8FB1-AA57-9B8CD42CF13E}.Release|Any CPU.ActiveCfg = Release|Any CPU
1689+
{FD53B608-138D-8FB1-AA57-9B8CD42CF13E}.Release|Any CPU.Build.0 = Release|Any CPU
1690+
{223AF8EB-3A4E-E778-4EBD-6E4876C308B6}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
1691+
{223AF8EB-3A4E-E778-4EBD-6E4876C308B6}.Debug|Any CPU.Build.0 = Debug|Any CPU
1692+
{223AF8EB-3A4E-E778-4EBD-6E4876C308B6}.Release|Any CPU.ActiveCfg = Release|Any CPU
1693+
{223AF8EB-3A4E-E778-4EBD-6E4876C308B6}.Release|Any CPU.Build.0 = Release|Any CPU
16821694
EndGlobalSection
16831695
GlobalSection(SolutionProperties) = preSolution
16841696
HideSolutionNode = FALSE
@@ -1987,6 +1999,8 @@ Global
19871999
{D2938171-1DBB-4E8D-AF16-97F75F1AE6DE} = {D173887B-AF42-4576-B9C1-96B9E9B3D9C0}
19882000
{162F0B66-E88F-4735-8CE0-BE8950F74CC6} = {D2938171-1DBB-4E8D-AF16-97F75F1AE6DE}
19892001
{A7EC9111-F3CC-46E8-B95E-3768481D67B4} = {D2938171-1DBB-4E8D-AF16-97F75F1AE6DE}
2002+
{FD53B608-138D-8FB1-AA57-9B8CD42CF13E} = {27381127-6C45-4B4C-8F18-41FF48DFE4B2}
2003+
{223AF8EB-3A4E-E778-4EBD-6E4876C308B6} = {C424395C-1235-41A4-BF55-07880A04368C}
19902004
EndGlobalSection
19912005
GlobalSection(ExtensibilityGlobals) = postSolution
19922006
SolutionGuid = {6DCEDFEC-988E-4CB3-B45B-191EB5086E0C}

Directory.Packages.props

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@
5858
<PackageVersion Include="AspNetCore.HealthChecks.AzureServiceBus" Version="8.0.1" />
5959
<PackageVersion Include="AspNetCore.HealthChecks.CosmosDb" Version="8.0.1" />
6060
<PackageVersion Include="AspNetCore.HealthChecks.Kafka" Version="8.0.1" />
61-
<PackageVersion Include="AspNetCore.HealthChecks.MongoDb" Version="8.1.0" />
61+
<PackageVersion Include="AspNetCore.HealthChecks.MongoDb" Version="9.0.0" />
6262
<PackageVersion Include="AspNetCore.HealthChecks.MySql" Version="8.0.1" />
6363
<PackageVersion Include="AspNetCore.HealthChecks.NpgSql" Version="8.0.2" />
6464
<PackageVersion Include="AspNetCore.HealthChecks.Oracle" Version="8.0.1" />
@@ -91,8 +91,8 @@
9191
<PackageVersion Include="Microsoft.FluentUI.AspNetCore.Components" Version="4.11.3" />
9292
<PackageVersion Include="Microsoft.FluentUI.AspNetCore.Components.Icons" Version="4.11.3" />
9393
<PackageVersion Include="Milvus.Client" Version="2.3.0-preview.1" />
94-
<PackageVersion Include="MongoDB.Driver" Version="[2.30.0,3.0.0)" />
95-
<PackageVersion Include="MongoDB.Driver.Core.Extensions.DiagnosticSources" Version="1.5.0" />
94+
<PackageVersion Include="MongoDB.Driver" Version="3.1.0" />
95+
<PackageVersion Include="MongoDB.Driver.Core.Extensions.DiagnosticSources" Version="2.0.0" />
9696
<PackageVersion Include="MySqlConnector.DependencyInjection" Version="2.3.6" />
9797
<PackageVersion Include="MySqlConnector.Logging.Microsoft.Extensions.Logging" Version="2.1.0" />
9898
<PackageVersion Include="NATS.Net" Version="2.5.3" />

playground/mongo/Mongo.ApiService/Mongo.ApiService.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
</PropertyGroup>
99

1010
<ItemGroup>
11-
<AspireProjectOrPackageReference Include="Aspire.MongoDB.Driver" />
11+
<AspireProjectOrPackageReference Include="Aspire.MongoDB.Driver.v3" />
1212

1313
<ProjectReference Include="..\..\Playground.ServiceDefaults\Playground.ServiceDefaults.csproj" />
1414
</ItemGroup>

src/Aspire.Hosting.MongoDB/MongoDBBuilderExtensions.cs

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
using Aspire.Hosting.MongoDB;
88
using Aspire.Hosting.Utils;
99
using Microsoft.Extensions.DependencyInjection;
10+
using MongoDB.Driver;
1011

1112
namespace Aspire.Hosting;
1213

@@ -71,7 +72,12 @@ public static IResourceBuilder<MongoDBServerResource> AddMongoDB(this IDistribut
7172
});
7273

7374
var healthCheckKey = $"{name}_check";
74-
builder.Services.AddHealthChecks().AddMongoDb(sp => connectionString ?? throw new InvalidOperationException("Connection string is unavailable"), name: healthCheckKey);
75+
// cache the client so it is reused on subsequent calls to the health check
76+
IMongoClient? client = null;
77+
builder.Services.AddHealthChecks()
78+
.AddMongoDb(
79+
sp => client ??= new MongoClient(connectionString ?? throw new InvalidOperationException("Connection string is unavailable")),
80+
name: healthCheckKey);
7581

7682
return builder
7783
.AddResource(mongoDBContainer)
@@ -117,7 +123,14 @@ public static IResourceBuilder<MongoDBDatabaseResource> AddDatabase(this IResour
117123
});
118124

119125
var healthCheckKey = $"{name}_check";
120-
builder.ApplicationBuilder.Services.AddHealthChecks().AddMongoDb(sp => connectionString ?? throw new InvalidOperationException("Connection string is unavailable"), name: healthCheckKey);
126+
// cache the database client so it is reused on subsequent calls to the health check
127+
IMongoDatabase? database = null;
128+
builder.ApplicationBuilder.Services.AddHealthChecks()
129+
.AddMongoDb(
130+
sp => database ??=
131+
new MongoClient(connectionString ?? throw new InvalidOperationException("Connection string is unavailable"))
132+
.GetDatabase(databaseName),
133+
name: healthCheckKey);
121134

122135
return builder.ApplicationBuilder
123136
.AddResource(mongoDBDatabase);
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<TargetFramework>$(DefaultTargetFramework)</TargetFramework>
5+
<IsPackable>true</IsPackable>
6+
<PackageTags>$(ComponentDatabasePackageTags) MongoDB</PackageTags>
7+
<PackageIconFullPath>$(SharedDir)MongoDB_300px.png</PackageIconFullPath>
8+
<Description>A generic MongoDB client that integrates with Aspire.</Description>
9+
<MinCodeCoverage>86</MinCodeCoverage>
10+
<!-- MongoDB.Driver.Core.Extensions.DiagnosticSources is not signed -->
11+
<NoWarn>$(NoWarn);CS8002</NoWarn>
12+
<!-- Disable package validation as this package hasn't shipped yet. -->
13+
<EnablePackageValidation>false</EnablePackageValidation>
14+
15+
<!-- Keep the same assembly name as the main library. -->
16+
<AssemblyName>Aspire.MongoDB.Driver</AssemblyName>
17+
<!-- PackageId defaults to AssemblyName, so need to reset it. -->
18+
<PackageId>$(MSBuildProjectName)</PackageId>
19+
</PropertyGroup>
20+
21+
<ItemGroup>
22+
<Compile Include="..\Aspire.MongoDB.Driver\AspireMongoDBDriverExtensions.cs" />
23+
<Compile Include="..\Aspire.MongoDB.Driver\AssemblyInfo.cs" />
24+
<Compile Include="..\Aspire.MongoDB.Driver\MongoDBSettings.cs" />
25+
<None Include="..\Aspire.MongoDB.Driver\README.md" Pack="true" PackagePath="\" />
26+
27+
<Compile Include="..\Common\ConfigurationSchemaAttributes.cs" Link="ConfigurationSchemaAttributes.cs" />
28+
<Compile Include="..\Common\HealthChecksExtensions.cs" Link="HealthChecksExtensions.cs" />
29+
<Compile Include="..\Common\ConnectionStringValidation.cs" Link="ConnectionStringValidation.cs" />
30+
</ItemGroup>
31+
32+
<ItemGroup>
33+
<PackageReference Include="AspNetCore.HealthChecks.MongoDb" />
34+
<PackageReference Include="MongoDB.Driver" />
35+
<PackageReference Include="Microsoft.Extensions.Configuration.Binder" />
36+
<PackageReference Include="Microsoft.Extensions.Diagnostics.HealthChecks" />
37+
<PackageReference Include="Microsoft.Extensions.Hosting.Abstractions" />
38+
<PackageReference Include="MongoDB.Driver.Core.Extensions.DiagnosticSources" />
39+
<PackageReference Include="OpenTelemetry.Extensions.Hosting" />
40+
</ItemGroup>
41+
42+
</Project>
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
{
2+
"definitions": {
3+
"logLevel": {
4+
"properties": {
5+
"MongoDB": {
6+
"$ref": "#/definitions/logLevelThreshold"
7+
},
8+
"MongoDB.Command": {
9+
"$ref": "#/definitions/logLevelThreshold"
10+
},
11+
"MongoDB.Connection": {
12+
"$ref": "#/definitions/logLevelThreshold"
13+
},
14+
"MongoDB.Internal": {
15+
"$ref": "#/definitions/logLevelThreshold"
16+
},
17+
"MongoDB.SDAM": {
18+
"$ref": "#/definitions/logLevelThreshold"
19+
},
20+
"MongoDB.ServerSelection": {
21+
"$ref": "#/definitions/logLevelThreshold"
22+
}
23+
}
24+
}
25+
},
26+
"type": "object",
27+
"properties": {
28+
"Aspire": {
29+
"type": "object",
30+
"properties": {
31+
"MongoDB": {
32+
"type": "object",
33+
"properties": {
34+
"Driver": {
35+
"type": "object",
36+
"properties": {
37+
"ConnectionString": {
38+
"type": "string",
39+
"description": "Gets or sets the connection string of the MongoDB database to connect to."
40+
},
41+
"DisableHealthChecks": {
42+
"type": "boolean",
43+
"description": "Gets or sets a boolean value that indicates whether the MongoDB health check is disabled or not.",
44+
"default": false
45+
},
46+
"DisableTracing": {
47+
"type": "boolean",
48+
"description": "Gets or sets a boolean value that indicates whether the OpenTelemetry tracing is disabled or not.",
49+
"default": false
50+
},
51+
"HealthCheckTimeout": {
52+
"type": "integer",
53+
"description": "Gets or sets a integer value that indicates the MongoDB health check timeout in milliseconds."
54+
}
55+
},
56+
"description": "Provides the client configuration settings for connecting to a MongoDB database using MongoDB driver."
57+
}
58+
}
59+
}
60+
}
61+
}
62+
}
63+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
#nullable enable
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
#nullable enable
2+
Aspire.MongoDB.Driver.MongoDBSettings
3+
Aspire.MongoDB.Driver.MongoDBSettings.ConnectionString.get -> string?
4+
Aspire.MongoDB.Driver.MongoDBSettings.ConnectionString.set -> void
5+
Aspire.MongoDB.Driver.MongoDBSettings.DisableHealthChecks.get -> bool
6+
Aspire.MongoDB.Driver.MongoDBSettings.DisableHealthChecks.set -> void
7+
Aspire.MongoDB.Driver.MongoDBSettings.DisableTracing.get -> bool
8+
Aspire.MongoDB.Driver.MongoDBSettings.DisableTracing.set -> void
9+
Aspire.MongoDB.Driver.MongoDBSettings.HealthCheckTimeout.get -> int?
10+
Aspire.MongoDB.Driver.MongoDBSettings.HealthCheckTimeout.set -> void
11+
Aspire.MongoDB.Driver.MongoDBSettings.MongoDBSettings() -> void
12+
Microsoft.Extensions.Hosting.AspireMongoDBDriverExtensions
13+
static Microsoft.Extensions.Hosting.AspireMongoDBDriverExtensions.AddKeyedMongoDBClient(this Microsoft.Extensions.Hosting.IHostApplicationBuilder! builder, string! name, System.Action<Aspire.MongoDB.Driver.MongoDBSettings!>? configureSettings = null, System.Action<MongoDB.Driver.MongoClientSettings!>? configureClientSettings = null) -> void
14+
static Microsoft.Extensions.Hosting.AspireMongoDBDriverExtensions.AddMongoDBClient(this Microsoft.Extensions.Hosting.IHostApplicationBuilder! builder, string! connectionName, System.Action<Aspire.MongoDB.Driver.MongoDBSettings!>? configureSettings = null, System.Action<MongoDB.Driver.MongoClientSettings!>? configureClientSettings = null) -> void

src/Components/Aspire.MongoDB.Driver/Aspire.MongoDB.Driver.csproj

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,9 @@
66
<PackageTags>$(ComponentDatabasePackageTags) MongoDB</PackageTags>
77
<PackageIconFullPath>$(SharedDir)MongoDB_300px.png</PackageIconFullPath>
88
<Description>A generic MongoDB client that integrates with Aspire.</Description>
9-
<NoWarn>$(NoWarn);CS8002</NoWarn> <!-- MongoDB.Driver.Core.Extensions.DiagnosticSources and AspNetCore.HealthChecks.MongoDb packages are not signed -->
9+
<!-- MongoDB.Driver.Core.Extensions.DiagnosticSources and AspNetCore.HealthChecks.MongoDb packages are not signed -->
10+
<NoWarn>$(NoWarn);CS8002</NoWarn>
11+
<DefineConstants>$(DefineConstants);MONGODB_V2</DefineConstants>
1012
</PropertyGroup>
1113

1214
<PropertyGroup>
@@ -20,14 +22,13 @@
2022
</ItemGroup>
2123

2224
<ItemGroup>
23-
<PackageReference Include="AspNetCore.HealthChecks.MongoDb" />
24-
<PackageReference Include="MongoDB.Driver" />
25+
<PackageReference Include="AspNetCore.HealthChecks.MongoDb" VersionOverride="[8.1.0,9.0.0)" />
26+
<PackageReference Include="MongoDB.Driver" VersionOverride="[2.30.0,3.0.0)" />
2527
<PackageReference Include="Microsoft.Extensions.Configuration.Binder" />
2628
<PackageReference Include="Microsoft.Extensions.Diagnostics.HealthChecks" />
2729
<PackageReference Include="Microsoft.Extensions.Hosting.Abstractions" />
28-
<PackageReference Include="MongoDB.Driver.Core.Extensions.DiagnosticSources" />
30+
<PackageReference Include="MongoDB.Driver.Core.Extensions.DiagnosticSources" VersionOverride="[1.5.0,2.0.0)" />
2931
<PackageReference Include="OpenTelemetry.Extensions.Hosting" />
3032
</ItemGroup>
3133

32-
3334
</Project>

src/Components/Aspire.MongoDB.Driver/AspireMongoDBDriverExtensions.cs

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,8 @@ private static void AddMongoDBClient(
9595

9696
builder.AddMongoDatabase(settings.ConnectionString, serviceKey);
9797
builder.AddHealthCheck(
98-
serviceKey is null ? "MongoDB.Driver" : $"MongoDB.Driver_{connectionName}",
98+
connectionName,
99+
serviceKey,
99100
settings);
100101
}
101102

@@ -158,18 +159,27 @@ private static void AddMongoDatabase(
158159

159160
private static void AddHealthCheck(
160161
this IHostApplicationBuilder builder,
161-
string healthCheckName,
162+
string connectionName,
163+
object? serviceKey,
162164
MongoDBSettings settings)
163165
{
164166
if (settings.DisableHealthChecks || string.IsNullOrWhiteSpace(settings.ConnectionString))
165167
{
166168
return;
167169
}
168170

171+
var healthCheckName = serviceKey is null ? "MongoDB.Driver" : $"MongoDB.Driver_{connectionName}";
169172
builder.TryAddHealthCheck(
170173
healthCheckName,
171174
healthCheck => healthCheck.AddMongoDb(
175+
#if MONGODB_V2
172176
settings.ConnectionString,
177+
#else
178+
serviceKey is null
179+
? sp => sp.GetRequiredService<IMongoClient>()
180+
: sp => sp.GetRequiredKeyedService<IMongoClient>(serviceKey),
181+
_ => MongoUrl.Create(settings.ConnectionString).DatabaseName,
182+
#endif
173183
healthCheckName,
174184
null,
175185
null,
@@ -194,7 +204,7 @@ private static MongoClient CreateMongoClient(
194204
configureClientSettings?.Invoke(clientSettings);
195205

196206
clientSettings.LoggingSettings ??= new LoggingSettings(serviceProvider.GetService<ILoggerFactory>());
197-
207+
198208
var aspireVersion = typeof(MongoDBSettings).Assembly.GetName().Version?.ToString();
199209
if (clientSettings.LibraryInfo != null)
200210
{
@@ -204,7 +214,7 @@ private static MongoClient CreateMongoClient(
204214
{
205215
clientSettings.LibraryInfo = new LibraryInfo("aspire", aspireVersion);
206216
}
207-
217+
208218
return new MongoClient(clientSettings);
209219
}
210220

0 commit comments

Comments
 (0)