Skip to content

Commit

Permalink
Ensure providers aren't registered twice
Browse files Browse the repository at this point in the history
  • Loading branch information
JeremySkinner committed Feb 29, 2020
1 parent a79751f commit 7776bff
Showing 1 changed file with 13 additions and 2 deletions.
15 changes: 13 additions & 2 deletions src/FluentValidation.AspNetCore/FluentValidationMvcExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ namespace FluentValidation.AspNetCore {
using Microsoft.Extensions.Options;
using FluentValidation;
using System.Collections.Generic;
using System.Linq;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.DependencyInjection.Extensions;
using Microsoft.Extensions.Logging;
Expand All @@ -48,8 +49,18 @@ public static class FluentValidationMvcExtensions {
RegisterServices(mvcBuilder.Services, config);

mvcBuilder.AddMvcOptions(options => {
options.ModelMetadataDetailsProviders.Add(new FluentValidationBindingMetadataProvider());
options.ModelValidatorProviders.Insert(0, new FluentValidationModelValidatorProvider(config.ImplicitlyValidateChildProperties));
// Check if the providers have already been added.
// We shouldn't have to do this, but there's a bug in the ASP.NET Core integration
// testing components that can cause Configureservices to be called multple times
// meaning we end up with duplicates.
if (!options.ModelMetadataDetailsProviders.Any(x => x is FluentValidationBindingMetadataProvider)) {
options.ModelMetadataDetailsProviders.Add(new FluentValidationBindingMetadataProvider());
}
if (!options.ModelValidatorProviders.Any(x => x is FluentValidationModelValidatorProvider)) {
options.ModelValidatorProviders.Insert(0, new FluentValidationModelValidatorProvider(config.ImplicitlyValidateChildProperties));
}
});

return mvcBuilder;
Expand Down

0 comments on commit 7776bff

Please sign in to comment.