Skip to content

Commit a0a2586

Browse files
committed
fix(messaging): give a clear startup error when no transport is configured
1 parent f5dcdcd commit a0a2586

3 files changed

Lines changed: 20 additions & 2 deletions

File tree

src/Vulthil.Messaging/ConsumerHostedService.cs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,12 @@ internal sealed class ConsumerHostedService : BackgroundService
66
{
77
private readonly ITransport _transport;
88

9-
public ConsumerHostedService(ITransport transport)
9+
public ConsumerHostedService(IEnumerable<ITransport> transports)
1010
{
11-
_transport = transport;
11+
_transport = transports.LastOrDefault()
12+
?? throw new InvalidOperationException(
13+
"No messaging transport is registered. Call a transport extension such as .UseRabbitMq(...) " +
14+
"inside AddMessaging(...) (or .UseTestHarness() in a test).");
1215
}
1316

1417
protected override Task ExecuteAsync(CancellationToken stoppingToken) => _transport.StartAsync(stoppingToken);

src/Vulthil.Messaging/DependencyInjection.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,9 @@ public static class DependencyInjection
2222
/// Code registrations performed inside the configurator action merge onto the loaded values and take precedence.
2323
/// Built-in consume filters (see <see cref="ConsumeFilterOptions"/>) are registered before the configurator
2424
/// action runs, so user-registered filters compose INSIDE the defaults.
25+
/// <para>A transport must be configured — e.g. <c>UseRabbitMq(...)</c> inside the configurator, or registered
26+
/// separately (as <c>ReplaceTransportWithTestHarness</c> does) — otherwise the host fails to start with a clear
27+
/// error.</para>
2528
/// </remarks>
2629
/// <param name="builder">The host application builder.</param>
2730
/// <param name="messagingConfiguratorAction">An action to configure messaging through <see cref="IMessagingConfigurator"/>.</param>

tests/Vulthil.Messaging.Tests/MessagingConfiguratiorTests.cs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,18 @@ public void AddMessagingShouldRegisterConsumerHostedService()
2929
hostedServices[0].Lifetime.ShouldBe(ServiceLifetime.Singleton);
3030
}
3131

32+
[Fact]
33+
public async Task StartingTheHostWithoutATransportThrowsAClearError()
34+
{
35+
// Arrange — messaging configured, but no transport (no UseRabbitMq/UseTestHarness).
36+
Target.AddMessaging(x => x.ConfigureQueue("orders", _ => { }));
37+
using var host = Target.Build();
38+
39+
// Act & Assert
40+
var exception = await Should.ThrowAsync<InvalidOperationException>(() => host.StartAsync(CancellationToken));
41+
exception.Message.ShouldContain("No messaging transport is registered");
42+
}
43+
3244
[Fact]
3345
public void AddMessagingShouldRegisterMessageConfigurationProvider()
3446
{

0 commit comments

Comments
 (0)