Skip to content

Commit df2dd63

Browse files
committed
Update to latest HealthChecks versions.
Fix Hosting.RabbitMQ.Tests
1 parent ce1b75d commit df2dd63

File tree

8 files changed

+43
-25
lines changed

8 files changed

+43
-25
lines changed

Directory.Packages.props

+2-2
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@
7070
<PackageVersion Include="AspNetCore.HealthChecks.MySql" Version="8.0.1" />
7171
<PackageVersion Include="AspNetCore.HealthChecks.NpgSql" Version="8.0.2" />
7272
<PackageVersion Include="AspNetCore.HealthChecks.Oracle" Version="8.0.1" />
73-
<PackageVersion Include="AspNetCore.HealthChecks.Rabbitmq" Version="8.0.2" />
73+
<PackageVersion Include="AspNetCore.HealthChecks.Rabbitmq" Version="9.0.0" />
7474
<PackageVersion Include="AspNetCore.HealthChecks.Redis" Version="8.0.1" />
7575
<PackageVersion Include="AspNetCore.HealthChecks.SqlServer" Version="8.0.2" />
7676
<PackageVersion Include="AspNetCore.HealthChecks.Uris" Version="8.0.1" />
@@ -125,7 +125,7 @@
125125
<PackageVersion Include="Polly.Extensions" Version="8.5.0" />
126126
<PackageVersion Include="Pomelo.EntityFrameworkCore.MySql" Version="8.0.2" />
127127
<PackageVersion Include="Qdrant.Client" Version="1.12.0" />
128-
<PackageVersion Include="RabbitMQ.Client" Version="[6.8.1,7.0.0)" />
128+
<PackageVersion Include="RabbitMQ.Client" Version="7.0.0" />
129129
<PackageVersion Include="StackExchange.Redis" Version="2.8.22" />
130130
<PackageVersion Include="System.IO.Hashing" Version="8.0.0" />
131131
<PackageVersion Include="Yarp.ReverseProxy" Version="2.2.0" />

src/Aspire.Hosting.RabbitMQ/RabbitMQBuilderExtensions.cs

+16-5
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
using Aspire.Hosting.RabbitMQ;
66
using Aspire.Hosting.Utils;
77
using Microsoft.Extensions.DependencyInjection;
8+
using RabbitMQ.Client;
89

910
namespace Aspire.Hosting;
1011

@@ -52,12 +53,22 @@ public static IResourceBuilder<RabbitMQServerResource> AddRabbitMQ(this IDistrib
5253
});
5354

5455
var healthCheckKey = $"{name}_check";
55-
builder.Services.AddHealthChecks().AddRabbitMQ((sp, options) =>
56+
// cache the connection so it is reused on subsequent calls to the health check
57+
IConnection? connection = null;
58+
builder.Services.AddHealthChecks().AddRabbitMQ(async (sp) =>
5659
{
57-
// NOTE: This specific callback signature needs to be used to ensure
58-
// that execution of this setup callback is deferred until after
59-
// the container is build & started.
60-
options.ConnectionUri = new Uri(connectionString!);
60+
// NOTE: Ensure that execution of this setup callback is deferred until after
61+
// the container is built & started.
62+
return connection ??= await CreateConnection(connectionString!).ConfigureAwait(false);
63+
64+
static Task<IConnection> CreateConnection(string connectionString)
65+
{
66+
var factory = new ConnectionFactory
67+
{
68+
Uri = new Uri(connectionString)
69+
};
70+
return factory.CreateConnectionAsync();
71+
}
6172
}, healthCheckKey);
6273

6374
var rabbitmq = builder.AddResource(rabbitMq)

src/Components/Aspire.RabbitMQ.Client/Aspire.RabbitMQ.Client.csproj

+2-2
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,11 @@
1919
</ItemGroup>
2020

2121
<ItemGroup>
22-
<PackageReference Include="AspNetCore.HealthChecks.Rabbitmq" />
22+
<PackageReference Include="AspNetCore.HealthChecks.Rabbitmq" VersionOverride="[8.0.2,9.0.0)" />
2323
<PackageReference Include="Microsoft.Extensions.Configuration.Binder" />
2424
<PackageReference Include="Microsoft.Extensions.Hosting.Abstractions" />
2525
<PackageReference Include="Polly.Core" />
26-
<PackageReference Include="RabbitMQ.Client" />
26+
<PackageReference Include="RabbitMQ.Client" VersionOverride="[6.8.1,7.0.0)" />
2727
<PackageReference Include="OpenTelemetry.Extensions.Hosting" />
2828
</ItemGroup>
2929

src/Components/Aspire.RabbitMQ.Client/AspireRabbitMQExtensions.cs

+6-1
Original file line numberDiff line numberDiff line change
@@ -145,9 +145,14 @@ IConnectionFactory CreateConnectionFactory(IServiceProvider sp)
145145
try
146146
{
147147
// if the IConnection can't be resolved, make a health check that will fail
148+
var connection = serviceKey is null ? sp.GetRequiredService<IConnection>() : sp.GetRequiredKeyedService<IConnection>(serviceKey);
149+
#if RABBITMQ_V6
148150
var options = new RabbitMQHealthCheckOptions();
149-
options.Connection = serviceKey is null ? sp.GetRequiredService<IConnection>() : sp.GetRequiredKeyedService<IConnection>(serviceKey);
151+
options.Connection = connection;
150152
return new RabbitMQHealthCheck(options);
153+
#else
154+
return new RabbitMQHealthCheck(connection);
155+
#endif
151156
}
152157
catch (Exception ex)
153158
{

tests/Aspire.Hosting.RabbitMQ.Tests/Aspire.Hosting.RabbitMQ.Tests.csproj

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
<ItemGroup>
88
<ProjectReference Include="..\..\src\Aspire.Hosting.AppHost\Aspire.Hosting.AppHost.csproj" />
99
<ProjectReference Include="..\..\src\Aspire.Hosting.RabbitMQ\Aspire.Hosting.RabbitMQ.csproj" />
10-
<ProjectReference Include="..\..\src\Components\Aspire.RabbitMQ.Client\Aspire.RabbitMQ.Client.csproj" />
10+
<ProjectReference Include="..\..\src\Components\Aspire.RabbitMQ.Client.v7\Aspire.RabbitMQ.Client.v7.csproj" />
1111
<ProjectReference Include="..\Aspire.Hosting.Tests\Aspire.Hosting.Tests.csproj" />
1212
</ItemGroup>
1313

tests/Aspire.Hosting.RabbitMQ.Tests/RabbitMQFunctionalTests.cs

+14-13
Original file line numberDiff line numberDiff line change
@@ -80,17 +80,17 @@ public async Task VerifyRabbitMQResource()
8080

8181
var connection = host.Services.GetRequiredService<IConnection>();
8282

83-
using var channel = connection.CreateModel();
83+
await using var channel = await connection.CreateChannelAsync();
8484
const string queueName = "hello";
85-
channel.QueueDeclare(queueName, durable: false, exclusive: false, autoDelete: false, arguments: null);
85+
await channel.QueueDeclareAsync(queueName, durable: false, exclusive: false, autoDelete: false, arguments: null);
8686

8787
const string message = "Hello World!";
8888
var body = Encoding.UTF8.GetBytes(message);
8989

90-
channel.BasicPublish(exchange: string.Empty, routingKey: queueName, basicProperties: null, body: body);
90+
await channel.BasicPublishAsync(exchange: string.Empty, routingKey: queueName, body: body);
9191

92-
var result = channel.BasicGet(queueName, true);
93-
Assert.Equal(message, Encoding.UTF8.GetString(result.Body.Span));
92+
var result = await channel.BasicGetAsync(queueName, true);
93+
Assert.Equal(message, Encoding.UTF8.GetString(result!.Body.Span));
9494
}
9595

9696
[Theory]
@@ -142,18 +142,19 @@ public async Task WithDataShouldPersistStateBetweenUsages(bool useVolume)
142142

143143
var connection = host.Services.GetRequiredService<IConnection>();
144144

145-
using var channel = connection.CreateModel();
145+
await using var channel = await connection.CreateChannelAsync();
146146
const string queueName = "hello";
147-
channel.QueueDeclare(queueName, durable: true, exclusive: false);
147+
await channel.QueueDeclareAsync(queueName, durable: true, exclusive: false);
148148

149149
const string message = "Hello World!";
150150
var body = Encoding.UTF8.GetBytes(message);
151151

152-
var props = channel.CreateBasicProperties();
152+
var props = new BasicProperties();
153153
props.Persistent = true; // or props.DeliveryMode = 2;
154-
channel.BasicPublish(
154+
await channel.BasicPublishAsync(
155155
exchange: string.Empty,
156156
queueName,
157+
mandatory: true,
157158
props,
158159
body);
159160
}
@@ -199,12 +200,12 @@ public async Task WithDataShouldPersistStateBetweenUsages(bool useVolume)
199200

200201
var connection = host.Services.GetRequiredService<IConnection>();
201202

202-
using var channel = connection.CreateModel();
203+
await using var channel = await connection.CreateChannelAsync();
203204
const string queueName = "hello";
204-
channel.QueueDeclare(queueName, durable: true, exclusive: false);
205+
await channel.QueueDeclareAsync(queueName, durable: true, exclusive: false);
205206

206-
var result = channel.BasicGet(queueName, true);
207-
Assert.Equal("Hello World!", Encoding.UTF8.GetString(result.Body.Span));
207+
var result = await channel.BasicGetAsync(queueName, true);
208+
Assert.Equal("Hello World!", Encoding.UTF8.GetString(result!.Body.Span));
208209
}
209210
}
210211
finally

tests/Aspire.RabbitMQ.Client.Tests/Aspire.RabbitMQ.Client.Tests.csproj

+2
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@
1010
<Compile Include="$(RepoRoot)src\Aspire.Hosting.RabbitMQ\RabbitMQContainerImageTags.cs" />
1111

1212
<ProjectReference Include="..\..\src\Components\Aspire.RabbitMQ.Client\Aspire.RabbitMQ.Client.csproj" />
13+
<PackageReference Include="RabbitMQ.Client" VersionOverride="[6.8.1,7.0.0)" />
14+
1315
<ProjectReference Include="..\Aspire.Components.Common.Tests\Aspire.Components.Common.Tests.csproj" />
1416

1517
<PackageReference Include="Testcontainers.RabbitMq" />

tests/Aspire.RabbitMQ.Client.v7.Tests/Aspire.RabbitMQ.Client.v7.Tests.csproj

-1
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@
1616
<ProjectReference Include="..\..\src\Components\Aspire.RabbitMQ.Client.v7\Aspire.RabbitMQ.Client.v7.csproj" />
1717
<ProjectReference Include="..\Aspire.Components.Common.Tests\Aspire.Components.Common.Tests.csproj" />
1818

19-
<PackageReference Include="RabbitMQ.Client" VersionOverride="7.0.0" />
2019
<PackageReference Include="Testcontainers.RabbitMq" />
2120
</ItemGroup>
2221

0 commit comments

Comments
 (0)