我在.Net核心存储库模式中做了一个CRUD操作,并试图实现FluidValidation,但我收到运行时错误“TypeLoadException: GetValidationVisitor‘in type FluentValidation.AspNetCore’TypeLoadException,Version=8.0.0.0,Culture=neutral,PublicKeyToken=7de548da2fbae0f0‘in not a implementation”。
我的代码是
public class ActivitiesService : AbstractValidator<Activity>,IActivitiesService
{
private readonly DataContext _dataContext;
public ActivitiesService(DataContext dataContext)
{
_dataContext = dataContext;
RuleFor(x => x.Title).NotNull();
RuleFor(x => x.Description).NotEmpty();
RuleFor(x => x.Category).NotEmpty();
RuleFor(x => x.Date).NotEmpty();
RuleFor(x => x.City).NotEmpty();
RuleFor(x => x.Venue).NotEmpty();
}
public async Task<AccountResult> CreateActivity(Activity activity)
{
var resultMessage = new AccountResult();
_dataContext.Activities.Add(activity);
await _dataContext.SaveChangesAsync();
return resultMessage;
}
}
ActivitiesService继承自接口
我的startup.cs代码是:
public void ConfigureServices(IServiceCollection services)
{
services.AddDbContext<DataContext>(options => options.UseSqlServer(Configuration.GetConnectionString("PreactivitiesDBConnection")));
services.AddCors(opt => {
opt.AddPolicy("CorsPolicy", policy => {
policy.AllowAnyHeader().AllowAnyMethod().WithOrigins("http://localhost:3000");
});
});
services.AddControllers().AddNewtonsoftJson(options =>
options.SerializerSettings.ReferenceLoopHandling = Newtonsoft.Json.ReferenceLoopHandling.Ignore
);
services.AddScoped<IActivitiesService, ActivitiesService>();
services.AddMediatR(typeof(List.Handler).Assembly);
services.AddMvc(option => option.EnableEndpointRouting = false).AddFluentValidation(cfg => cfg.RegisterValidatorsFromAssemblyContaining<ActivitiesService>());
}
发布于 2020-07-09 00:59:13
如此链接所示,已通过使用最新版本升级FluentValidation.AspNetCore解决了此问题。
https://stackoverflow.com/questions/62799021
复制相似问题