diff --git a/src/Mocha/src/Examples/Middleware/CustomMiddleware/CustomMiddleware.cs b/src/Mocha/src/Examples/Middleware/CustomMiddleware/CustomMiddleware.cs index 4901d25a758..f14762a49af 100644 --- a/src/Mocha/src/Examples/Middleware/CustomMiddleware/CustomMiddleware.cs +++ b/src/Mocha/src/Examples/Middleware/CustomMiddleware/CustomMiddleware.cs @@ -16,12 +16,12 @@ messageBus.ConfigureMessageBus(bus => { - // Prepend tenant dispatch middleware so every outgoing message carries the tenant header. - bus.PrependDispatch(TenantDispatchMiddleware.Create("acme")); + // Add tenant dispatch middleware so every outgoing message carries the tenant header. + bus.UseDispatch(TenantDispatchMiddleware.Create("acme")); // Insert logging receive middleware after ReceiveInstrumentation so telemetry spans are // already open when our middleware runs. - bus.AppendReceive("ReceiveInstrumentation", LoggingReceiveMiddleware.Create()); + bus.UseReceive(LoggingReceiveMiddleware.Create(), after: "ReceiveInstrumentation"); }); messageBus diff --git a/src/Mocha/src/Mocha.Inbox/InboxCoreServiceCollectionExtensions.cs b/src/Mocha/src/Mocha.Inbox/InboxCoreServiceCollectionExtensions.cs index dd0e32e1769..f632a602208 100644 --- a/src/Mocha/src/Mocha.Inbox/InboxCoreServiceCollectionExtensions.cs +++ b/src/Mocha/src/Mocha.Inbox/InboxCoreServiceCollectionExtensions.cs @@ -24,7 +24,7 @@ public static class InboxCoreServiceCollectionExtensions /// The same instance for chaining. public static IMessageBusHostBuilder UseInboxCore(this IMessageBusHostBuilder builder) { - builder.ConfigureMessageBus(x => x.AppendConsume(ConsumeInboxMiddleware.Create())); + builder.ConfigureMessageBus(x => x.UseConsume(ConsumeInboxMiddleware.Create())); return builder; } diff --git a/src/Mocha/src/Mocha.Mediator/DependencyInjection/IMediatorBuilder.cs b/src/Mocha/src/Mocha.Mediator/DependencyInjection/IMediatorBuilder.cs index 4579c1a864e..81a71c7f94d 100644 --- a/src/Mocha/src/Mocha.Mediator/DependencyInjection/IMediatorBuilder.cs +++ b/src/Mocha/src/Mocha.Mediator/DependencyInjection/IMediatorBuilder.cs @@ -15,26 +15,13 @@ public interface IMediatorBuilder IMediatorBuilder ConfigureOptions(Action configure); /// - /// Appends a middleware to the end of the pipeline. + /// Adds a middleware to the pipeline. When neither nor + /// is specified the middleware is appended to the end of the pipeline. /// - IMediatorBuilder Use(MediatorMiddlewareConfiguration middleware); - - /// - /// Inserts a middleware after the middleware identified by . - /// If no middleware with that key is found, the middleware is appended to the end. - /// - IMediatorBuilder Append(string after, MediatorMiddlewareConfiguration middleware); - - /// - /// Inserts a middleware at the beginning of the pipeline. - /// - IMediatorBuilder Prepend(MediatorMiddlewareConfiguration middleware); - - /// - /// Inserts a middleware before the middleware identified by . - /// If no middleware with that key is found, the middleware is prepended to the beginning. - /// - IMediatorBuilder Prepend(string before, MediatorMiddlewareConfiguration middleware); + /// The middleware configuration. + /// If specified, the middleware is inserted before the middleware with the given key. + /// If specified, the middleware is inserted after the middleware with the given key. + IMediatorBuilder Use(MediatorMiddlewareConfiguration middleware, string? before = null, string? after = null); /// /// Configures the mediator's feature collection. diff --git a/src/Mocha/src/Mocha.Mediator/DependencyInjection/MediatorBuilder.cs b/src/Mocha/src/Mocha.Mediator/DependencyInjection/MediatorBuilder.cs index 538d019d91e..fbe7e73f89b 100644 --- a/src/Mocha/src/Mocha.Mediator/DependencyInjection/MediatorBuilder.cs +++ b/src/Mocha/src/Mocha.Mediator/DependencyInjection/MediatorBuilder.cs @@ -33,63 +33,37 @@ public IMediatorBuilder ConfigureOptions(Action configure) } /// - public IMediatorBuilder Use(MediatorMiddlewareConfiguration middleware) + public IMediatorBuilder Use(MediatorMiddlewareConfiguration middleware, string? before = null, string? after = null) { ArgumentNullException.ThrowIfNull(middleware); - _middlewares.Add(middleware); - return this; - } - - /// - public IMediatorBuilder Append(string after, MediatorMiddlewareConfiguration middleware) - { - ArgumentNullException.ThrowIfNull(after); - ArgumentNullException.ThrowIfNull(middleware); - - _pipelineModifiers.Add(pipeline => + if (before is not null && after is not null) { - var index = pipeline.FindIndex(m => m.Key == after); - - if (index >= 0) - { - pipeline.Insert(index + 1, middleware); - } - else - { - pipeline.Add(middleware); - } - }); - return this; - } - - /// - public IMediatorBuilder Prepend(MediatorMiddlewareConfiguration middleware) - { - ArgumentNullException.ThrowIfNull(middleware); + throw new ArgumentException( + "Only one of 'before' or 'after' can be specified at the same time."); + } - _pipelineModifiers.Add(pipeline => pipeline.Insert(0, middleware)); - return this; - } + if (before is null && after is null) + { + _middlewares.Add(middleware); + return this; + } - /// - public IMediatorBuilder Prepend(string before, MediatorMiddlewareConfiguration middleware) - { - ArgumentNullException.ThrowIfNull(before); - ArgumentNullException.ThrowIfNull(middleware); + var anchor = (before ?? after)!; _pipelineModifiers.Add(pipeline => { - var index = pipeline.FindIndex(m => m.Key == before); - if (index >= 0) - { - pipeline.Insert(index, middleware); - } - else + var index = pipeline.FindIndex(m => m.Key == anchor); + + if (index == -1) { - pipeline.Insert(0, middleware); + throw new InvalidOperationException( + $"The middleware with the key `{anchor}` was not found."); } + + pipeline.Insert(before is not null ? index : index + 1, middleware); }); + return this; } diff --git a/src/Mocha/src/Mocha.Mediator/DependencyInjection/MediatorHostBuilderExtensions.cs b/src/Mocha/src/Mocha.Mediator/DependencyInjection/MediatorHostBuilderExtensions.cs index 10ec0e77e13..2441d397f6c 100644 --- a/src/Mocha/src/Mocha.Mediator/DependencyInjection/MediatorHostBuilderExtensions.cs +++ b/src/Mocha/src/Mocha.Mediator/DependencyInjection/MediatorHostBuilderExtensions.cs @@ -9,62 +9,19 @@ namespace Mocha.Mediator; public static class MediatorHostBuilderExtensions { /// - /// Appends a middleware to the end of the pipeline. + /// Adds a middleware to the pipeline. When neither nor + /// is specified the middleware is appended to the end of the pipeline. /// public static IMediatorHostBuilder Use( this IMediatorHostBuilder builder, - MediatorMiddlewareConfiguration middleware) + MediatorMiddlewareConfiguration middleware, + string? before = null, + string? after = null) { ArgumentNullException.ThrowIfNull(builder); ArgumentNullException.ThrowIfNull(middleware); - builder.ConfigureMediator(b => b.Use(middleware)); - return builder; - } - - /// - /// Inserts a middleware after the middleware identified by . - /// - public static IMediatorHostBuilder Append( - this IMediatorHostBuilder builder, - string after, - MediatorMiddlewareConfiguration middleware) - { - ArgumentNullException.ThrowIfNull(builder); - ArgumentNullException.ThrowIfNull(after); - ArgumentNullException.ThrowIfNull(middleware); - - builder.ConfigureMediator(b => b.Append(after, middleware)); - return builder; - } - - /// - /// Inserts a middleware at the beginning of the pipeline. - /// - public static IMediatorHostBuilder Prepend( - this IMediatorHostBuilder builder, - MediatorMiddlewareConfiguration middleware) - { - ArgumentNullException.ThrowIfNull(builder); - ArgumentNullException.ThrowIfNull(middleware); - - builder.ConfigureMediator(b => b.Prepend(middleware)); - return builder; - } - - /// - /// Inserts a middleware before the middleware identified by . - /// - public static IMediatorHostBuilder Prepend( - this IMediatorHostBuilder builder, - string before, - MediatorMiddlewareConfiguration middleware) - { - ArgumentNullException.ThrowIfNull(builder); - ArgumentNullException.ThrowIfNull(before); - ArgumentNullException.ThrowIfNull(middleware); - - builder.ConfigureMediator(b => b.Prepend(before, middleware)); + builder.ConfigureMediator(b => b.Use(middleware, before: before, after: after)); return builder; } diff --git a/src/Mocha/src/Mocha.Transport.InMemory/Descriptors/IInMemoryDispatchEndpointDescriptor.cs b/src/Mocha/src/Mocha.Transport.InMemory/Descriptors/IInMemoryDispatchEndpointDescriptor.cs index 69185e2bce2..ac7961d0933 100644 --- a/src/Mocha/src/Mocha.Transport.InMemory/Descriptors/IInMemoryDispatchEndpointDescriptor.cs +++ b/src/Mocha/src/Mocha.Transport.InMemory/Descriptors/IInMemoryDispatchEndpointDescriptor.cs @@ -28,13 +28,8 @@ public interface IInMemoryDispatchEndpointDescriptor new IInMemoryDispatchEndpointDescriptor Publish(); /// - new IInMemoryDispatchEndpointDescriptor UseDispatch(DispatchMiddlewareConfiguration configuration); - - /// - new IInMemoryDispatchEndpointDescriptor AppendDispatch(string after, DispatchMiddlewareConfiguration configuration); - - /// - new IInMemoryDispatchEndpointDescriptor PrependDispatch( - string before, - DispatchMiddlewareConfiguration configuration); + new IInMemoryDispatchEndpointDescriptor UseDispatch( + DispatchMiddlewareConfiguration configuration, + string? before = null, + string? after = null); } diff --git a/src/Mocha/src/Mocha.Transport.InMemory/Descriptors/IInMemoryMessagingTransportDescriptor.cs b/src/Mocha/src/Mocha.Transport.InMemory/Descriptors/IInMemoryMessagingTransportDescriptor.cs index fdc90729a2e..46a9cc3148c 100644 --- a/src/Mocha/src/Mocha.Transport.InMemory/Descriptors/IInMemoryMessagingTransportDescriptor.cs +++ b/src/Mocha/src/Mocha.Transport.InMemory/Descriptors/IInMemoryMessagingTransportDescriptor.cs @@ -64,26 +64,14 @@ public interface IInMemoryMessagingTransportDescriptor : IMessagingTransportDesc new IInMemoryMessagingTransportDescriptor IsDefaultTransport(); /// - new IInMemoryMessagingTransportDescriptor UseDispatch(DispatchMiddlewareConfiguration configuration); + new IInMemoryMessagingTransportDescriptor UseDispatch( + DispatchMiddlewareConfiguration configuration, + string? before = null, + string? after = null); /// - new IInMemoryMessagingTransportDescriptor AppendDispatch( - string after, - DispatchMiddlewareConfiguration configuration); - - /// - new IInMemoryMessagingTransportDescriptor PrependDispatch( - string before, - DispatchMiddlewareConfiguration configuration); - - /// - new IInMemoryMessagingTransportDescriptor UseReceive(ReceiveMiddlewareConfiguration configuration); - - /// - new IInMemoryMessagingTransportDescriptor AppendReceive(string after, ReceiveMiddlewareConfiguration configuration); - - /// - new IInMemoryMessagingTransportDescriptor PrependReceive( - string before, - ReceiveMiddlewareConfiguration configuration); + new IInMemoryMessagingTransportDescriptor UseReceive( + ReceiveMiddlewareConfiguration configuration, + string? before = null, + string? after = null); } diff --git a/src/Mocha/src/Mocha.Transport.InMemory/Descriptors/IInMemoryReceiveEndpointDescriptor.cs b/src/Mocha/src/Mocha.Transport.InMemory/Descriptors/IInMemoryReceiveEndpointDescriptor.cs index 0423e3b1e00..0029c7d9674 100644 --- a/src/Mocha/src/Mocha.Transport.InMemory/Descriptors/IInMemoryReceiveEndpointDescriptor.cs +++ b/src/Mocha/src/Mocha.Transport.InMemory/Descriptors/IInMemoryReceiveEndpointDescriptor.cs @@ -32,11 +32,8 @@ public interface IInMemoryReceiveEndpointDescriptor : IReceiveEndpointDescriptor IInMemoryReceiveEndpointDescriptor Queue(string name); /// - new IInMemoryReceiveEndpointDescriptor UseReceive(ReceiveMiddlewareConfiguration configuration); - - /// - new IInMemoryReceiveEndpointDescriptor AppendReceive(string after, ReceiveMiddlewareConfiguration configuration); - - /// - new IInMemoryReceiveEndpointDescriptor PrependReceive(string before, ReceiveMiddlewareConfiguration configuration); + new IInMemoryReceiveEndpointDescriptor UseReceive( + ReceiveMiddlewareConfiguration configuration, + string? before = null, + string? after = null); } diff --git a/src/Mocha/src/Mocha.Transport.InMemory/Descriptors/InMemoryDispatchEndpointDescriptor.cs b/src/Mocha/src/Mocha.Transport.InMemory/Descriptors/InMemoryDispatchEndpointDescriptor.cs index 0f89ef056ee..f90cf40d96d 100644 --- a/src/Mocha/src/Mocha.Transport.InMemory/Descriptors/InMemoryDispatchEndpointDescriptor.cs +++ b/src/Mocha/src/Mocha.Transport.InMemory/Descriptors/InMemoryDispatchEndpointDescriptor.cs @@ -37,25 +37,12 @@ public IInMemoryDispatchEndpointDescriptor ToTopic(string name) return this; } - public new IInMemoryDispatchEndpointDescriptor UseDispatch(DispatchMiddlewareConfiguration configuration) + public new IInMemoryDispatchEndpointDescriptor UseDispatch( + DispatchMiddlewareConfiguration configuration, + string? before = null, + string? after = null) { - base.UseDispatch(configuration); - return this; - } - - public new IInMemoryDispatchEndpointDescriptor AppendDispatch( - string after, - DispatchMiddlewareConfiguration configuration) - { - base.AppendDispatch(after, configuration); - return this; - } - - public new IInMemoryDispatchEndpointDescriptor PrependDispatch( - string before, - DispatchMiddlewareConfiguration configuration) - { - base.PrependDispatch(before, configuration); + base.UseDispatch(configuration, before: before, after: after); return this; } diff --git a/src/Mocha/src/Mocha.Transport.InMemory/Descriptors/InMemoryMessagingTransportDescriptor.cs b/src/Mocha/src/Mocha.Transport.InMemory/Descriptors/InMemoryMessagingTransportDescriptor.cs index 6fd2da02963..59e700ca46d 100644 --- a/src/Mocha/src/Mocha.Transport.InMemory/Descriptors/InMemoryMessagingTransportDescriptor.cs +++ b/src/Mocha/src/Mocha.Transport.InMemory/Descriptors/InMemoryMessagingTransportDescriptor.cs @@ -63,57 +63,23 @@ public InMemoryMessagingTransportDescriptor(IMessagingSetupContext discoveryCont } /// - public new IInMemoryMessagingTransportDescriptor UseDispatch(DispatchMiddlewareConfiguration configuration) + public new IInMemoryMessagingTransportDescriptor UseDispatch( + DispatchMiddlewareConfiguration configuration, + string? before = null, + string? after = null) { - base.UseDispatch(configuration); + base.UseDispatch(configuration, before: before, after: after); return this; } /// - public new IInMemoryMessagingTransportDescriptor AppendDispatch( - string after, - DispatchMiddlewareConfiguration configuration) + public new IInMemoryMessagingTransportDescriptor UseReceive( + ReceiveMiddlewareConfiguration configuration, + string? before = null, + string? after = null) { - base.AppendDispatch(after, configuration); - - return this; - } - - /// - public new IInMemoryMessagingTransportDescriptor PrependDispatch( - string before, - DispatchMiddlewareConfiguration configuration) - { - base.PrependDispatch(before, configuration); - - return this; - } - - /// - public new IInMemoryMessagingTransportDescriptor UseReceive(ReceiveMiddlewareConfiguration configuration) - { - base.UseReceive(configuration); - - return this; - } - - /// - public new IInMemoryMessagingTransportDescriptor AppendReceive( - string after, - ReceiveMiddlewareConfiguration configuration) - { - base.AppendReceive(after, configuration); - - return this; - } - - /// - public new IInMemoryMessagingTransportDescriptor PrependReceive( - string before, - ReceiveMiddlewareConfiguration configuration) - { - base.PrependReceive(before, configuration); + base.UseReceive(configuration, before: before, after: after); return this; } diff --git a/src/Mocha/src/Mocha.Transport.InMemory/Descriptors/InMemoryReceiveEndpointDescriptor.cs b/src/Mocha/src/Mocha.Transport.InMemory/Descriptors/InMemoryReceiveEndpointDescriptor.cs index 3c2bb2962de..7e96eee090d 100644 --- a/src/Mocha/src/Mocha.Transport.InMemory/Descriptors/InMemoryReceiveEndpointDescriptor.cs +++ b/src/Mocha/src/Mocha.Transport.InMemory/Descriptors/InMemoryReceiveEndpointDescriptor.cs @@ -59,27 +59,12 @@ public IInMemoryReceiveEndpointDescriptor Queue(string name) return this; } - public new IInMemoryReceiveEndpointDescriptor UseReceive(ReceiveMiddlewareConfiguration configuration) + public new IInMemoryReceiveEndpointDescriptor UseReceive( + ReceiveMiddlewareConfiguration configuration, + string? before = null, + string? after = null) { - base.UseReceive(configuration); - - return this; - } - - public new IInMemoryReceiveEndpointDescriptor AppendReceive( - string after, - ReceiveMiddlewareConfiguration configuration) - { - base.AppendReceive(after, configuration); - - return this; - } - - public new IInMemoryReceiveEndpointDescriptor PrependReceive( - string before, - ReceiveMiddlewareConfiguration configuration) - { - base.PrependReceive(before, configuration); + base.UseReceive(configuration, before: before, after: after); return this; } diff --git a/src/Mocha/src/Mocha.Transport.Postgres/Descriptors/IPostgresDispatchEndpointDescriptor.cs b/src/Mocha/src/Mocha.Transport.Postgres/Descriptors/IPostgresDispatchEndpointDescriptor.cs index df738dc09a2..843e7b94258 100644 --- a/src/Mocha/src/Mocha.Transport.Postgres/Descriptors/IPostgresDispatchEndpointDescriptor.cs +++ b/src/Mocha/src/Mocha.Transport.Postgres/Descriptors/IPostgresDispatchEndpointDescriptor.cs @@ -27,14 +27,9 @@ public interface IPostgresDispatchEndpointDescriptor /// new IPostgresDispatchEndpointDescriptor Publish(); - /// - new IPostgresDispatchEndpointDescriptor UseDispatch(DispatchMiddlewareConfiguration configuration); - - /// - new IPostgresDispatchEndpointDescriptor AppendDispatch(string after, DispatchMiddlewareConfiguration configuration); - - /// - new IPostgresDispatchEndpointDescriptor PrependDispatch( - string before, - DispatchMiddlewareConfiguration configuration); + /// + new IPostgresDispatchEndpointDescriptor UseDispatch( + DispatchMiddlewareConfiguration configuration, + string? before = null, + string? after = null); } diff --git a/src/Mocha/src/Mocha.Transport.Postgres/Descriptors/IPostgresMessagingTransportDescriptor.cs b/src/Mocha/src/Mocha.Transport.Postgres/Descriptors/IPostgresMessagingTransportDescriptor.cs index de55cdc6bfb..4da7bbf330f 100644 --- a/src/Mocha/src/Mocha.Transport.Postgres/Descriptors/IPostgresMessagingTransportDescriptor.cs +++ b/src/Mocha/src/Mocha.Transport.Postgres/Descriptors/IPostgresMessagingTransportDescriptor.cs @@ -90,27 +90,15 @@ public interface IPostgresMessagingTransportDescriptor /// new IPostgresMessagingTransportDescriptor IsDefaultTransport(); - /// - new IPostgresMessagingTransportDescriptor UseDispatch(DispatchMiddlewareConfiguration configuration); - - /// - new IPostgresMessagingTransportDescriptor AppendDispatch( - string after, - DispatchMiddlewareConfiguration configuration); - - /// - new IPostgresMessagingTransportDescriptor PrependDispatch( - string before, - DispatchMiddlewareConfiguration configuration); - - /// - new IPostgresMessagingTransportDescriptor UseReceive(ReceiveMiddlewareConfiguration configuration); - - /// - new IPostgresMessagingTransportDescriptor AppendReceive(string after, ReceiveMiddlewareConfiguration configuration); - - /// - new IPostgresMessagingTransportDescriptor PrependReceive( - string before, - ReceiveMiddlewareConfiguration configuration); + /// + new IPostgresMessagingTransportDescriptor UseDispatch( + DispatchMiddlewareConfiguration configuration, + string? before = null, + string? after = null); + + /// + new IPostgresMessagingTransportDescriptor UseReceive( + ReceiveMiddlewareConfiguration configuration, + string? before = null, + string? after = null); } diff --git a/src/Mocha/src/Mocha.Transport.Postgres/Descriptors/IPostgresReceiveEndpointDescriptor.cs b/src/Mocha/src/Mocha.Transport.Postgres/Descriptors/IPostgresReceiveEndpointDescriptor.cs index 3867d470b0c..72e4a9285c9 100644 --- a/src/Mocha/src/Mocha.Transport.Postgres/Descriptors/IPostgresReceiveEndpointDescriptor.cs +++ b/src/Mocha/src/Mocha.Transport.Postgres/Descriptors/IPostgresReceiveEndpointDescriptor.cs @@ -38,12 +38,9 @@ public interface IPostgresReceiveEndpointDescriptor : IReceiveEndpointDescriptor /// The descriptor for method chaining. IPostgresReceiveEndpointDescriptor MaxBatchSize(int size); - /// - new IPostgresReceiveEndpointDescriptor UseReceive(ReceiveMiddlewareConfiguration configuration); - - /// - new IPostgresReceiveEndpointDescriptor AppendReceive(string after, ReceiveMiddlewareConfiguration configuration); - - /// - new IPostgresReceiveEndpointDescriptor PrependReceive(string before, ReceiveMiddlewareConfiguration configuration); + /// + new IPostgresReceiveEndpointDescriptor UseReceive( + ReceiveMiddlewareConfiguration configuration, + string? before = null, + string? after = null); } diff --git a/src/Mocha/src/Mocha.Transport.Postgres/Descriptors/PostgresDispatchEndpointDescriptor.cs b/src/Mocha/src/Mocha.Transport.Postgres/Descriptors/PostgresDispatchEndpointDescriptor.cs index 07046f265de..a19ddc3efd9 100644 --- a/src/Mocha/src/Mocha.Transport.Postgres/Descriptors/PostgresDispatchEndpointDescriptor.cs +++ b/src/Mocha/src/Mocha.Transport.Postgres/Descriptors/PostgresDispatchEndpointDescriptor.cs @@ -43,27 +43,12 @@ public IPostgresDispatchEndpointDescriptor ToTopic(string name) } /// > - public new IPostgresDispatchEndpointDescriptor UseDispatch(DispatchMiddlewareConfiguration configuration) + public new IPostgresDispatchEndpointDescriptor UseDispatch( + DispatchMiddlewareConfiguration configuration, + string? before = null, + string? after = null) { - base.UseDispatch(configuration); - return this; - } - - /// > - public new IPostgresDispatchEndpointDescriptor AppendDispatch( - string after, - DispatchMiddlewareConfiguration configuration) - { - base.AppendDispatch(after, configuration); - return this; - } - - /// > - public new IPostgresDispatchEndpointDescriptor PrependDispatch( - string before, - DispatchMiddlewareConfiguration configuration) - { - base.PrependDispatch(before, configuration); + base.UseDispatch(configuration, before: before, after: after); return this; } diff --git a/src/Mocha/src/Mocha.Transport.Postgres/Descriptors/PostgresMessagingTransportDescriptor.cs b/src/Mocha/src/Mocha.Transport.Postgres/Descriptors/PostgresMessagingTransportDescriptor.cs index 67f6b450133..afe325388d8 100644 --- a/src/Mocha/src/Mocha.Transport.Postgres/Descriptors/PostgresMessagingTransportDescriptor.cs +++ b/src/Mocha/src/Mocha.Transport.Postgres/Descriptors/PostgresMessagingTransportDescriptor.cs @@ -63,57 +63,23 @@ public PostgresMessagingTransportDescriptor(IMessagingSetupContext discoveryCont } /// - public new IPostgresMessagingTransportDescriptor UseDispatch(DispatchMiddlewareConfiguration configuration) + public new IPostgresMessagingTransportDescriptor UseDispatch( + DispatchMiddlewareConfiguration configuration, + string? before = null, + string? after = null) { - base.UseDispatch(configuration); + base.UseDispatch(configuration, before: before, after: after); return this; } /// - public new IPostgresMessagingTransportDescriptor AppendDispatch( - string after, - DispatchMiddlewareConfiguration configuration) + public new IPostgresMessagingTransportDescriptor UseReceive( + ReceiveMiddlewareConfiguration configuration, + string? before = null, + string? after = null) { - base.AppendDispatch(after, configuration); - - return this; - } - - /// - public new IPostgresMessagingTransportDescriptor PrependDispatch( - string before, - DispatchMiddlewareConfiguration configuration) - { - base.PrependDispatch(before, configuration); - - return this; - } - - /// - public new IPostgresMessagingTransportDescriptor UseReceive(ReceiveMiddlewareConfiguration configuration) - { - base.UseReceive(configuration); - - return this; - } - - /// - public new IPostgresMessagingTransportDescriptor AppendReceive( - string after, - ReceiveMiddlewareConfiguration configuration) - { - base.AppendReceive(after, configuration); - - return this; - } - - /// - public new IPostgresMessagingTransportDescriptor PrependReceive( - string before, - ReceiveMiddlewareConfiguration configuration) - { - base.PrependReceive(before, configuration); + base.UseReceive(configuration, before: before, after: after); return this; } diff --git a/src/Mocha/src/Mocha.Transport.Postgres/Descriptors/PostgresReceiveEndpointDescriptor.cs b/src/Mocha/src/Mocha.Transport.Postgres/Descriptors/PostgresReceiveEndpointDescriptor.cs index e584411501c..8470ff48304 100644 --- a/src/Mocha/src/Mocha.Transport.Postgres/Descriptors/PostgresReceiveEndpointDescriptor.cs +++ b/src/Mocha/src/Mocha.Transport.Postgres/Descriptors/PostgresReceiveEndpointDescriptor.cs @@ -75,29 +75,12 @@ public IPostgresReceiveEndpointDescriptor MaxBatchSize(int size) } /// - public new IPostgresReceiveEndpointDescriptor UseReceive(ReceiveMiddlewareConfiguration configuration) + public new IPostgresReceiveEndpointDescriptor UseReceive( + ReceiveMiddlewareConfiguration configuration, + string? before = null, + string? after = null) { - base.UseReceive(configuration); - - return this; - } - - /// - public new IPostgresReceiveEndpointDescriptor AppendReceive( - string after, - ReceiveMiddlewareConfiguration configuration) - { - base.AppendReceive(after, configuration); - - return this; - } - - /// - public new IPostgresReceiveEndpointDescriptor PrependReceive( - string before, - ReceiveMiddlewareConfiguration configuration) - { - base.PrependReceive(before, configuration); + base.UseReceive(configuration, before: before, after: after); return this; } diff --git a/src/Mocha/src/Mocha.Transport.Postgres/Topology/Extensions/PostgresTransportDescriptorExtensions.cs b/src/Mocha/src/Mocha.Transport.Postgres/Topology/Extensions/PostgresTransportDescriptorExtensions.cs index 9387a533e25..b45f8e00bac 100644 --- a/src/Mocha/src/Mocha.Transport.Postgres/Topology/Extensions/PostgresTransportDescriptorExtensions.cs +++ b/src/Mocha/src/Mocha.Transport.Postgres/Topology/Extensions/PostgresTransportDescriptorExtensions.cs @@ -18,7 +18,7 @@ internal static IPostgresMessagingTransportDescriptor AddDefaults( descriptor.AddConvention(new PostgresReceiveEndpointTopologyConvention()); descriptor.AddConvention(new PostgresDispatchEndpointTopologyConvention()); - descriptor.AppendReceive(ReceiveMiddlewares.ConcurrencyLimiter.Key, PostgresReceiveMiddlewares.Parsing); + descriptor.UseReceive(PostgresReceiveMiddlewares.Parsing, after: ReceiveMiddlewares.ConcurrencyLimiter.Key); return descriptor; } diff --git a/src/Mocha/src/Mocha.Transport.RabbitMQ/Descriptors/IRabbitMQDispatchEndpointDescriptor.cs b/src/Mocha/src/Mocha.Transport.RabbitMQ/Descriptors/IRabbitMQDispatchEndpointDescriptor.cs index 8e86bd7cc70..e8da9ba6c52 100644 --- a/src/Mocha/src/Mocha.Transport.RabbitMQ/Descriptors/IRabbitMQDispatchEndpointDescriptor.cs +++ b/src/Mocha/src/Mocha.Transport.RabbitMQ/Descriptors/IRabbitMQDispatchEndpointDescriptor.cs @@ -27,13 +27,8 @@ public interface IRabbitMQDispatchEndpointDescriptor new IRabbitMQDispatchEndpointDescriptor Publish(); /// - new IRabbitMQDispatchEndpointDescriptor UseDispatch(DispatchMiddlewareConfiguration configuration); - - /// - new IRabbitMQDispatchEndpointDescriptor AppendDispatch(string after, DispatchMiddlewareConfiguration configuration); - - /// - new IRabbitMQDispatchEndpointDescriptor PrependDispatch( - string before, - DispatchMiddlewareConfiguration configuration); + new IRabbitMQDispatchEndpointDescriptor UseDispatch( + DispatchMiddlewareConfiguration configuration, + string? before = null, + string? after = null); } diff --git a/src/Mocha/src/Mocha.Transport.RabbitMQ/Descriptors/IRabbitMQMessagingTransportDescriptor.cs b/src/Mocha/src/Mocha.Transport.RabbitMQ/Descriptors/IRabbitMQMessagingTransportDescriptor.cs index 7077a05e64e..565d540afdf 100644 --- a/src/Mocha/src/Mocha.Transport.RabbitMQ/Descriptors/IRabbitMQMessagingTransportDescriptor.cs +++ b/src/Mocha/src/Mocha.Transport.RabbitMQ/Descriptors/IRabbitMQMessagingTransportDescriptor.cs @@ -91,26 +91,14 @@ IRabbitMQMessagingTransportDescriptor ConnectionProvider( new IRabbitMQMessagingTransportDescriptor IsDefaultTransport(); /// - new IRabbitMQMessagingTransportDescriptor UseDispatch(DispatchMiddlewareConfiguration configuration); - - /// - new IRabbitMQMessagingTransportDescriptor AppendDispatch( - string after, - DispatchMiddlewareConfiguration configuration); - - /// - new IRabbitMQMessagingTransportDescriptor PrependDispatch( - string before, - DispatchMiddlewareConfiguration configuration); + new IRabbitMQMessagingTransportDescriptor UseDispatch( + DispatchMiddlewareConfiguration configuration, + string? before = null, + string? after = null); /// - new IRabbitMQMessagingTransportDescriptor UseReceive(ReceiveMiddlewareConfiguration configuration); - - /// - new IRabbitMQMessagingTransportDescriptor AppendReceive(string after, ReceiveMiddlewareConfiguration configuration); - - /// - new IRabbitMQMessagingTransportDescriptor PrependReceive( - string before, - ReceiveMiddlewareConfiguration configuration); + new IRabbitMQMessagingTransportDescriptor UseReceive( + ReceiveMiddlewareConfiguration configuration, + string? before = null, + string? after = null); } diff --git a/src/Mocha/src/Mocha.Transport.RabbitMQ/Descriptors/IRabbitMQReceiveEndpointDescriptor.cs b/src/Mocha/src/Mocha.Transport.RabbitMQ/Descriptors/IRabbitMQReceiveEndpointDescriptor.cs index de15baace29..f5081a8e208 100644 --- a/src/Mocha/src/Mocha.Transport.RabbitMQ/Descriptors/IRabbitMQReceiveEndpointDescriptor.cs +++ b/src/Mocha/src/Mocha.Transport.RabbitMQ/Descriptors/IRabbitMQReceiveEndpointDescriptor.cs @@ -41,11 +41,8 @@ public interface IRabbitMQReceiveEndpointDescriptor : IReceiveEndpointDescriptor IRabbitMQReceiveEndpointDescriptor MaxPrefetch(ushort maxPrefetch); /// - new IRabbitMQReceiveEndpointDescriptor UseReceive(ReceiveMiddlewareConfiguration configuration); - - /// - new IRabbitMQReceiveEndpointDescriptor AppendReceive(string after, ReceiveMiddlewareConfiguration configuration); - - /// - new IRabbitMQReceiveEndpointDescriptor PrependReceive(string before, ReceiveMiddlewareConfiguration configuration); + new IRabbitMQReceiveEndpointDescriptor UseReceive( + ReceiveMiddlewareConfiguration configuration, + string? before = null, + string? after = null); } diff --git a/src/Mocha/src/Mocha.Transport.RabbitMQ/Descriptors/RabbitMQDispatchEndpointDescriptor.cs b/src/Mocha/src/Mocha.Transport.RabbitMQ/Descriptors/RabbitMQDispatchEndpointDescriptor.cs index 31a569a947b..7d738f40d88 100644 --- a/src/Mocha/src/Mocha.Transport.RabbitMQ/Descriptors/RabbitMQDispatchEndpointDescriptor.cs +++ b/src/Mocha/src/Mocha.Transport.RabbitMQ/Descriptors/RabbitMQDispatchEndpointDescriptor.cs @@ -45,27 +45,12 @@ public IRabbitMQDispatchEndpointDescriptor ToExchange(string name) } /// - public new IRabbitMQDispatchEndpointDescriptor UseDispatch(DispatchMiddlewareConfiguration configuration) + public new IRabbitMQDispatchEndpointDescriptor UseDispatch( + DispatchMiddlewareConfiguration configuration, + string? before = null, + string? after = null) { - base.UseDispatch(configuration); - return this; - } - - /// - public new IRabbitMQDispatchEndpointDescriptor AppendDispatch( - string after, - DispatchMiddlewareConfiguration configuration) - { - base.AppendDispatch(after, configuration); - return this; - } - - /// - public new IRabbitMQDispatchEndpointDescriptor PrependDispatch( - string before, - DispatchMiddlewareConfiguration configuration) - { - base.PrependDispatch(before, configuration); + base.UseDispatch(configuration, before, after); return this; } diff --git a/src/Mocha/src/Mocha.Transport.RabbitMQ/Descriptors/RabbitMQMessagingTransportDescriptor.cs b/src/Mocha/src/Mocha.Transport.RabbitMQ/Descriptors/RabbitMQMessagingTransportDescriptor.cs index 6bacc61c643..31531099116 100644 --- a/src/Mocha/src/Mocha.Transport.RabbitMQ/Descriptors/RabbitMQMessagingTransportDescriptor.cs +++ b/src/Mocha/src/Mocha.Transport.RabbitMQ/Descriptors/RabbitMQMessagingTransportDescriptor.cs @@ -57,57 +57,23 @@ public RabbitMQMessagingTransportDescriptor(IMessagingSetupContext discoveryCont } /// - public new IRabbitMQMessagingTransportDescriptor UseDispatch(DispatchMiddlewareConfiguration configuration) + public new IRabbitMQMessagingTransportDescriptor UseDispatch( + DispatchMiddlewareConfiguration configuration, + string? before = null, + string? after = null) { - base.UseDispatch(configuration); + base.UseDispatch(configuration, before, after); return this; } /// - public new IRabbitMQMessagingTransportDescriptor AppendDispatch( - string after, - DispatchMiddlewareConfiguration configuration) + public new IRabbitMQMessagingTransportDescriptor UseReceive( + ReceiveMiddlewareConfiguration configuration, + string? before = null, + string? after = null) { - base.AppendDispatch(after, configuration); - - return this; - } - - /// - public new IRabbitMQMessagingTransportDescriptor PrependDispatch( - string before, - DispatchMiddlewareConfiguration configuration) - { - base.PrependDispatch(before, configuration); - - return this; - } - - /// - public new IRabbitMQMessagingTransportDescriptor UseReceive(ReceiveMiddlewareConfiguration configuration) - { - base.UseReceive(configuration); - - return this; - } - - /// - public new IRabbitMQMessagingTransportDescriptor AppendReceive( - string after, - ReceiveMiddlewareConfiguration configuration) - { - base.AppendReceive(after, configuration); - - return this; - } - - /// - public new IRabbitMQMessagingTransportDescriptor PrependReceive( - string before, - ReceiveMiddlewareConfiguration configuration) - { - base.PrependReceive(before, configuration); + base.UseReceive(configuration, before, after); return this; } diff --git a/src/Mocha/src/Mocha.Transport.RabbitMQ/Descriptors/RabbitMQReceiveEndpointDescriptor.cs b/src/Mocha/src/Mocha.Transport.RabbitMQ/Descriptors/RabbitMQReceiveEndpointDescriptor.cs index 13d77635e2d..b6fb9fd5555 100644 --- a/src/Mocha/src/Mocha.Transport.RabbitMQ/Descriptors/RabbitMQReceiveEndpointDescriptor.cs +++ b/src/Mocha/src/Mocha.Transport.RabbitMQ/Descriptors/RabbitMQReceiveEndpointDescriptor.cs @@ -77,29 +77,12 @@ public IRabbitMQReceiveEndpointDescriptor MaxPrefetch(ushort maxPrefetch) } /// - public new IRabbitMQReceiveEndpointDescriptor UseReceive(ReceiveMiddlewareConfiguration configuration) + public new IRabbitMQReceiveEndpointDescriptor UseReceive( + ReceiveMiddlewareConfiguration configuration, + string? before = null, + string? after = null) { - base.UseReceive(configuration); - - return this; - } - - /// - public new IRabbitMQReceiveEndpointDescriptor AppendReceive( - string after, - ReceiveMiddlewareConfiguration configuration) - { - base.AppendReceive(after, configuration); - - return this; - } - - /// - public new IRabbitMQReceiveEndpointDescriptor PrependReceive( - string before, - ReceiveMiddlewareConfiguration configuration) - { - base.PrependReceive(before, configuration); + base.UseReceive(configuration, before, after); return this; } diff --git a/src/Mocha/src/Mocha.Transport.RabbitMQ/Topology/Extensions/RabbitMQTransportDescriptorExtensions.cs b/src/Mocha/src/Mocha.Transport.RabbitMQ/Topology/Extensions/RabbitMQTransportDescriptorExtensions.cs index 02e4ca2a654..9ef4a7a1c24 100644 --- a/src/Mocha/src/Mocha.Transport.RabbitMQ/Topology/Extensions/RabbitMQTransportDescriptorExtensions.cs +++ b/src/Mocha/src/Mocha.Transport.RabbitMQ/Topology/Extensions/RabbitMQTransportDescriptorExtensions.cs @@ -14,8 +14,10 @@ internal static IRabbitMQMessagingTransportDescriptor AddDefaults( descriptor.AddConvention(new RabbitMQReceiveEndpointTopologyConvention()); descriptor.AddConvention(new RabbitMQDispatchEndpointTopologyConvention()); - descriptor.AppendReceive(ReceiveMiddlewares.ConcurrencyLimiter.Key, RabbitMQReceiveMiddlewares.Acknowledgement); - descriptor.AppendReceive(RabbitMQReceiveMiddlewares.Acknowledgement.Key, RabbitMQReceiveMiddlewares.Parsing); + descriptor + .UseReceive(RabbitMQReceiveMiddlewares.Acknowledgement, after: ReceiveMiddlewares.ConcurrencyLimiter.Key); + descriptor + .UseReceive(RabbitMQReceiveMiddlewares.Parsing, after: RabbitMQReceiveMiddlewares.Acknowledgement.Key); return descriptor; } diff --git a/src/Mocha/src/Mocha/Abstractions/IMessageBusBuilder.cs b/src/Mocha/src/Mocha/Abstractions/IMessageBusBuilder.cs index 8fc969b1259..a98de65d3f6 100644 --- a/src/Mocha/src/Mocha/Abstractions/IMessageBusBuilder.cs +++ b/src/Mocha/src/Mocha/Abstractions/IMessageBusBuilder.cs @@ -102,122 +102,83 @@ IMessageBusBuilder AddBatchHandler(Action? configure = n MessagingRuntime Build(IServiceProvider services); /// - /// Appends a dispatch middleware configuration to the end of the dispatch pipeline. + /// Adds a dispatch middleware configuration to the dispatch pipeline. + /// When neither nor is specified, the + /// middleware is appended to the end of the pipeline. + /// When is specified, the middleware is inserted immediately before + /// the middleware with the given key. + /// When is specified, the middleware is inserted immediately after + /// the middleware with the given key. /// /// The dispatch middleware configuration to add. + /// + /// The key of the existing middleware before which to insert, or null to skip + /// positional insertion. + /// + /// + /// The key of the existing middleware after which to insert, or null to skip + /// positional insertion. + /// /// The builder instance for method chaining. - IMessageBusBuilder UseDispatch(DispatchMiddlewareConfiguration configuration); - - /// - /// Inserts a dispatch middleware configuration immediately after the middleware with the - /// specified name. - /// - /// The name of the existing middleware after which to insert. - /// The dispatch middleware configuration to insert. - /// The builder instance for method chaining. - IMessageBusBuilder AppendDispatch(string after, DispatchMiddlewareConfiguration configuration); - - /// - /// Inserts a dispatch middleware configuration immediately before the middleware with the - /// specified name. - /// - /// The name of the existing middleware before which to insert. - /// The dispatch middleware configuration to insert. - /// The builder instance for method chaining. - IMessageBusBuilder PrependDispatch(string before, DispatchMiddlewareConfiguration configuration); - - /// - /// Appends a dispatch middleware configuration to the end of the current dispatch pipeline. - /// - /// The dispatch middleware configuration to append. - /// The builder instance for method chaining. - IMessageBusBuilder AppendDispatch(DispatchMiddlewareConfiguration configuration); - - /// - /// Prepends a dispatch middleware configuration to the beginning of the current dispatch - /// pipeline. - /// - /// The dispatch middleware configuration to prepend. - /// The builder instance for method chaining. - IMessageBusBuilder PrependDispatch(DispatchMiddlewareConfiguration configuration); + /// + /// Thrown when both and are specified. + /// + IMessageBusBuilder UseDispatch( + DispatchMiddlewareConfiguration configuration, + string? before = null, + string? after = null); /// - /// Appends a receive middleware configuration to the end of the receive pipeline. + /// Adds a receive middleware configuration to the receive pipeline. + /// When neither nor is specified, the + /// middleware is appended to the end of the pipeline. + /// When is specified, the middleware is inserted immediately before + /// the middleware with the given key. + /// When is specified, the middleware is inserted immediately after + /// the middleware with the given key. /// /// The receive middleware configuration to add. + /// + /// The key of the existing middleware before which to insert, or null to skip + /// positional insertion. + /// + /// + /// The key of the existing middleware after which to insert, or null to skip + /// positional insertion. + /// /// The builder instance for method chaining. - IMessageBusBuilder UseReceive(ReceiveMiddlewareConfiguration configuration); - - /// - /// Inserts a receive middleware configuration immediately after the middleware with the - /// specified name. - /// - /// The name of the existing middleware after which to insert. - /// The receive middleware configuration to insert. - /// The builder instance for method chaining. - IMessageBusBuilder AppendReceive(string after, ReceiveMiddlewareConfiguration configuration); - - /// - /// Inserts a receive middleware configuration immediately before the middleware with the - /// specified name. - /// - /// The name of the existing middleware before which to insert. - /// The receive middleware configuration to insert. - /// The builder instance for method chaining. - IMessageBusBuilder PrependReceive(string before, ReceiveMiddlewareConfiguration configuration); - - /// - /// Appends a receive middleware configuration to the end of the current receive pipeline. - /// - /// The receive middleware configuration to append. - /// The builder instance for method chaining. - IMessageBusBuilder AppendReceive(ReceiveMiddlewareConfiguration configuration); - - /// - /// Prepends a receive middleware configuration to the beginning of the current receive - /// pipeline. - /// - /// The receive middleware configuration to prepend. - /// The builder instance for method chaining. - IMessageBusBuilder PrependReceive(ReceiveMiddlewareConfiguration configuration); + /// + /// Thrown when both and are specified. + /// + IMessageBusBuilder UseReceive( + ReceiveMiddlewareConfiguration configuration, + string? before = null, + string? after = null); /// - /// Appends a consumer middleware configuration to the end of the consumer pipeline. + /// Adds a consumer middleware configuration to the consumer pipeline. + /// When neither nor is specified, the + /// middleware is appended to the end of the pipeline. + /// When is specified, the middleware is inserted immediately before + /// the middleware with the given key. + /// When is specified, the middleware is inserted immediately after + /// the middleware with the given key. /// /// The consumer middleware configuration to add. + /// + /// The key of the existing middleware before which to insert, or null to skip + /// positional insertion. + /// + /// + /// The key of the existing middleware after which to insert, or null to skip + /// positional insertion. + /// /// The builder instance for method chaining. - IMessageBusBuilder UseConsume(ConsumerMiddlewareConfiguration configuration); - - /// - /// Inserts a consumer middleware configuration immediately after the middleware with the - /// specified name. - /// - /// The name of the existing middleware after which to insert. - /// The consumer middleware configuration to insert. - /// The builder instance for method chaining. - IMessageBusBuilder AppendConsume(string after, ConsumerMiddlewareConfiguration configuration); - - /// - /// Inserts a consumer middleware configuration immediately before the middleware with the - /// specified name. - /// - /// The name of the existing middleware before which to insert. - /// The consumer middleware configuration to insert. - /// The builder instance for method chaining. - IMessageBusBuilder PrependConsume(string before, ConsumerMiddlewareConfiguration configuration); - - /// - /// Appends a consumer middleware configuration to the end of the current consumer pipeline. - /// - /// The consumer middleware configuration to append. - /// The builder instance for method chaining. - IMessageBusBuilder AppendConsume(ConsumerMiddlewareConfiguration configuration); - - /// - /// Prepends a consumer middleware configuration to the beginning of the current consumer - /// pipeline. - /// - /// The consumer middleware configuration to prepend. - /// The builder instance for method chaining. - IMessageBusBuilder PrependConsume(ConsumerMiddlewareConfiguration configuration); + /// + /// Thrown when both and are specified. + /// + IMessageBusBuilder UseConsume( + ConsumerMiddlewareConfiguration configuration, + string? before = null, + string? after = null); } diff --git a/src/Mocha/src/Mocha/Builder/MessageBusBuilder.Middlewares.cs b/src/Mocha/src/Mocha/Builder/MessageBusBuilder.Middlewares.cs index 1dfef3a91e8..d7db6eb8865 100644 --- a/src/Mocha/src/Mocha/Builder/MessageBusBuilder.Middlewares.cs +++ b/src/Mocha/src/Mocha/Builder/MessageBusBuilder.Middlewares.cs @@ -12,192 +12,153 @@ public partial class MessageBusBuilder /// /// Adds a consumer middleware to the bus-level consume pipeline, applied to all consumers /// across all transports. + /// When neither nor is specified, the + /// middleware is appended to the end of the pipeline. + /// When is specified, the middleware is inserted immediately before + /// the middleware with the given key. + /// When is specified, the middleware is inserted immediately after + /// the middleware with the given key. /// /// The consumer middleware configuration to add. + /// + /// The key of the existing middleware before which to insert, or null to skip + /// positional insertion. + /// + /// + /// The key of the existing middleware after which to insert, or null to skip + /// positional insertion. + /// /// The builder instance for method chaining. - public IMessageBusBuilder UseConsume(ConsumerMiddlewareConfiguration configuration) + /// + /// Thrown when both and are specified. + /// + public IMessageBusBuilder UseConsume( + ConsumerMiddlewareConfiguration configuration, + string? before = null, + string? after = null) { - _handlerMiddlewares.Add(configuration); - - return this; - } - - /// - /// Prepends a consumer middleware to the beginning of the bus-level consume pipeline. - /// - /// The consumer middleware configuration to prepend. - /// The builder instance for method chaining. - public IMessageBusBuilder PrependConsume(ConsumerMiddlewareConfiguration configuration) - { - _handlerModifiers.Prepend(configuration, null); - - return this; - } - - /// - /// Appends a consumer middleware to the end of the bus-level consume pipeline. - /// - /// The consumer middleware configuration to append. - /// The builder instance for method chaining. - public IMessageBusBuilder AppendConsume(ConsumerMiddlewareConfiguration configuration) - { - _handlerModifiers.Append(configuration, null); - - return this; - } - - /// - /// Inserts a consumer middleware into the bus-level consume pipeline immediately after the - /// middleware identified by . - /// - /// The name of the existing middleware to insert after. - /// The consumer middleware configuration to insert. - /// The builder instance for method chaining. - public IMessageBusBuilder AppendConsume(string after, ConsumerMiddlewareConfiguration configuration) - { - _handlerModifiers.Append(configuration, after); - - return this; - } - - /// - /// Inserts a consumer middleware into the bus-level consume pipeline immediately before the - /// middleware identified by . - /// - /// The name of the existing middleware to insert before. - /// The consumer middleware configuration to insert. - /// The builder instance for method chaining. - public IMessageBusBuilder PrependConsume(string before, ConsumerMiddlewareConfiguration configuration) - { - _handlerModifiers.Prepend(configuration, before); + if (before is not null && after is not null) + { + throw new ArgumentException( + "Only one of 'before' or 'after' can be specified at the same time."); + } + + if (before is null && after is null) + { + _handlerMiddlewares.Add(configuration); + return this; + } + + if (before is not null) + { + _handlerModifiers.Prepend(configuration, before); + } + else + { + _handlerModifiers.Append(configuration, after); + } return this; } /// /// Adds a receive middleware to the bus-level inbound pipeline, applied to all transports. + /// When neither nor is specified, the + /// middleware is appended to the end of the pipeline. + /// When is specified, the middleware is inserted immediately before + /// the middleware with the given key. + /// When is specified, the middleware is inserted immediately after + /// the middleware with the given key. /// /// The receive middleware configuration to add. + /// + /// The key of the existing middleware before which to insert, or null to skip + /// positional insertion. + /// + /// + /// The key of the existing middleware after which to insert, or null to skip + /// positional insertion. + /// /// The builder instance for method chaining. - public IMessageBusBuilder UseReceive(ReceiveMiddlewareConfiguration configuration) - { - _receiveMiddlewares.Add(configuration); - - return this; - } - - /// - /// Prepends a receive middleware to the beginning of the bus-level inbound pipeline. - /// - /// The receive middleware configuration to prepend. - /// The builder instance for method chaining. - public IMessageBusBuilder PrependReceive(ReceiveMiddlewareConfiguration configuration) - { - _receiveModifiers.Prepend(configuration, null); - - return this; - } - - /// - /// Appends a receive middleware to the end of the bus-level inbound pipeline. - /// - /// The receive middleware configuration to append. - /// The builder instance for method chaining. - public IMessageBusBuilder AppendReceive(ReceiveMiddlewareConfiguration configuration) + /// + /// Thrown when both and are specified. + /// + public IMessageBusBuilder UseReceive( + ReceiveMiddlewareConfiguration configuration, + string? before = null, + string? after = null) { - _receiveModifiers.Append(configuration, null); - - return this; - } - - /// - /// Inserts a receive middleware into the bus-level inbound pipeline immediately after the - /// middleware identified by . - /// - /// The name of the existing middleware to insert after. - /// The receive middleware configuration to insert. - /// The builder instance for method chaining. - public IMessageBusBuilder AppendReceive(string after, ReceiveMiddlewareConfiguration configuration) - { - _receiveModifiers.Append(configuration, after); - - return this; - } - - /// - /// Inserts a receive middleware into the bus-level inbound pipeline immediately before the - /// middleware identified by . - /// - /// The name of the existing middleware to insert before. - /// The receive middleware configuration to insert. - /// The builder instance for method chaining. - public IMessageBusBuilder PrependReceive(string before, ReceiveMiddlewareConfiguration configuration) - { - _receiveModifiers.Prepend(configuration, before); + if (before is not null && after is not null) + { + throw new ArgumentException( + "Only one of 'before' or 'after' can be specified at the same time."); + } + + if (before is null && after is null) + { + _receiveMiddlewares.Add(configuration); + return this; + } + + if (before is not null) + { + _receiveModifiers.Prepend(configuration, before); + } + else + { + _receiveModifiers.Append(configuration, after); + } return this; } /// /// Adds a dispatch middleware to the bus-level outbound pipeline, applied to all transports. + /// When neither nor is specified, the + /// middleware is appended to the end of the pipeline. + /// When is specified, the middleware is inserted immediately before + /// the middleware with the given key. + /// When is specified, the middleware is inserted immediately after + /// the middleware with the given key. /// /// The dispatch middleware configuration to add. + /// + /// The key of the existing middleware before which to insert, or null to skip + /// positional insertion. + /// + /// + /// The key of the existing middleware after which to insert, or null to skip + /// positional insertion. + /// /// The builder instance for method chaining. - public IMessageBusBuilder UseDispatch(DispatchMiddlewareConfiguration configuration) - { - _dispatchMiddlewares.Add(configuration); - - return this; - } - - /// - /// Appends a dispatch middleware to the end of the bus-level outbound pipeline. - /// - /// The dispatch middleware configuration to append. - /// The builder instance for method chaining. - public IMessageBusBuilder AppendDispatch(DispatchMiddlewareConfiguration configuration) - { - _dispatchModifiers.Append(configuration, null); - - return this; - } - - /// - /// Inserts a dispatch middleware into the bus-level outbound pipeline immediately after the - /// middleware identified by . - /// - /// The name of the existing middleware to insert after. - /// The dispatch middleware configuration to insert. - /// The builder instance for method chaining. - public IMessageBusBuilder AppendDispatch(string after, DispatchMiddlewareConfiguration configuration) - { - _dispatchModifiers.Append(configuration, after); - - return this; - } - - /// - /// Prepends a dispatch middleware to the beginning of the bus-level outbound pipeline. - /// - /// The dispatch middleware configuration to prepend. - /// The builder instance for method chaining. - public IMessageBusBuilder PrependDispatch(DispatchMiddlewareConfiguration configuration) - { - _dispatchModifiers.Prepend(configuration, null); - - return this; - } - - /// - /// Inserts a dispatch middleware into the bus-level outbound pipeline immediately before the - /// middleware identified by . - /// - /// The name of the existing middleware to insert before. - /// The dispatch middleware configuration to insert. - /// The builder instance for method chaining. - public IMessageBusBuilder PrependDispatch(string before, DispatchMiddlewareConfiguration configuration) + /// + /// Thrown when both and are specified. + /// + public IMessageBusBuilder UseDispatch( + DispatchMiddlewareConfiguration configuration, + string? before = null, + string? after = null) { - _dispatchModifiers.Prepend(configuration, before); + if (before is not null && after is not null) + { + throw new ArgumentException( + "Only one of 'before' or 'after' can be specified at the same time."); + } + + if (before is null && after is null) + { + _dispatchMiddlewares.Add(configuration); + return this; + } + + if (before is not null) + { + _dispatchModifiers.Prepend(configuration, before); + } + else + { + _dispatchModifiers.Append(configuration, after); + } return this; } diff --git a/src/Mocha/src/Mocha/Consumers/Descriptors/ConsumerDescriptor.cs b/src/Mocha/src/Mocha/Consumers/Descriptors/ConsumerDescriptor.cs index 088216fe5d2..4bbba6566e8 100644 --- a/src/Mocha/src/Mocha/Consumers/Descriptors/ConsumerDescriptor.cs +++ b/src/Mocha/src/Mocha/Consumers/Descriptors/ConsumerDescriptor.cs @@ -38,23 +38,32 @@ public IConsumerDescriptor AddRoute(Action configure) } /// - public IConsumerDescriptor UseConsumer(ConsumerMiddlewareConfiguration configuration) + public IConsumerDescriptor UseConsumer( + ConsumerMiddlewareConfiguration configuration, + string? before = null, + string? after = null) { - Configuration.ConsumerMiddlewares.Add(configuration); - return this; - } + if (before is not null && after is not null) + { + throw new ArgumentException( + "Only one of 'before' or 'after' can be specified at the same time."); + } - /// - public IConsumerDescriptor AppendConsumer(string after, ConsumerMiddlewareConfiguration configuration) - { - Configuration.ConsumerPipelineModifiers.Append(configuration, after); - return this; - } + if (before is null && after is null) + { + Configuration.ConsumerMiddlewares.Add(configuration); + return this; + } + + if (before is not null) + { + Configuration.ConsumerPipelineModifiers.Prepend(configuration, before); + } + else + { + Configuration.ConsumerPipelineModifiers.Append(configuration, after); + } - /// - public IConsumerDescriptor PrependConsumer(string before, ConsumerMiddlewareConfiguration configuration) - { - Configuration.ConsumerPipelineModifiers.Prepend(configuration, before); return this; } diff --git a/src/Mocha/src/Mocha/Consumers/Descriptors/IConsumerDescriptor.cs b/src/Mocha/src/Mocha/Consumers/Descriptors/IConsumerDescriptor.cs index 73ebb84b0f8..333879eb6f3 100644 --- a/src/Mocha/src/Mocha/Consumers/Descriptors/IConsumerDescriptor.cs +++ b/src/Mocha/src/Mocha/Consumers/Descriptors/IConsumerDescriptor.cs @@ -22,27 +22,29 @@ public interface IConsumerDescriptor : IMessagingDescriptor configure); /// - /// Appends a consumer-scoped middleware configuration to the consumer's middleware pipeline. + /// Adds a consumer-scoped middleware configuration to the consumer's middleware pipeline. + /// When neither nor is specified, the + /// middleware is appended to the end of the pipeline. + /// When is specified, the middleware is inserted immediately before + /// the middleware with the given key. + /// When is specified, the middleware is inserted immediately after + /// the middleware with the given key. /// /// The consumer middleware configuration to add. + /// + /// The key of the existing middleware before which to insert, or null to skip + /// positional insertion. + /// + /// + /// The key of the existing middleware after which to insert, or null to skip + /// positional insertion. + /// /// The descriptor instance for method chaining. - IConsumerDescriptor UseConsumer(ConsumerMiddlewareConfiguration configuration); - - /// - /// Inserts a consumer-scoped middleware configuration immediately after the middleware with the - /// specified name. - /// - /// The name of the existing middleware after which to insert. - /// The consumer middleware configuration to insert. - /// The descriptor instance for method chaining. - IConsumerDescriptor AppendConsumer(string after, ConsumerMiddlewareConfiguration configuration); - - /// - /// Inserts a consumer-scoped middleware configuration immediately before the middleware with - /// the specified name. - /// - /// The name of the existing middleware before which to insert. - /// The consumer middleware configuration to insert. - /// The descriptor instance for method chaining. - IConsumerDescriptor PrependConsumer(string before, ConsumerMiddlewareConfiguration configuration); + /// + /// Thrown when both and are specified. + /// + IConsumerDescriptor UseConsumer( + ConsumerMiddlewareConfiguration configuration, + string? before = null, + string? after = null); } diff --git a/src/Mocha/src/Mocha/Endpoints/Descriptors/DispatchEndpointDescriptor.cs b/src/Mocha/src/Mocha/Endpoints/Descriptors/DispatchEndpointDescriptor.cs index 26d59c1eeb4..554fffd47f7 100644 --- a/src/Mocha/src/Mocha/Endpoints/Descriptors/DispatchEndpointDescriptor.cs +++ b/src/Mocha/src/Mocha/Endpoints/Descriptors/DispatchEndpointDescriptor.cs @@ -22,21 +22,32 @@ public IDispatchEndpointDescriptor Publish() return this; } - public IDispatchEndpointDescriptor UseDispatch(DispatchMiddlewareConfiguration configuration) + public IDispatchEndpointDescriptor UseDispatch( + DispatchMiddlewareConfiguration configuration, + string? before = null, + string? after = null) { - Configuration.DispatchMiddlewares.Add(configuration); - return this; - } + if (before is not null && after is not null) + { + throw new ArgumentException( + "Only one of 'before' or 'after' can be specified at the same time."); + } - public IDispatchEndpointDescriptor AppendDispatch(string after, DispatchMiddlewareConfiguration configuration) - { - Configuration.DispatchPipelineModifiers.Append(configuration, after); - return this; - } + if (before is null && after is null) + { + Configuration.DispatchMiddlewares.Add(configuration); + return this; + } + + if (before is not null) + { + Configuration.DispatchPipelineModifiers.Prepend(configuration, before); + } + else + { + Configuration.DispatchPipelineModifiers.Append(configuration, after); + } - public IDispatchEndpointDescriptor PrependDispatch(string before, DispatchMiddlewareConfiguration configuration) - { - Configuration.DispatchPipelineModifiers.Prepend(configuration, before); return this; } } diff --git a/src/Mocha/src/Mocha/Endpoints/Descriptors/IDispatchEndpointDescriptor.cs b/src/Mocha/src/Mocha/Endpoints/Descriptors/IDispatchEndpointDescriptor.cs index 6637097bb8d..910bab3eef5 100644 --- a/src/Mocha/src/Mocha/Endpoints/Descriptors/IDispatchEndpointDescriptor.cs +++ b/src/Mocha/src/Mocha/Endpoints/Descriptors/IDispatchEndpointDescriptor.cs @@ -22,29 +22,15 @@ public interface IDispatchEndpointDescriptor : IMessagingDescrip IDispatchEndpointDescriptor Publish(); /// - /// Appends a dispatch middleware configuration to this endpoint's dispatch pipeline. + /// Adds a dispatch middleware to this endpoint's dispatch pipeline. Optionally positions it + /// relative to an existing middleware by specifying or . /// /// The dispatch middleware configuration to add. + /// The name of the existing middleware to insert before. + /// The name of the existing middleware to insert after. /// The descriptor instance for method chaining. - IDispatchEndpointDescriptor UseDispatch(DispatchMiddlewareConfiguration configuration); - - /// - /// Inserts a dispatch middleware configuration after the middleware with the specified name. - /// - /// The name of the existing middleware after which to insert. - /// The dispatch middleware configuration to insert. - /// The descriptor instance for method chaining. - IDispatchEndpointDescriptor AppendDispatch( - string after, - DispatchMiddlewareConfiguration configuration); - - /// - /// Inserts a dispatch middleware configuration before the middleware with the specified name. - /// - /// The name of the existing middleware before which to insert. - /// The dispatch middleware configuration to insert. - /// The descriptor instance for method chaining. - IDispatchEndpointDescriptor PrependDispatch( - string before, - DispatchMiddlewareConfiguration configuration); + IDispatchEndpointDescriptor UseDispatch( + DispatchMiddlewareConfiguration configuration, + string? before = null, + string? after = null); } diff --git a/src/Mocha/src/Mocha/Endpoints/Descriptors/IReceiveEndpointDescriptor.cs b/src/Mocha/src/Mocha/Endpoints/Descriptors/IReceiveEndpointDescriptor.cs index 351b55a6ba6..ea9b3d26e0c 100644 --- a/src/Mocha/src/Mocha/Endpoints/Descriptors/IReceiveEndpointDescriptor.cs +++ b/src/Mocha/src/Mocha/Endpoints/Descriptors/IReceiveEndpointDescriptor.cs @@ -52,29 +52,15 @@ public interface IReceiveEndpointDescriptor IReceiveEndpointDescriptor SkippedEndpoint(string name); /// - /// Appends a receive middleware configuration to this endpoint's receive pipeline. + /// Adds a receive middleware to this endpoint's receive pipeline. Optionally positions it + /// relative to an existing middleware by specifying or . /// /// The receive middleware configuration to add. + /// The name of the existing middleware to insert before. + /// The name of the existing middleware to insert after. /// The descriptor instance for method chaining. - IReceiveEndpointDescriptor UseReceive(ReceiveMiddlewareConfiguration configuration); - - /// - /// Inserts a receive middleware configuration after the middleware with the specified name. - /// - /// The name of the existing middleware after which to insert. - /// The receive middleware configuration to insert. - /// The descriptor instance for method chaining. - IReceiveEndpointDescriptor AppendReceive( - string after, - ReceiveMiddlewareConfiguration configuration); - - /// - /// Inserts a receive middleware configuration before the middleware with the specified name. - /// - /// The name of the existing middleware before which to insert. - /// The receive middleware configuration to insert. - /// The descriptor instance for method chaining. - IReceiveEndpointDescriptor PrependReceive( - string before, - ReceiveMiddlewareConfiguration configuration); + IReceiveEndpointDescriptor UseReceive( + ReceiveMiddlewareConfiguration configuration, + string? before = null, + string? after = null); } diff --git a/src/Mocha/src/Mocha/Endpoints/Descriptors/ReceiveEndpointDescriptor.cs b/src/Mocha/src/Mocha/Endpoints/Descriptors/ReceiveEndpointDescriptor.cs index acbdc26a9f6..fcb113eb08c 100644 --- a/src/Mocha/src/Mocha/Endpoints/Descriptors/ReceiveEndpointDescriptor.cs +++ b/src/Mocha/src/Mocha/Endpoints/Descriptors/ReceiveEndpointDescriptor.cs @@ -46,21 +46,31 @@ public IReceiveEndpointDescriptor SkippedEndpoint(string address) return this; } - public IReceiveEndpointDescriptor UseReceive(ReceiveMiddlewareConfiguration configuration) + public IReceiveEndpointDescriptor UseReceive( + ReceiveMiddlewareConfiguration configuration, + string? before = null, + string? after = null) { - Configuration.ReceiveMiddlewares.Add(configuration); - return this; - } + if (before is not null && after is not null) + { + throw new ArgumentException( + "Only one of 'before' or 'after' can be specified at the same time."); + } - public IReceiveEndpointDescriptor AppendReceive(string after, ReceiveMiddlewareConfiguration configuration) - { - Configuration.ReceivePipelineModifiers.Append(configuration, after); - return this; - } + if (before is null && after is null) + { + Configuration.ReceiveMiddlewares.Add(configuration); + return this; + } - public IReceiveEndpointDescriptor PrependReceive(string before, ReceiveMiddlewareConfiguration configuration) - { - Configuration.ReceivePipelineModifiers.Prepend(configuration, before); + if (before is not null) + { + Configuration.ReceivePipelineModifiers.Prepend(configuration, before); + } + else + { + Configuration.ReceivePipelineModifiers.Append(configuration, after); + } return this; } diff --git a/src/Mocha/src/Mocha/Transport/MessagingTransportDescriptor.cs b/src/Mocha/src/Mocha/Transport/MessagingTransportDescriptor.cs index c720f0ea99a..24e11ba2d27 100644 --- a/src/Mocha/src/Mocha/Transport/MessagingTransportDescriptor.cs +++ b/src/Mocha/src/Mocha/Transport/MessagingTransportDescriptor.cs @@ -66,50 +66,30 @@ public interface IMessagingTransportDescriptor IMessagingTransportDescriptor IsDefaultTransport(); /// - /// Adds a dispatch middleware to the transport-scoped outbound pipeline. + /// Adds a dispatch middleware to the transport-scoped outbound pipeline. Optionally positions it + /// relative to an existing middleware by specifying or . /// /// The middleware configuration to add. - /// The descriptor for method chaining. - IMessagingTransportDescriptor UseDispatch(DispatchMiddlewareConfiguration configuration); - - /// - /// Inserts a dispatch middleware into the transport-scoped outbound pipeline immediately after the middleware identified by . - /// - /// The name of the existing middleware to insert after. - /// The middleware configuration to insert. - /// The descriptor for method chaining. - IMessagingTransportDescriptor AppendDispatch(string after, DispatchMiddlewareConfiguration configuration); - - /// - /// Inserts a dispatch middleware into the transport-scoped outbound pipeline immediately before the middleware identified by . - /// /// The name of the existing middleware to insert before. - /// The middleware configuration to insert. - /// The descriptor for method chaining. - IMessagingTransportDescriptor PrependDispatch(string before, DispatchMiddlewareConfiguration configuration); - - /// - /// Adds a receive middleware to the transport-scoped inbound pipeline. - /// - /// The middleware configuration to add. - /// The descriptor for method chaining. - IMessagingTransportDescriptor UseReceive(ReceiveMiddlewareConfiguration configuration); - - /// - /// Inserts a receive middleware into the transport-scoped inbound pipeline immediately after the middleware identified by . - /// /// The name of the existing middleware to insert after. - /// The middleware configuration to insert. /// The descriptor for method chaining. - IMessagingTransportDescriptor AppendReceive(string after, ReceiveMiddlewareConfiguration configuration); + IMessagingTransportDescriptor UseDispatch( + DispatchMiddlewareConfiguration configuration, + string? before = null, + string? after = null); /// - /// Inserts a receive middleware into the transport-scoped inbound pipeline immediately before the middleware identified by . + /// Adds a receive middleware to the transport-scoped inbound pipeline. Optionally positions it + /// relative to an existing middleware by specifying or . /// + /// The middleware configuration to add. /// The name of the existing middleware to insert before. - /// The middleware configuration to insert. + /// The name of the existing middleware to insert after. /// The descriptor for method chaining. - IMessagingTransportDescriptor PrependReceive(string before, ReceiveMiddlewareConfiguration configuration); + IMessagingTransportDescriptor UseReceive( + ReceiveMiddlewareConfiguration configuration, + string? before = null, + string? after = null); } /// @@ -165,16 +145,32 @@ public IMessagingTransportDescriptor IsDefaultTransport() } /// - public IMessagingTransportDescriptor UseDispatch(DispatchMiddlewareConfiguration configuration) + public IMessagingTransportDescriptor UseDispatch( + DispatchMiddlewareConfiguration configuration, + string? before = null, + string? after = null) { - Configuration.DispatchMiddlewares.Add(configuration); - return this; - } + if (before is not null && after is not null) + { + throw new ArgumentException( + "Only one of 'before' or 'after' can be specified at the same time."); + } + + if (before is null && after is null) + { + Configuration.DispatchMiddlewares.Add(configuration); + return this; + } + + if (before is not null) + { + Configuration.DispatchPipelineModifiers.Prepend(configuration, before); + } + else + { + Configuration.DispatchPipelineModifiers.Append(configuration, after); + } - /// - public IMessagingTransportDescriptor AppendDispatch(string after, DispatchMiddlewareConfiguration configuration) - { - Configuration.DispatchPipelineModifiers.Append(configuration, after); return this; } @@ -186,30 +182,32 @@ public IMessagingTransportDescriptor AddConvention(IConvention convention) } /// - public IMessagingTransportDescriptor PrependDispatch(string before, DispatchMiddlewareConfiguration configuration) + public IMessagingTransportDescriptor UseReceive( + ReceiveMiddlewareConfiguration configuration, + string? before = null, + string? after = null) { - Configuration.DispatchPipelineModifiers.Prepend(configuration, before); - return this; - } + if (before is not null && after is not null) + { + throw new ArgumentException( + "Only one of 'before' or 'after' can be specified at the same time."); + } + + if (before is null && after is null) + { + Configuration.ReceiveMiddlewares.Add(configuration); + return this; + } + + if (before is not null) + { + Configuration.ReceivePipelineModifiers.Prepend(configuration, before); + } + else + { + Configuration.ReceivePipelineModifiers.Append(configuration, after); + } - /// - public IMessagingTransportDescriptor UseReceive(ReceiveMiddlewareConfiguration configuration) - { - Configuration.ReceiveMiddlewares.Add(configuration); - return this; - } - - /// - public IMessagingTransportDescriptor AppendReceive(string after, ReceiveMiddlewareConfiguration configuration) - { - Configuration.ReceivePipelineModifiers.Append(configuration, after); - return this; - } - - /// - public IMessagingTransportDescriptor PrependReceive(string before, ReceiveMiddlewareConfiguration configuration) - { - Configuration.ReceivePipelineModifiers.Prepend(configuration, before); return this; } diff --git a/src/Mocha/test/Mocha.Tests/Inbox/InboxIntegrationTests.cs b/src/Mocha/test/Mocha.Tests/Inbox/InboxIntegrationTests.cs index 10e8e3ad997..b67cfad4925 100644 --- a/src/Mocha/test/Mocha.Tests/Inbox/InboxIntegrationTests.cs +++ b/src/Mocha/test/Mocha.Tests/Inbox/InboxIntegrationTests.cs @@ -112,8 +112,7 @@ public async Task Inbox_Should_SkipRecording_When_SkipInboxFeatureSet() // Add a consumer middleware before inbox that sets SkipInbox b.ConfigureMessageBus(h => - h.PrependConsume( - "Inbox", + h.UseConsume( new ConsumerMiddlewareConfiguration( static (_, next) => ctx => @@ -122,7 +121,8 @@ public async Task Inbox_Should_SkipRecording_When_SkipInboxFeatureSet() feature.SkipInbox = true; return next(ctx); }, - "SkipInboxCheck")) + "SkipInboxCheck"), + before: "Inbox") ); }); diff --git a/src/Mocha/test/Mocha.Tests/Outbox/OutboxIntegrationTests.cs b/src/Mocha/test/Mocha.Tests/Outbox/OutboxIntegrationTests.cs index c8912f9672d..9a242578fcd 100644 --- a/src/Mocha/test/Mocha.Tests/Outbox/OutboxIntegrationTests.cs +++ b/src/Mocha/test/Mocha.Tests/Outbox/OutboxIntegrationTests.cs @@ -151,8 +151,7 @@ private static async Task CreateBusWithOutboxAsync( // Add a middleware before outbox that checks for the skip header builder.ConfigureMessageBus(h => - h.PrependDispatch( - "Outbox", + h.UseDispatch( new DispatchMiddlewareConfiguration( static (_, next) => ctx => @@ -163,7 +162,8 @@ private static async Task CreateBusWithOutboxAsync( } return next(ctx); }, - "SkipOutboxCheck")) + "SkipOutboxCheck"), + before: "Outbox") ); configure(builder); diff --git a/src/Mocha/test/Mocha.Transport.InMemory.Tests/Behaviors/InboxTests.cs b/src/Mocha/test/Mocha.Transport.InMemory.Tests/Behaviors/InboxTests.cs index 7d96f3d19e6..d83d80dca17 100644 --- a/src/Mocha/test/Mocha.Transport.InMemory.Tests/Behaviors/InboxTests.cs +++ b/src/Mocha/test/Mocha.Transport.InMemory.Tests/Behaviors/InboxTests.cs @@ -28,14 +28,15 @@ public async Task Inbox_Should_DeduplicateMessage_When_SameMessageIdPublishedTwi // Force every dispatched message to use the same MessageId b.ConfigureMessageBus(h => - h.PrependDispatch(new DispatchMiddlewareConfiguration( + h.UseDispatch(new DispatchMiddlewareConfiguration( (_, next) => ctx => { ctx.MessageId = messageId; return next(ctx); }, - "ForceMessageId"))); + "ForceMessageId"), + before: "Instrumentation")); }); using var scope = provider.CreateScope(); @@ -121,7 +122,7 @@ public async Task Inbox_Should_ProcessMessage_When_SkipInboxIsSet() // Force the dispatched message to use the pre-seeded MessageId b.ConfigureMessageBus(h => - h.PrependDispatch(new DispatchMiddlewareConfiguration( + h.UseDispatch(new DispatchMiddlewareConfiguration( (_, next) => ctx => { @@ -132,8 +133,7 @@ public async Task Inbox_Should_ProcessMessage_When_SkipInboxIsSet() // Add a consumer middleware before inbox that sets SkipInbox b.ConfigureMessageBus(h => - h.PrependConsume( - "Inbox", + h.UseConsume( new ConsumerMiddlewareConfiguration( static (_, next) => ctx => @@ -142,7 +142,8 @@ public async Task Inbox_Should_ProcessMessage_When_SkipInboxIsSet() feature.SkipInbox = true; return next(ctx); }, - "SkipInboxCheck"))); + "SkipInboxCheck"), + before: "Inbox")); }); using var scope = provider.CreateScope(); @@ -174,14 +175,15 @@ public async Task Inbox_Should_ProcessMessage_When_MessageIdIsNull() // Null out the MessageId on the dispatch side b.ConfigureMessageBus(h => - h.PrependDispatch(new DispatchMiddlewareConfiguration( + h.UseDispatch(new DispatchMiddlewareConfiguration( (_, next) => ctx => { ctx.MessageId = null; return next(ctx); }, - "NullifyMessageId"))); + "NullifyMessageId"), + before: "Instrumentation")); }); using var scope = provider.CreateScope(); @@ -220,14 +222,15 @@ public async Task Inbox_Should_SkipSucceededHandler_And_RetryFailedHandler_When_ // Force every dispatched message to use the same MessageId b.ConfigureMessageBus(h => - h.PrependDispatch(new DispatchMiddlewareConfiguration( + h.UseDispatch(new DispatchMiddlewareConfiguration( (_, next) => ctx => { ctx.MessageId = messageId; return next(ctx); }, - "ForceMessageId"))); + "ForceMessageId"), + before: "Instrumentation")); }); using var scope = provider.CreateScope(); @@ -347,8 +350,7 @@ private static async Task CreateBusWithTransactionalInboxAsync( // consumer middleware factory runs against an internal service provider that does not // contain application-level singletons. builder.ConfigureMessageBus(h => - h.PrependConsume( - "Inbox", + h.UseConsume( new ConsumerMiddlewareConfiguration( (_, next) => async ctx => @@ -372,7 +374,8 @@ private static async Task CreateBusWithTransactionalInboxAsync( throw; } }, - "InboxTransactionRollback"))); + "InboxTransactionRollback"), + before: "Inbox")); configure(builder); builder.AddInMemory(); diff --git a/src/Mocha/test/Mocha.Transport.Postgres.Tests/Behaviors/EndpointMiddlewareTests.cs b/src/Mocha/test/Mocha.Transport.Postgres.Tests/Behaviors/EndpointMiddlewareTests.cs index 8713fdeb946..1a8366482ae 100644 --- a/src/Mocha/test/Mocha.Transport.Postgres.Tests/Behaviors/EndpointMiddlewareTests.cs +++ b/src/Mocha/test/Mocha.Transport.Postgres.Tests/Behaviors/EndpointMiddlewareTests.cs @@ -56,7 +56,7 @@ public async Task UseReceive_Should_InvokeMiddleware_When_MessageReceived() } [Fact] - public async Task AppendReceive_Should_InvokeMiddleware_When_MessageReceived() + public async Task UseReceive_After_Should_InvokeMiddleware_When_MessageReceived() { // arrange var tracker = new MiddlewareTracker(); @@ -72,8 +72,7 @@ public async Task AppendReceive_Should_InvokeMiddleware_When_MessageReceived() t.ConnectionString(db.ConnectionString); t.Endpoint("ep") .Consumer() - .AppendReceive( - "PostgresParsing", + .UseReceive( new ReceiveMiddlewareConfiguration( (_, next) => async context => @@ -81,7 +80,8 @@ public async Task AppendReceive_Should_InvokeMiddleware_When_MessageReceived() tracker.Add("append-receive-mw"); await next(context); }, - "test-append-receive")); + "test-append-receive"), + after: "PostgresParsing"); }) .BuildTestBusAsync(); @@ -97,7 +97,7 @@ public async Task AppendReceive_Should_InvokeMiddleware_When_MessageReceived() } [Fact] - public async Task PrependReceive_Should_InvokeMiddleware_When_MessageReceived() + public async Task UseReceive_Before_Should_InvokeMiddleware_When_MessageReceived() { // arrange var tracker = new MiddlewareTracker(); @@ -113,8 +113,7 @@ public async Task PrependReceive_Should_InvokeMiddleware_When_MessageReceived() t.ConnectionString(db.ConnectionString); t.Endpoint("ep") .Consumer() - .PrependReceive( - "PostgresParsing", + .UseReceive( new ReceiveMiddlewareConfiguration( (_, next) => async context => @@ -122,7 +121,8 @@ public async Task PrependReceive_Should_InvokeMiddleware_When_MessageReceived() tracker.Add("prepend-receive-mw"); await next(context); }, - "test-prepend-receive")); + "test-prepend-receive"), + before: "PostgresParsing"); }) .BuildTestBusAsync(); @@ -183,7 +183,7 @@ public async Task UseDispatch_Should_InvokeMiddleware_When_MessageDispatched() } [Fact] - public async Task AppendDispatch_Should_InvokeMiddleware_When_MessageDispatched() + public async Task UseDispatch_After_Should_InvokeMiddleware_When_MessageDispatched() { // arrange var tracker = new MiddlewareTracker(); @@ -204,8 +204,7 @@ public async Task AppendDispatch_Should_InvokeMiddleware_When_MessageDispatched( t.DispatchEndpoint("ep") .ToTopic("ex") .Publish() - .AppendDispatch( - "Instrumentation", + .UseDispatch( new DispatchMiddlewareConfiguration( (_, next) => async context => @@ -213,7 +212,8 @@ public async Task AppendDispatch_Should_InvokeMiddleware_When_MessageDispatched( tracker.Add("append-dispatch-mw"); await next(context); }, - "test-append-dispatch")); + "test-append-dispatch"), + after: "Instrumentation"); }) .BuildTestBusAsync(); @@ -229,7 +229,7 @@ public async Task AppendDispatch_Should_InvokeMiddleware_When_MessageDispatched( } [Fact] - public async Task PrependDispatch_Should_InvokeMiddleware_When_MessageDispatched() + public async Task UseDispatch_Before_Should_InvokeMiddleware_When_MessageDispatched() { // arrange var tracker = new MiddlewareTracker(); @@ -250,8 +250,7 @@ public async Task PrependDispatch_Should_InvokeMiddleware_When_MessageDispatched t.DispatchEndpoint("ep") .ToTopic("ex") .Publish() - .PrependDispatch( - "Instrumentation", + .UseDispatch( new DispatchMiddlewareConfiguration( (ctx, next) => async context => @@ -259,7 +258,8 @@ public async Task PrependDispatch_Should_InvokeMiddleware_When_MessageDispatched tracker.Add("prepend-dispatch-mw"); await next(context); }, - "test-prepend-dispatch")); + "test-prepend-dispatch"), + before: "Instrumentation"); }) .BuildTestBusAsync(); diff --git a/src/Mocha/test/Mocha.Transport.Postgres.Tests/Behaviors/TransportMiddlewareTests.cs b/src/Mocha/test/Mocha.Transport.Postgres.Tests/Behaviors/TransportMiddlewareTests.cs index 79da187ff65..c16844d2af4 100644 --- a/src/Mocha/test/Mocha.Transport.Postgres.Tests/Behaviors/TransportMiddlewareTests.cs +++ b/src/Mocha/test/Mocha.Transport.Postgres.Tests/Behaviors/TransportMiddlewareTests.cs @@ -61,7 +61,7 @@ public async Task UseReceive_Should_InvokeOnAllEndpoints_When_ConfiguredAtTransp } [Fact] - public async Task AppendReceive_Should_InvokeOnAllEndpoints_When_ConfiguredAtTransportLevel() + public async Task UseReceive_After_Should_InvokeOnAllEndpoints_When_ConfiguredAtTransportLevel() { // arrange var tracker = new MiddlewareTracker(); @@ -76,8 +76,7 @@ public async Task AppendReceive_Should_InvokeOnAllEndpoints_When_ConfiguredAtTra { t.ConnectionString(db.ConnectionString); - t.AppendReceive( - "PostgresParsing", + t.UseReceive( new ReceiveMiddlewareConfiguration( (ctx, next) => async context => @@ -85,7 +84,8 @@ public async Task AppendReceive_Should_InvokeOnAllEndpoints_When_ConfiguredAtTra tracker.Add("transport-append-receive"); await next(context); }, - "test-transport-append-receive")); + "test-transport-append-receive"), + after: "PostgresParsing"); t.DeclareTopic("ex"); t.DeclareQueue("q"); @@ -107,7 +107,7 @@ public async Task AppendReceive_Should_InvokeOnAllEndpoints_When_ConfiguredAtTra } [Fact] - public async Task PrependReceive_Should_InvokeOnAllEndpoints_When_ConfiguredAtTransportLevel() + public async Task UseReceive_Before_Should_InvokeOnAllEndpoints_When_ConfiguredAtTransportLevel() { // arrange var tracker = new MiddlewareTracker(); @@ -122,8 +122,7 @@ public async Task PrependReceive_Should_InvokeOnAllEndpoints_When_ConfiguredAtTr { t.ConnectionString(db.ConnectionString); - t.PrependReceive( - "PostgresParsing", + t.UseReceive( new ReceiveMiddlewareConfiguration( (ctx, next) => async context => @@ -131,7 +130,8 @@ public async Task PrependReceive_Should_InvokeOnAllEndpoints_When_ConfiguredAtTr tracker.Add("transport-prepend-receive"); await next(context); }, - "test-transport-prepend-receive")); + "test-transport-prepend-receive"), + before: "PostgresParsing"); t.DeclareTopic("ex"); t.DeclareQueue("q"); @@ -192,7 +192,7 @@ public async Task UseDispatch_Should_InvokeOnAllEndpoints_When_ConfiguredAtTrans } [Fact] - public async Task AppendDispatch_Should_InvokeOnAllEndpoints_When_ConfiguredAtTransportLevel() + public async Task UseDispatch_After_Should_InvokeOnAllEndpoints_When_ConfiguredAtTransportLevel() { // arrange var tracker = new MiddlewareTracker(); @@ -207,8 +207,7 @@ public async Task AppendDispatch_Should_InvokeOnAllEndpoints_When_ConfiguredAtTr { t.ConnectionString(db.ConnectionString); - t.AppendDispatch( - "Instrumentation", + t.UseDispatch( new DispatchMiddlewareConfiguration( (ctx, next) => async context => @@ -216,7 +215,8 @@ public async Task AppendDispatch_Should_InvokeOnAllEndpoints_When_ConfiguredAtTr tracker.Add("transport-append-dispatch"); await next(context); }, - "test-transport-append-dispatch")); + "test-transport-append-dispatch"), + after: "Instrumentation"); }) .BuildTestBusAsync(); @@ -232,7 +232,7 @@ public async Task AppendDispatch_Should_InvokeOnAllEndpoints_When_ConfiguredAtTr } [Fact] - public async Task PrependDispatch_Should_InvokeOnAllEndpoints_When_ConfiguredAtTransportLevel() + public async Task UseDispatch_Before_Should_InvokeOnAllEndpoints_When_ConfiguredAtTransportLevel() { // arrange var tracker = new MiddlewareTracker(); @@ -247,8 +247,7 @@ public async Task PrependDispatch_Should_InvokeOnAllEndpoints_When_ConfiguredAtT { t.ConnectionString(db.ConnectionString); - t.PrependDispatch( - "Instrumentation", + t.UseDispatch( new DispatchMiddlewareConfiguration( (ctx, next) => async context => @@ -256,7 +255,8 @@ public async Task PrependDispatch_Should_InvokeOnAllEndpoints_When_ConfiguredAtT tracker.Add("transport-prepend-dispatch"); await next(context); }, - "test-transport-prepend-dispatch")); + "test-transport-prepend-dispatch"), + before: "Instrumentation"); }) .BuildTestBusAsync(); diff --git a/src/Mocha/test/Mocha.Transport.RabbitMQ.Tests/Behaviors/EndpointMiddlewareTests.cs b/src/Mocha/test/Mocha.Transport.RabbitMQ.Tests/Behaviors/EndpointMiddlewareTests.cs index c29619c5793..87c7e8710bb 100644 --- a/src/Mocha/test/Mocha.Transport.RabbitMQ.Tests/Behaviors/EndpointMiddlewareTests.cs +++ b/src/Mocha/test/Mocha.Transport.RabbitMQ.Tests/Behaviors/EndpointMiddlewareTests.cs @@ -56,7 +56,7 @@ public async Task UseReceive_Should_InvokeMiddleware_When_MessageReceived() } [Fact] - public async Task AppendReceive_Should_InvokeMiddleware_When_MessageReceived() + public async Task UseReceive_After_Should_InvokeMiddleware_When_MessageReceived() { // arrange var tracker = new MiddlewareTracker(); @@ -72,8 +72,7 @@ public async Task AppendReceive_Should_InvokeMiddleware_When_MessageReceived() { t.Endpoint("ep") .Consumer() - .AppendReceive( - "RabbitMQAcknowledgement", + .UseReceive( new ReceiveMiddlewareConfiguration( (_, next) => async context => @@ -81,7 +80,8 @@ public async Task AppendReceive_Should_InvokeMiddleware_When_MessageReceived() tracker.Add("append-receive-mw"); await next(context); }, - "test-append-receive")); + "test-append-receive"), + after: "RabbitMQAcknowledgement"); }) .BuildTestBusAsync(); @@ -97,7 +97,7 @@ public async Task AppendReceive_Should_InvokeMiddleware_When_MessageReceived() } [Fact] - public async Task PrependReceive_Should_InvokeMiddleware_When_MessageReceived() + public async Task UseReceive_Before_Should_InvokeMiddleware_When_MessageReceived() { // arrange var tracker = new MiddlewareTracker(); @@ -113,8 +113,7 @@ public async Task PrependReceive_Should_InvokeMiddleware_When_MessageReceived() { t.Endpoint("ep") .Consumer() - .PrependReceive( - "RabbitMQAcknowledgement", + .UseReceive( new ReceiveMiddlewareConfiguration( (_, next) => async context => @@ -122,7 +121,8 @@ public async Task PrependReceive_Should_InvokeMiddleware_When_MessageReceived() tracker.Add("prepend-receive-mw"); await next(context); }, - "test-prepend-receive")); + "test-prepend-receive"), + before: "RabbitMQAcknowledgement"); }) .BuildTestBusAsync(); @@ -178,7 +178,7 @@ public async Task UseDispatch_Should_InvokeMiddleware_When_MessageDispatched() } [Fact] - public async Task AppendDispatch_Should_InvokeMiddleware_When_MessageDispatched() + public async Task UseDispatch_After_Should_InvokeMiddleware_When_MessageDispatched() { // arrange var tracker = new MiddlewareTracker(); @@ -194,8 +194,7 @@ public async Task AppendDispatch_Should_InvokeMiddleware_When_MessageDispatched( { t.DispatchEndpoint("ep") .Publish() - .AppendDispatch( - "Instrumentation", + .UseDispatch( new DispatchMiddlewareConfiguration( (_, next) => async context => @@ -203,7 +202,8 @@ public async Task AppendDispatch_Should_InvokeMiddleware_When_MessageDispatched( tracker.Add("append-dispatch-mw"); await next(context); }, - "test-append-dispatch")); + "test-append-dispatch"), + after: "Instrumentation"); }) .BuildTestBusAsync(); @@ -219,7 +219,7 @@ public async Task AppendDispatch_Should_InvokeMiddleware_When_MessageDispatched( } [Fact] - public async Task PrependDispatch_Should_InvokeMiddleware_When_MessageDispatched() + public async Task UseDispatch_Before_Should_InvokeMiddleware_When_MessageDispatched() { // arrange var tracker = new MiddlewareTracker(); @@ -235,8 +235,7 @@ public async Task PrependDispatch_Should_InvokeMiddleware_When_MessageDispatched { t.DispatchEndpoint("ep") .Publish() - .PrependDispatch( - "Instrumentation", + .UseDispatch( new DispatchMiddlewareConfiguration( (ctx, next) => async context => @@ -244,7 +243,8 @@ public async Task PrependDispatch_Should_InvokeMiddleware_When_MessageDispatched tracker.Add("prepend-dispatch-mw"); await next(context); }, - "test-prepend-dispatch")); + "test-prepend-dispatch"), + before: "Instrumentation"); }) .BuildTestBusAsync(); diff --git a/src/Mocha/test/Mocha.Transport.RabbitMQ.Tests/Behaviors/InboxTests.cs b/src/Mocha/test/Mocha.Transport.RabbitMQ.Tests/Behaviors/InboxTests.cs index fe6a665ae1e..ee0b8dbe324 100644 --- a/src/Mocha/test/Mocha.Transport.RabbitMQ.Tests/Behaviors/InboxTests.cs +++ b/src/Mocha/test/Mocha.Transport.RabbitMQ.Tests/Behaviors/InboxTests.cs @@ -38,14 +38,15 @@ public async Task Inbox_Should_DeduplicateMessage_When_SameMessageIdPublishedTwi .UseInboxCore(); builder.ConfigureMessageBus(h => - h.PrependDispatch(new DispatchMiddlewareConfiguration( + h.UseDispatch(new DispatchMiddlewareConfiguration( (_, next) => ctx => { ctx.MessageId = fixedMessageId; return next(ctx); }, - "ForceMessageId"))); + "ForceMessageId"), + before: "Instrumentation")); await using var bus = await builder .AddRabbitMQ() @@ -128,18 +129,18 @@ public async Task Inbox_Should_ProcessMessage_When_SkipInboxIsSet() builder.ConfigureMessageBus(h => { // Force all messages to have the same MessageId - h.PrependDispatch(new DispatchMiddlewareConfiguration( + h.UseDispatch(new DispatchMiddlewareConfiguration( (_, next) => ctx => { ctx.MessageId = fixedMessageId; return next(ctx); }, - "ForceMessageId")); + "ForceMessageId"), + before: "Instrumentation"); // Add a consumer middleware before the inbox that sets SkipInbox - h.PrependConsume( - "Inbox", + h.UseConsume( new ConsumerMiddlewareConfiguration( static (_, next) => ctx => @@ -148,7 +149,8 @@ public async Task Inbox_Should_ProcessMessage_When_SkipInboxIsSet() feature.SkipInbox = true; return next(ctx); }, - "SkipInboxCheck")); + "SkipInboxCheck"), + before: "Inbox"); }); await using var bus = await builder @@ -189,14 +191,15 @@ public async Task Inbox_Should_ProcessMessage_When_MessageIdIsNull() .UseInboxCore(); builder.ConfigureMessageBus(h => - h.PrependDispatch(new DispatchMiddlewareConfiguration( + h.UseDispatch(new DispatchMiddlewareConfiguration( (_, next) => ctx => { ctx.MessageId = null; return next(ctx); }, - "ClearMessageId"))); + "ClearMessageId"), + before: "Instrumentation")); await using var bus = await builder .AddRabbitMQ() diff --git a/src/Mocha/test/Mocha.Transport.RabbitMQ.Tests/Behaviors/TransportMiddlewareTests.cs b/src/Mocha/test/Mocha.Transport.RabbitMQ.Tests/Behaviors/TransportMiddlewareTests.cs index e2336c33690..2b4b82bd827 100644 --- a/src/Mocha/test/Mocha.Transport.RabbitMQ.Tests/Behaviors/TransportMiddlewareTests.cs +++ b/src/Mocha/test/Mocha.Transport.RabbitMQ.Tests/Behaviors/TransportMiddlewareTests.cs @@ -60,7 +60,7 @@ public async Task UseReceive_Should_InvokeOnAllEndpoints_When_ConfiguredAtTransp } [Fact] - public async Task AppendReceive_Should_InvokeOnAllEndpoints_When_ConfiguredAtTransportLevel() + public async Task UseReceive_After_Should_InvokeOnAllEndpoints_When_ConfiguredAtTransportLevel() { // arrange var tracker = new MiddlewareTracker(); @@ -74,8 +74,7 @@ public async Task AppendReceive_Should_InvokeOnAllEndpoints_When_ConfiguredAtTra .AddConsumer() .AddRabbitMQ(t => { - t.AppendReceive( - "RabbitMQAcknowledgement", + t.UseReceive( new ReceiveMiddlewareConfiguration( (ctx, next) => async context => @@ -83,7 +82,8 @@ public async Task AppendReceive_Should_InvokeOnAllEndpoints_When_ConfiguredAtTra tracker.Add("transport-append-receive"); await next(context); }, - "test-transport-append-receive")); + "test-transport-append-receive"), + after: "RabbitMQAcknowledgement"); t.DeclareExchange("ex"); t.DeclareQueue("q"); @@ -105,7 +105,7 @@ public async Task AppendReceive_Should_InvokeOnAllEndpoints_When_ConfiguredAtTra } [Fact] - public async Task PrependReceive_Should_InvokeOnAllEndpoints_When_ConfiguredAtTransportLevel() + public async Task UseReceive_Before_Should_InvokeOnAllEndpoints_When_ConfiguredAtTransportLevel() { // arrange var tracker = new MiddlewareTracker(); @@ -119,8 +119,7 @@ public async Task PrependReceive_Should_InvokeOnAllEndpoints_When_ConfiguredAtTr .AddConsumer() .AddRabbitMQ(t => { - t.PrependReceive( - "RabbitMQAcknowledgement", + t.UseReceive( new ReceiveMiddlewareConfiguration( (ctx, next) => async context => @@ -128,7 +127,8 @@ public async Task PrependReceive_Should_InvokeOnAllEndpoints_When_ConfiguredAtTr tracker.Add("transport-prepend-receive"); await next(context); }, - "test-transport-prepend-receive")); + "test-transport-prepend-receive"), + before: "RabbitMQAcknowledgement"); t.DeclareExchange("ex"); t.DeclareQueue("q"); @@ -188,7 +188,7 @@ public async Task UseDispatch_Should_InvokeOnAllEndpoints_When_ConfiguredAtTrans } [Fact] - public async Task AppendDispatch_Should_InvokeOnAllEndpoints_When_ConfiguredAtTransportLevel() + public async Task UseDispatch_After_Should_InvokeOnAllEndpoints_When_ConfiguredAtTransportLevel() { // arrange var tracker = new MiddlewareTracker(); @@ -202,8 +202,7 @@ public async Task AppendDispatch_Should_InvokeOnAllEndpoints_When_ConfiguredAtTr .AddRequestHandler() .AddRabbitMQ(t => { - t.AppendDispatch( - "Instrumentation", + t.UseDispatch( new DispatchMiddlewareConfiguration( (ctx, next) => async context => @@ -211,7 +210,8 @@ public async Task AppendDispatch_Should_InvokeOnAllEndpoints_When_ConfiguredAtTr tracker.Add("transport-append-dispatch"); await next(context); }, - "test-transport-append-dispatch")); + "test-transport-append-dispatch"), + after: "Instrumentation"); }) .BuildTestBusAsync(); @@ -227,7 +227,7 @@ public async Task AppendDispatch_Should_InvokeOnAllEndpoints_When_ConfiguredAtTr } [Fact] - public async Task PrependDispatch_Should_InvokeOnAllEndpoints_When_ConfiguredAtTransportLevel() + public async Task UseDispatch_Before_Should_InvokeOnAllEndpoints_When_ConfiguredAtTransportLevel() { // arrange var tracker = new MiddlewareTracker(); @@ -241,8 +241,7 @@ public async Task PrependDispatch_Should_InvokeOnAllEndpoints_When_ConfiguredAtT .AddRequestHandler() .AddRabbitMQ(t => { - t.PrependDispatch( - "Instrumentation", + t.UseDispatch( new DispatchMiddlewareConfiguration( (ctx, next) => async context => @@ -250,7 +249,8 @@ public async Task PrependDispatch_Should_InvokeOnAllEndpoints_When_ConfiguredAtT tracker.Add("transport-prepend-dispatch"); await next(context); }, - "test-transport-prepend-dispatch")); + "test-transport-prepend-dispatch"), + before: "Instrumentation"); }) .BuildTestBusAsync(); diff --git a/website/src/docs/mocha/v1/mediator/pipeline-and-middleware.md b/website/src/docs/mocha/v1/mediator/pipeline-and-middleware.md index b5067129844..88cdead9156 100644 --- a/website/src/docs/mocha/v1/mediator/pipeline-and-middleware.md +++ b/website/src/docs/mocha/v1/mediator/pipeline-and-middleware.md @@ -332,31 +332,30 @@ Both approaches combine well - filter out entire message kinds at compile time, # Middleware positioning -Register middleware with `Use`, `Prepend`, or `Append` to control where it sits in the pipeline. +The `Use` method accepts optional `before` and `after` parameters to control where the middleware sits in the pipeline. -| Method | Behavior | -| ----------------------------------- | --------------------------------------------------------- | -| `Use(config)` | Appends to the end of the middleware list | -| `Prepend(config)` | Inserts at the beginning | -| `Prepend("Logging", config)` | Inserts before the middleware with key `"Logging"` | -| `Append("Instrumentation", config)` | Inserts after the middleware with key `"Instrumentation"` | +| Call | Behavior | +| ------------------------------------------- | --------------------------------------------------------- | +| `Use(config)` | Appends to the end of the middleware list | +| `Use(config, before: "Logging")` | Inserts before the middleware with key `"Logging"` | +| `Use(config, after: "Instrumentation")` | Inserts after the middleware with key `"Instrumentation"` | -If the referenced key is not found, `Prepend(key, ...)` falls back to inserting at the beginning and `Append(key, ...)` falls back to appending at the end. +Only one of `before` or `after` can be specified at the same time. If the referenced key is not found, an `InvalidOperationException` is thrown at startup. ```csharp builder.Services .AddMediator() .AddCatalog() - .Use(LoggingMiddleware.Create()) // position 1 - .Use(ValidationMiddleware.Create()) // position 2 - .Use(ExceptionHandlingMiddleware.Create()) // position 3 - .Prepend("Logging", SecurityMiddleware.Create()) // before "Logging" - .Append("Logging", CorrelationIdMiddleware.Create()); // after "Logging" + .Use(LoggingMiddleware.Create()) // position 1 + .Use(ValidationMiddleware.Create()) // position 2 + .Use(ExceptionHandlingMiddleware.Create()) // position 3 + .Use(SecurityMiddleware.Create(), before: "Logging") // before "Logging" + .Use(CorrelationIdMiddleware.Create(), after: "Logging"); // after "Logging" ``` Resulting order: Security -> Logging -> CorrelationId -> Validation -> ExceptionHandling -> Handler. -The `Key` property on `MediatorMiddlewareConfiguration` is optional. Middleware without a key can still be registered with `Use` and `Prepend(config)`, but cannot be referenced by other middleware for relative positioning. +The `Key` property on `MediatorMiddlewareConfiguration` is optional. Middleware without a key can still be registered with `Use(config)`, but cannot be referenced by other middleware for relative positioning. ## Built-in middleware keys @@ -555,7 +554,7 @@ If your middleware factory returns `next` for that message type (via compile-tim ## Middleware runs in the wrong order -Middleware executes in registration order (first registered = outermost). Use `Prepend` or `Append` with a key to control placement relative to other middleware. Check that the middleware you are referencing has a `Key` set in its `MediatorMiddlewareConfiguration`. +Middleware executes in registration order (first registered = outermost). Use `Use(config, before: "key")` or `Use(config, after: "key")` to control placement relative to other middleware. Check that the middleware you are referencing has a `Key` set in its `MediatorMiddlewareConfiguration`. ## Entity Framework transactions do not wrap queries diff --git a/website/src/docs/mocha/v1/middleware-and-pipelines.md b/website/src/docs/mocha/v1/middleware-and-pipelines.md index 418d4a1121f..fe4dec9015c 100644 --- a/website/src/docs/mocha/v1/middleware-and-pipelines.md +++ b/website/src/docs/mocha/v1/middleware-and-pipelines.md @@ -198,15 +198,15 @@ builder.Services .AddMessageBus(bus => { // Insert after ReceiveInstrumentation, before DeadLetter - bus.AppendReceive( - "ReceiveInstrumentation", - LoggingReceiveMiddleware.Create()); + bus.UseReceive( + LoggingReceiveMiddleware.Create(), + after: "ReceiveInstrumentation"); }) .AddEventHandler() .AddInMemory(); ``` -`AppendReceive("ReceiveInstrumentation", ...)` places your middleware immediately after the named middleware. The receive pipeline then executes: TransportCircuitBreaker, ConcurrencyLimiter, ReceiveInstrumentation, **Logging**, DeadLetter, Fault, and so on. +`UseReceive(..., after: "ReceiveInstrumentation")` places your middleware immediately after the named middleware. The receive pipeline then executes: TransportCircuitBreaker, ConcurrencyLimiter, ReceiveInstrumentation, **Logging**, DeadLetter, Fault, and so on. # Dispatch middleware @@ -249,7 +249,9 @@ Register before all other dispatch middleware so every outgoing message carries builder.Services .AddMessageBus(bus => { - bus.PrependDispatch(TenantDispatchMiddleware.Create("acme")); + bus.UseDispatch( + TenantDispatchMiddleware.Create("acme"), + before: "Instrumentation"); }) .AddEventHandler() .AddInMemory(); @@ -282,25 +284,21 @@ The factory lambda in `ReceiveMiddlewareConfiguration`, `ConsumerMiddlewareConfi # Control middleware ordering -All ordering methods are available on `IMessageBusBuilder` for each pipeline type: - -| Method | Pipeline | Behavior | -| ------------------------------ | -------- | ----------------------------------------------- | -| `UseReceive(config)` | Receive | Add after built-in defaults | -| `PrependReceive(config)` | Receive | Insert at the beginning | -| `AppendReceive(config)` | Receive | Append at the end | -| `PrependReceive(key, config)` | Receive | Insert before the middleware with the given key | -| `AppendReceive(key, config)` | Receive | Insert after the middleware with the given key | -| `UseDispatch(config)` | Dispatch | Add after built-in defaults | -| `PrependDispatch(config)` | Dispatch | Insert at the beginning | -| `AppendDispatch(config)` | Dispatch | Append at the end | -| `PrependDispatch(key, config)` | Dispatch | Insert before the middleware with the given key | -| `AppendDispatch(key, config)` | Dispatch | Insert after the middleware with the given key | -| `UseConsume(config)` | Consumer | Add after built-in defaults | -| `PrependConsume(config)` | Consumer | Insert at the beginning | -| `AppendConsume(config)` | Consumer | Append at the end | -| `PrependConsume(key, config)` | Consumer | Insert before the middleware with the given key | -| `AppendConsume(key, config)` | Consumer | Insert after the middleware with the given key | +Each pipeline type has a single registration method with optional `before` and `after` parameters: + +| Method | Pipeline | Behavior | +| --------------------------------------------------- | -------- | ----------------------------------------------- | +| `UseReceive(config)` | Receive | Append to the pipeline | +| `UseReceive(config, before: "key")` | Receive | Insert before the middleware with the given key | +| `UseReceive(config, after: "key")` | Receive | Insert after the middleware with the given key | +| `UseDispatch(config)` | Dispatch | Append to the pipeline | +| `UseDispatch(config, before: "key")` | Dispatch | Insert before the middleware with the given key | +| `UseDispatch(config, after: "key")` | Dispatch | Insert after the middleware with the given key | +| `UseConsume(config)` | Consumer | Append to the pipeline | +| `UseConsume(config, before: "key")` | Consumer | Insert before the middleware with the given key | +| `UseConsume(config, after: "key")` | Consumer | Insert after the middleware with the given key | + +Only one of `before` or `after` can be specified at the same time. If neither is specified, the middleware is appended after the built-in defaults. Middleware is compiled once at startup into a single delegate chain. Register all middleware during bus configuration, before the service provider is built. Middleware added after the bus starts has no effect. @@ -310,9 +308,9 @@ Middleware can also be registered at transport or endpoint scope. Bus-level midd The built-in middleware in the receive pipeline implements the reliability and observability features described on their own pages: -- The `Inbox` middleware deduplicates incoming messages based on `MessageId`, described in [Reliability](/docs/mocha/v1/reliability#deduplicate-messages-with-the-transactional-inbox). It runs in the **consumer pipeline** after the transaction middleware so that the inbox claim participates in the same database transaction as the handler's business data. Use `PrependConsume` and `AppendConsume` with the `"Inbox"` key to position your middleware relative to it. -- The `CircuitBreaker` and `DeadLetter` middleware implement the circuit breaker and dead-letter behaviors described in [Reliability](/docs/mocha/v1/reliability). Use `PrependReceive` and `AppendReceive` with their keys to position your middleware relative to them. -- The `ReceiveInstrumentation` middleware generates the OpenTelemetry spans and metrics described in [Observability](/docs/mocha/v1/observability). Place logging or correlation middleware after `ReceiveInstrumentation` so telemetry context is available. +- The `Inbox` middleware deduplicates incoming messages based on `MessageId`, described in [Reliability](/docs/mocha/v1/reliability#deduplicate-messages-with-the-transactional-inbox). It runs in the **consumer pipeline** after the transaction middleware so that the inbox claim participates in the same database transaction as the handler's business data. Use `UseConsume(config, before: "Inbox")` or `UseConsume(config, after: "Inbox")` to position your middleware relative to it. +- The `CircuitBreaker` and `DeadLetter` middleware implement the circuit breaker and dead-letter behaviors described in [Reliability](/docs/mocha/v1/reliability). Use `UseReceive(config, before: "key")` or `UseReceive(config, after: "key")` with their keys to position your middleware relative to them. +- The `ReceiveInstrumentation` middleware generates the OpenTelemetry spans and metrics described in [Observability](/docs/mocha/v1/observability). Place logging or correlation middleware after `ReceiveInstrumentation` using `UseReceive(config, after: "ReceiveInstrumentation")`. # Next steps diff --git a/website/src/docs/mocha/v1/reliability.md b/website/src/docs/mocha/v1/reliability.md index 84a9251ae88..7bca2410f75 100644 --- a/website/src/docs/mocha/v1/reliability.md +++ b/website/src/docs/mocha/v1/reliability.md @@ -523,8 +523,7 @@ Set the feature from a custom consumer middleware that runs before the inbox: builder.Services .AddMessageBus(bus => { - bus.PrependConsume( - "Inbox", + bus.UseConsume( new ConsumerMiddlewareConfiguration( static (_, next) => ctx => { @@ -532,7 +531,8 @@ builder.Services feature.SkipInbox = true; return next(ctx); }, - "SkipInboxCheck")); + "SkipInboxCheck"), + before: "Inbox"); }) .AddEventHandler() .AddEntityFramework(p => @@ -543,7 +543,7 @@ builder.Services .AddRabbitMQ(); ``` -`PrependConsume("Inbox", ...)` inserts your middleware immediately before the inbox middleware in the consumer pipeline. The `InboxMiddlewareFeature` is a pooled feature that resets automatically between messages. +`UseConsume(..., before: "Inbox")` inserts your middleware immediately before the inbox middleware in the consumer pipeline. The `InboxMiddlewareFeature` is a pooled feature that resets automatically between messages. ## How the inbox cleanup worker works diff --git a/website/src/docs/mocha/v1/transports/index.md b/website/src/docs/mocha/v1/transports/index.md index 59205b5419d..2bd317418f8 100644 --- a/website/src/docs/mocha/v1/transports/index.md +++ b/website/src/docs/mocha/v1/transports/index.md @@ -79,8 +79,8 @@ builder.Services transport.UseReceive(myReceiveMiddleware); // Insert middleware relative to existing ones - transport.AppendReceive("ConcurrencyLimiter", myMiddleware); - transport.PrependDispatch("Serialization", myMiddleware); + transport.UseReceive(myMiddleware, after: "ConcurrencyLimiter"); + transport.UseDispatch(myMiddleware, before: "Serialization"); }); ```