Skip to content

Control order of IRequestPreProcessor and IRequestPostProcessor #885

@DanTm99

Description

@DanTm99

When registering IRequestPreProcessors and IRequestPostProcessors implicitly like the example in the readme:

services.AddMediatR(cfg => {
    cfg.RegisterServicesFromAssembly(typeof(Startup).Assembly)
    .AddOpenBehavior(typeof(MyBehavior<,>));
});

is it possible to specify the order in which they're run when there are multiple preprocessors, or multiple post processors? There doesn't seem to be anything in the wiki that allows for this.

Currently if I register them explicitly:

services.AddMediatR(cfg => {
    cfg.RegisterServicesFromAssembly(typeof(Startup).Assembly);
    .AddBehavior(typeof(IRequestPreProcessor<>), typeof(MyFirstPreProcessor<>))
    .AddBehavior(typeof(IRequestPreProcessor<>), typeof(MySecondPreProcessor<>))
    .AddOpenBehavior(typeof(MyBehavior<,>))
    .AddBehavior(typeof(IRequestPostProcessor<,>), typeof(MyFirstPostProcessor<,>))
    .AddBehavior(typeof(IRequestPostProcessor<,>), typeof(MySecondPostProcessor<,>));
})

the preprocessors and the postprocessors run twice, due to them already having been registered implicitly.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions