I am creating ASP.NET CORE (2.1) project and trying to use FluentValidation library (https://fluentvalidation.net/). Unfortunately I am getting an error when I am sending request to my ApiController.
An unhandled exception occurred while processing the request.
InvalidOperationException: Unable to resolve service for type 'Core.Entities.Estimate' while attempting to activate 'Spinner.Features.Estimates.OnPost+CommandValidator'. Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteFactory.CreateArgumentCallSites(Type serviceType, Type implementationType, CallSiteChain callSiteChain, ParameterInfo[] parameters, bool throwIfCallSiteNotFound)
Here is the class where I have my model to validate and validator:
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();
}
}
Here is my Startup.cs - place where I dependency injectio configuration exists:
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);
}
I also tried to configure CommandValidator differently, something like this, but still this didn't help:
services.AddMvc()
.SetCompatibilityVersion(CompatibilityVersion.Version_2_1)
.AddFluentValidation();
services.AddTransient<IValidator<Command>, CommandValidator>();