gpt4 book ai didi

c# - MediatR 行为的约束违反异常

转载 作者:行者123 更新时间:2023-11-30 12:38:29 27 4
gpt4 key购买 nike

我正在使用 MediatR具有以下类:

public class GetPostsRequest : IRequest<Envelope<GetPostsResponse>> {
public Int32 Age { get; set; }
}

public class GetPostResponse {
public String Title { get; set; }
public String Content { get; set; }
}

其中 Envelope 是一个包装类:

public class Envelope<T> {
public List<T> Result { get; private set; } = new List<T>();
public List<Error> Errors { get; private set; } = new List<Error>();
}

中介者发送的 GetPostsRequest 由 Handler 执行:

public class GetPostsRequestHandler : IRequestHandler<GetPostsRequest, Envelope<GetPostsResponse>> {

public async Task<Envelope<GetPostsResponse>> Handle(GetPostsRequest request, CancellationToken cancellationToken) {
}

}

MediatR 允许使用在特定请求的处理程序之前执行的行为。我创建了一个 ValidationBehavior如下:

public class ValidationBehavior<TRequest, TResponse> : IPipelineBehavior<TRequest, Envelope<TResponse>> 
where TRequest : IRequest<Envelope<TResponse>> {

private readonly IEnumerable<IValidator<TRequest>> _validators;

public ValidationBehavior(IEnumerable<IValidator<TRequest>> validators) {
_validators = validators;
}

public Task<Envelope<TResponse>> Handle(TRequest request, CancellationToken cancellationToken, RequestHandlerDelegate<Envelope<TResponse>> next) {

ValidationContext context = new ValidationContext(request);

List<Error> errors = _validators
.Select(x => x.Validate(context))
.SelectMany(x => x.Errors)
.Select(x => new Error(ErrorCode.DataNotValid, x.ErrorMessage, x.PropertyName))
.ToList();

if (errors.Any())
return Task.FromResult<Envelope<TResponse>>(new Envelope<TResponse>(errors));

return next();

}

}

然后我在 ASP.NET Core 应用程序上注册了 ValidationBehavior:

services.AddScoped(typeof(IPipelineBehavior<,>), typeof(ValidationBehavior<,>));

当我调用 API 时,出现以下错误:

An unhandled exception has occurred while executing the request.
System.ArgumentException: GenericArguments[0], 'TRequest', on 'ValidationBehavior`2[TRequest,TResponse]' violates the constraint of type 'TRequest'.
---> System.TypeLoadException: GenericArguments[0], 'TRequest', on 'ValidationBehavior`2[TRequest,TResponse]' violates the constraint of type parameter 'TRequest'.

我错过了什么?

最佳答案

services.AddScoped(typeof(IPipelineBehavior<,>), typeof(ValidationBehavior<,>));

我认为这条线没有达到您的预期。

在您的示例中,您的请求类型是 IRequest<Envelope<Response>> ,所以当 MediatR 寻找管道行为时,它会寻找 IPipelineBehavior<Request, Envelope<Response>> .

因为 IPipelineBehavior<,> 在 DI 容器中有一个开放式通用注册,将使用该实现。所以来自 IPipelineBehavior<,> 的通用类型参数将用于实例化 ValidationBehavior<,> 的类型.

所以对于 IPipelineBehavior<Request, Envelope<Response>> , 这将创建一个 ValidationBehavior<Request, Envelope<Response>> .这意味着在您的通用实现中,TRequest将是 Request , 和 TResponse将是 Envelope<Response> .然而,这意味着您的类型上的类型限制意味着 TRequest , 或 Request在这种情况下,需要实现 IRequest<Envelope<Envelope<Response>>> .当然,情况并非如此,因此可以解释您所看到的类型约束冲突。

不幸的是,这意味着您无法按照您希望的方式进行操作。您不能对泛型类型施加类型约束,至少不能为 Envelope<T> 设置类型约束。 .

关于c# - MediatR 行为的约束违反异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52080415/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com