亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

ASP.NET Core API,Mediatr 發送方法引發無法解析類型服務的異常

ASP.NET Core API,Mediatr 發送方法引發無法解析類型服務的異常

C#
蝴蝶不菲 2023-04-29 16:54:23
我有一個 ASP.NET Core API,我正在嘗試將 FluentValidation 與 Mediatr 一起使用。當前,當控制器方法嘗試在它生成的 mediatr 實例上調用 Send 時:拋出的異常:Microsoft.Extensions.DependencyInjection.dll 中的“System.InvalidOperationException”:“嘗試激活‘GetApplicationQueryValidator’時無法解析‘GetApplicationQuery’類型的服務?!辈樵?、驗證器和響應類如下所示:public class GetApplicationQuery : IRequest<Response>{    private string _name;    public GetApplicationQuery(string name)    {        _name = name;    }    public string Name { get { return _name; } }}public class GetApplicationQueryHandler : IRequestHandler<GetApplicationQuery, Response>{    public GetApplicationQueryHandler() { }    public async Task<Response> Handle(GetApplicationQuery request, CancellationToken cancellationToken)    {        return new Response("yadda yadda");                }}public class GetApplicationQueryValidator : AbstractValidator<GetApplicationQuery>{    public GetApplicationQueryValidator(GetApplicationQuery request)    {           RuleFor(m => m.Name).MinimumLength(30).WithMessage("Name must be greater than 30 characters, long");    }}public class Response{    private readonly IList<string> _messages = new List<string>();    public IEnumerable<string> Errors { get; }    public object Result { get; }    public Response() => Errors = new ReadOnlyCollection<string>(_messages);    public Response(object result) : this() => Result = result;    public Response AddError(string message)    {        _messages.Add(message);        return this;    }}我在 Startup 類中的配置如下所示:public void ConfigureServices(IServiceCollection services){    AddMediatr(services);    services.AddMvc().AddFluentValidation(fv =>         {            fv.RegisterValidatorsFromAssemblyContaining<Startup>();            fv.RunDefaultMvcValidationAfterFluentValidationExecutes = false;        }).SetCompatibilityVersion(CompatibilityVersion.Version_2_2);           }
查看完整描述

1 回答

?
偶然的你

TA貢獻1841條經驗 獲得超3個贊

GetApplicationQueryValidator正在GetApplicationQuery作為構造函數依賴項,但集合不知道它是否能夠注入它。


也沒有看到它是如何在該驗證器中使用的。我建議GetApplicationQuery從構造函數中刪除,因為它看起來并不需要。


public class GetApplicationQueryValidator : AbstractValidator<GetApplicationQuery> {

    public GetApplicationQueryValidator() { 

        RuleFor(m => m.Name).MinimumLength(30).WithMessage("Name must be greater than 30 characters, long");

    }

}


查看完整回答
反對 回復 2023-04-29
  • 1 回答
  • 0 關注
  • 198 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號