We have a custom DateTimeModelBinder that used to work fine with ASP.NET Core 1.1, but after upgrading to 2.2, it fails. This is the breaking part:
bindingContext.ModelState.TryAddModelError(
bindingContext.ModelName,
bindingContext.ModelMetadata.ModelBindingMessageProvider.ValueMustNotBeNullAccessor(
valueProviderResult.ToString()));
The error is:
Method not found: 'Microsoft.AspNetCore.Mvc.ModelBinding.Metadata.IModelBindingMessageProvider Microsoft.AspNetCore.Mvc.ModelBinding.ModelMetadata.get_ModelBindingMessageProvider()'.
The failing part is:
bindingContext.ModelMetadata.ModelBindingMessageProvider
Obviously something has changed. I found this message explaining a bit about this breaking change, but all of it:
https://github.com/aspnet/Announcements/issues/240
Apparently we have to use DefaultModelBindingMessageProvider
now, but how does the whole application share a common one, inluding model binders?
Problem is, in ModelBinders, the bindingContext.ModelMetadata.ModelBindingMessageProvider
used to poing at a class containing the strings, but now is pointing to the abstract class and hence fails with the above error.
Using this instead works, but newing up this class for each use seems wrong (also, changes to the translations would not be reflected).
bindingContext.ModelState.TryAddModelError(
bindingContext.ModelName,
new DefaultModelBindingMessageProvider().ValueMustNotBeNullAccessor(
valueProviderResult.ToString()));
I feel I should be able to set the ModelBindingMessageProvider
property of the ModelMetadata
in the bindingContext
in my Startup.cs, but haven't found out how (or if I am on the right track). Am I?