[英].NET Core 3.1 custom model validation with fluentvalidation
[英].NET Core - FluentValidation inject
我正在創建ASP.NET CORE(2.1)項目,並嘗試使用FluentValidation庫( https://fluentvalidation.net/ )。 不幸的是,我在向ApiController發送請求時遇到錯誤。
處理請求時發生未處理的異常。
InvalidOperationException:嘗試激活“ Spinner.Features.Estimates.OnPost + CommandValidator”時,無法解析類型為“ Core.Entities.Estimate”的服務。 Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteFactory.CreateArgumentCallSites(Type serviceType,Type ImplementationType,CallSiteChain callSiteChain,ParameterInfo [] parameters,bool throwIfCallSiteNotFound)
這是我要驗證和驗證模型的類:
public class Command : IRequest<AcceptCostEstimateResponse>
{
public EstimateDTO Estimate { get; set; }
public ClientHeaderDTO ClientHeader { get; set; }
}
public class CommandValidator : AbstractValidator<Command>
{
public CommandValidator(Estimate configuration)
{
RuleFor(x => x.Estimate).NotNull();
RuleFor(x => x.Estimate.Name).NotEmpty();
RuleFor(x => x.Estimate.Variant).NotEmpty();
RuleFor(x => x.ClientHeader.ClientName).NotEmpty();
RuleFor(x => x.ClientHeader.RequestId).NotEmpty();
}
}
這是我的Startup.cs-我依賴項注入配置所在的位置:
public void ConfigureServices(IServiceCollection services)
{
services.AddMvc()
.SetCompatibilityVersion(CompatibilityVersion.Version_2_1)
.AddFluentValidation(fv => fv.RegisterValidatorsFromAssemblyContaining<Startup>());
services.AddAutoMapper();
services.AddMediatR();
services.AddDbContextPool<SpinnerContext>(options => options.UseSqlServer(Configuration.GetConnectionString("DefaultConnection")));
ConfigureCore(services);
ConfigureRepositories(services);
}
我也嘗試以其他方式配置CommandValidator,如下所示,但這仍然無濟於事:
services.AddMvc()
.SetCompatibilityVersion(CompatibilityVersion.Version_2_1)
.AddFluentValidation();
services.AddTransient<IValidator<Command>, CommandValidator>();
我在CommandValidator構造函數中刪除了Estimate配置參數,現在得到了不同的錯誤。 處理請求時發生未處理的異常。 NullReferenceException:對象引用未設置為對象的實例。
對於此錯誤,這是由於沒有Estimate
或ClientHeader
值而導致的,它們將為null, FluentValidation
將嘗試訪問其屬性。
嘗試以下選項:
更改Command
以設置子屬性。
public class Command : IRequest<AcceptCostEstimateResponse> { public Command() { Estimate = new EstimateDTO(); ClientHeader = new ClientHeaderDTO(); } public EstimateDTO Estimate { get; set; } public ClientHeaderDTO ClientHeader { get; set; } }
僅當CommandValidator
不為null時,才將其更改為驗證子屬性。
public CommandValidator() { //RuleFor(x => x.Estimate).NotNull(); //RuleFor(x => x.Estimate.Name).NotEmpty(); //RuleFor(x => x.Estimate.Variant).NotEmpty(); //RuleFor(x => x.ClientHeader.ClientName).NotEmpty(); //RuleFor(x => x.ClientHeader.RequestId).NotEmpty(); RuleFor(x => x).NotNull(); When(x => x != null, () => { RuleFor(x => x.Estimate).NotNull(); When(x => x.Estimate != null, () => { RuleFor(x => x.Estimate.Name).NotEmpty(); RuleFor(x => x.Estimate.Variant).NotEmpty(); }); RuleFor(x => x.ClientHeader).NotNull(); When(x => x.ClientHeader != null, () => { RuleFor(x => x.ClientHeader.ClientName).NotEmpty(); RuleFor(x => x.ClientHeader.RequestId).NotEmpty(); }); }); }
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.