-
Notifications
You must be signed in to change notification settings - Fork 2.1k
When creating a custom ValidationAttribute, the ValidationContext always don't have a valid ServiceContainer #6346
Comments
Are you saying that 328f4d6 does not fix this issue? It's certainly our intent that |
To be confirmed... Since I'm compiling against NET452 I can't test in the newer/beta version (2.0.0-preview1) If fixed, I believe that should be explicit mentioned in the release notes. @rynowak thanks for the follow up anyway |
It's working in new/blank project! I'm still trying to figure out why my specific solution doesn't work correctly.. I can't request any services to the ServiceProvider (always returns null). I'm using:
EDIT: OK I found the issue: I was trying to do:
but the way that works is:
API call problem 😢 - so the bug exists with the |
Yeah that's a bit of a bizzare property. I guess it's intended for you to be able to share state? Since service provider is read-only but service container is r/w. Ok, going to close this since there's no actual ASP.NET problem. Glad you got it figured out. |
Following this article: https://andrewlock.net/injecting-services-into-validationattributes-in-asp-net-core/
When creating custom
ValidationAttribute
several people already note that is impossible to access/get any service from theGetService
method inside theValidationContext
class, ServiceContainer property. Looking into the code is possible to see:https://github.com/aspnet/Mvc/blob/760c8f38678118734399c58c2dac981ea6e47046/src/Microsoft.AspNetCore.Mvc.DataAnnotations/Internal/DataAnnotationsModelValidator.cs
Since the ActionContext was obsolete: https://stackoverflow.com/questions/37286530/actioncontext-gone-in-microsoft-aspnetcore-mvc-controller - The service provider for a validation attribute is always null or empty. Let's make the
ValidationAttribute
great again, please fix this issue in the next patch.The text was updated successfully, but these errors were encountered: