I just updated my application to .NET Core 3.1 and fluent validation to the latest version (8.6.2) and I am getting the following error when rendering view components, which have input fields.
System.NullReferenceException: Object reference not set to an instance of an object.
at FluentValidation.AspNetCore.ValidatorDescriptorCache.GetCachedDescriptor(ClientValidatorProviderContext context, IHttpContextAccessor httpContextAccessor) in
/home/jskinner/code/FluentValidation/src/FluentValidation.AspNetCore/ValidatorDescriptorCache.cs:line 45
at FluentValidation.AspNetCore.FluentValidationClientModelValidatorProvider.CreateValidators(ClientValidatorProviderContext context) in \/home\/jskinner\/code\/FluentValidation\/src\/FluentValidation.AspNetCore\/FluentValidationClientModelValidatorProvider.cs:line 73
at Microsoft.AspNetCore.Mvc.ModelBinding.Validation.CompositeClientModelValidatorProvider.CreateValidators(ClientValidatorProviderContext context)
at Microsoft.AspNetCore.Mvc.ModelBinding.Validation.ClientValidatorCache.GetValidators(ModelMetadata metadata, IClientModelValidatorProvider validatorProvider)
at Microsoft.AspNetCore.Mvc.ViewFeatures.DefaultValidationHtmlAttributeProvider.AddValidationAttributes(ViewContext viewContext, ModelExplorer modelExplorer, IDictionary`2 attributes)
at Microsoft.AspNetCore.Mvc.ViewFeatures.ValidationHtmlAttributeProvider.AddAndTrackValidationAttributes(ViewContext viewContext, ModelExplorer modelExplorer, String expression, IDictionary`2 attributes)
at Microsoft.AspNetCore.Mvc.ViewFeatures.DefaultHtmlGenerator.AddValidationAttributes(ViewContext viewContext, TagBuilder tagBuilder, ModelExplorer modelExplorer, String expression)
at Microsoft.AspNetCore.Mvc.ViewFeatures.DefaultHtmlGenerator.GenerateInput(ViewContext viewContext, InputType inputType, ModelExplorer modelExplorer, String expression, Object value, Boolean useViewData, Boolean isChecked, Boolean setId, Boolean isExplicitValue, String format, IDictionary`2 htmlAttributes)
at Microsoft.AspNetCore.Mvc.ViewFeatures.DefaultHtmlGenerator.GenerateTextBox(ViewContext viewContext, ModelExplorer modelExplorer, String expression, Object value, String format, Object htmlAttributes)
at Microsoft.AspNetCore.Mvc.TagHelpers.InputTagHelper.GenerateHidden(ModelExplorer modelExplorer, IDictionary`2 htmlAttributes) at Microsoft.AspNetCore.Mvc.TagHelpers.InputTagHelper.Process(TagHelperContext context, TagHelperOutput output)
at Microsoft.AspNetCore.Razor.TagHelpers.TagHelper.ProcessAsync(TagHelperContext context, TagHelperOutput output)
at Microsoft.AspNetCore.Razor.Runtime.TagHelpers.TagHelperRunner.RunAsync(TagHelperExecutionContext executionContext)
at AspNetCore.Views_Waardecheck_SubscribeToEmail_SubscribeToEmail_Unsubscribed.<ExecuteAsync>b__11_0() in
The issue is that on a view component, the HttpContext
of the IHttpContextAccessor
instance is null
, and therefore giving a null reference exception when fluent validation tries to insert the validators into the pipeline. And I do have services.AddHttpContextAccessor();
in the Startup
.
This is one of the affected views. If I remove the asp-for
tag helper in the input
field then it works. But that's not really a solution.
<div data-subscribe-to-email class="waardecheck-subscribe-to-email waardecheck-subscribe-to-email--unsubscribed fd-bg-light-3 fd-padding-horizontal fd-padding-vertical-s fd-justify-content-between fd-border-light-2 fd-border-horizontal-remove fd-border-bottom-remove">
<p class="fd-margin-none fd-margin-bottom-xs">@WaardecheckResources.EnableUpdatesDescription</p>
<form asp-action="UpdateEmailPreferences">
<input asp-for="SelectedEmailFrequency" value="@Model.SelectedEmailFrequency" type="hidden">
<button type="submit" class="fd-btn fd-btn--primary-alt fd-btn--block"><span class="icon-alert-white"></span> Enable</button>
</form>
</div>
Has anybody encountered the same issue, or do you have any idea on how can I work around this?