jbogard / MediatR

Simple, unambitious mediator implementation in .NET
Apache License 2.0
11.06k stars 1.17k forks source link

IPipelineBehavior not being executed when I have custom interface #1002

Closed roozbeh-hoseiny closed 5 months ago

roozbeh-hoseiny commented 7 months ago

I have these interfaces and classes :

public interface IPrimitiveResultCommand<TResponse> : IRequest<PrimitiveResult<TResponse>> { }

public interface IPrimitiveResultCommandHandler<TQuery, TResponse> : IRequestHandler<TQuery, PrimitiveResult<TResponse>>
    where TQuery : IPrimitiveResultCommand<TResponse>
{ }

public sealed class ValidationBehaviour<TRequest, TResponse> : IPipelineBehavior<TRequest, PrimitiveResult<TResponse>>
    where TRequest : IApiRequest<TRequest>
{
    public async Task<PrimitiveResult<TResponse>> Handle(TRequest request, RequestHandlerDelegate<PrimitiveResult<TResponse>> next, CancellationToken cancellationToken)
    {
        return await (
            await PrimitiveMaybe.PrimitiveMaybe
            .From(request)
            .Match(
                request =>
                    PrimitiveResult.Success(request)
                    .Bind(_ => _.Validate())
                    .Match(
                        validatedRequest => next(),
                        validationError => PrimitiveResult.Failure<TResponse>(validationError)
                    ),
                () => ValueTask.FromResult(PrimitiveResult.Failure<TResponse>("Validation.Error", "Bad Request. the api request is null"))
            ).ConfigureAwait(false))
            .Match(
                success => success,
                error => PrimitiveResult.Failure<TResponse>(error))
            .ConfigureAwait(false);
    }
}

I have registered this "ValidationBehaviour" like this :

services.AddMediatR(config => {
    config.RegisterServicesFromAssembly(Assembly.GetExecutingAssembly());

    config.AddBehavior(typeof(IPipelineBehavior<,>), typeof(ValidationBehaviour<,>));
});

but when I wanna send a command i get this error :

System.ArgumentException: 'Implementation type 'ConsoleApp1.AppCore.Presentation.Features.UserFeratures.ValidationBehaviour`2[ConsoleApp1.AppCore.Presentation.Features.UserFeratures.AddUserCommandApiRequest,PrimitiveResults.PrimitiveResult`1[Dpi.Ports.IamService.Domain.Entities.UserEntity.User]]' can't be converted to service type 'MediatR.IPipelineBehavior`2[ConsoleApp1.AppCore.Presentation.Features.UserFeratures.AddUserCommandApiRequest,PrimitiveResults.PrimitiveResult`1[Dpi.Ports.IamService.Domain.Entities.UserEntity.User]]''
github-actions[bot] commented 5 months ago

This issue is stale because it has been open 60 days with no activity. Remove stale label or comment or this will be closed in 14 days.

github-actions[bot] commented 5 months ago

This issue was closed because it has been stalled for 14 days with no activity.