Guides & Recipes — Messaging
Verified testing and middleware recipes compiled on .NET 10 (ImplicitUsings=disable, Nullable=enable).
Recipe 1: Testing Publishing with InMemoryTestHarness
Section titled “Recipe 1: Testing Publishing with InMemoryTestHarness”using System;using System.Text;using System.Threading;using System.Threading.Tasks;using EricksonLopez.Messaging.Contracts;using EricksonLopez.Messaging.Testing;using EricksonLopez.Messaging.Transport;using EricksonLopez.Result;
namespace Example.MessagingQuickStart;
public static class HarnessRecipe{ public static async Task VerifyPublishAsync(CancellationToken ct) { var harness = new InMemoryTestHarness(); byte[] payload = Encoding.UTF8.GetBytes("{\"orderId\":\"1001\"}"); var metadata = TransportMessageMetadata.Create("orders.confirmed", correlationId: "corr-100");
Result result = await harness.PublishRawAsync("orders-topic", payload, metadata, ct); if (result.IsFailure || harness.PublishedMessages.Count != 1) { throw new InvalidOperationException("Expected one published message in harness."); } }}Recipe 2: Configuring RetryMiddleware with RetryOptions (v2.0.0)
Section titled “Recipe 2: Configuring RetryMiddleware with RetryOptions (v2.0.0)”In v2.0.0 (BC-003), construct RetryMiddleware using RetryOptions rather than the removed 3-parameter constructor:
using System;using EricksonLopez.Messaging.Middleware;
namespace Example.MessagingQuickStart;
public static class RetryRecipe{ public static RetryMiddleware CreateRetryMiddleware() { return new RetryMiddleware(new RetryOptions { MaxRetries = 3, InitialDelay = TimeSpan.FromSeconds(1), TimeProvider = TimeProvider.System, }); }}