The Wayback Machine - https://web.archive.org/web/20220108060605/https://github.com/dotnet/aspnetcore/issues/39382
Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Can't inject service in ValidationAttribute #39382

Open
1 task done
sipi41 opened this issue Jan 8, 2022 · 0 comments
Open
1 task done

Can't inject service in ValidationAttribute #39382

sipi41 opened this issue Jan 8, 2022 · 0 comments

Comments

@sipi41
Copy link

@sipi41 sipi41 commented Jan 8, 2022

Is there an existing issue for this?

  • I have searched the existing issues

Describe the bug

Dear friends, yes, it's me again to bother you again! :-(

I love learning and found an article that woke up my mind, it was about creating a custom validation attribute (ValidationAttribute) but trying to inject a service (X) in order to get access to data or a method, etc... the idea sounds like a real project and I decided to give it a try... unfortunately every time I try to get a service I just get NULL... and yes, I tried to get some help from the MS help but the documentation is poor, no examples. (except a simple one where you learn how to override the IsValid method)

I created a simple class with a list of names and a method... the idea is that the Validation Attribute inject this as a service and then may ask the method if the name is valid or not... (if is on the list):

public class NamesService
{
public List Names { get; set; } = new() { "Guillermo", "Cinthy" };
public bool IsAValidName(string Name) {
return Names.Contains(Name);
}
}

Then I basically injected this as an Scoped service on my program.cs as follow:

...
builder.Services.AddScoped();
...

I did try to inject the service on a regular class and it works, also on a Blazor page and it works also... now, I created another class where I tried in 2 ways:

  1. Using the IValidatableObject like this, and you can see I made a breakpoint and the value is NULL:

image

  1. Ok, so I created a Custom Validation Attribute and the result is the same, like this:

image

Now I noticed several other options on the context, like:

  • validationContext.InitializeServiceProvider
  • validationContext.CreateAsyncScope

But no examples or what's the purpose or how to use these options? or will this help resolve the problem I'm facing?

Thank you for your help, this will help with what's being said all around... https://andrewlock.net/injecting-services-into-validationattributes-in-asp-net-core/

Expected Behavior

No response

Steps To Reproduce

No response

Exceptions (if any)

No response

.NET Version

6

Anything else?

No response

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Linked pull requests

Successfully merging a pull request may close this issue.

None yet
1 participant