Post processor MediatR

39 views Asked by At

I'm trying to use the post processor in MediatR, but it doesn't work in either the Pre processor or the Post processor. That's how I injected the post and pre processor, but apparently not properly.

1

There are 1 answers

1
Engin Ozsozgun On
public class MyPreProcessor<TRequest> : IRequestPreProcessor<TRequest>
{
    public Task Process(TRequest request, CancellationToken cancellationToken)
    {
        
        Console.WriteLine("Pre-processing logic executed");
        return Task.CompletedTask;
    }
}

public class MyPostProcessor<TRequest, TResponse> : IRequestPostProcessor<TRequest, TResponse>
{
    public Task Process(TRequest request, TResponse response, CancellationToken cancellationToken)
    {
        
        Console.WriteLine("Post-processing logic executed");
        return Task.CompletedTask;
    }
}   

public void ConfigureServices(IServiceCollection services)
{
            
    services.AddTransient(typeof(IPipelineBehavior<,>), typeof(MyPreProcessor<>));
    services.AddTransient(typeof(IPipelineBehavior<,>), typeof(MyPostProcessor<,>));

    
    services.AddMediatR(typeof(Startup).Assembly);
}
   
public class Startup
{
    public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
    {
               
        app.UseMediatR();
    }
}