[release/8.0] [Blazor] Support injecting keyed services with [Inject] by MackinnonBuck · Pull Request #50561 · dotnet/aspnetcore (original) (raw)

Good point. The thing is, though, that ComponentFactory in its current state isn't limited to using a single IServiceProvider instance, so it might not always be correct to verify the existence of a service on the first IServiceProvider and reuse that result for future IServiceProviders.

Also, it might happen that we create/cache a property injector but never invoke it, in which case we throw an exception unnecessarily. For example, it's possible that a component could have an interactive render mode that disables prerendering, so the component never actually gets rendered by the endpoint renderer (and we hit that final else block in InstantiateComponent()). In this case, the component might require a service unavailable during prerendering but available during interactive rendering, and we would throw an exception unnecessarily.

So, I'm thinking it might be best to leave the validation at the point when [Inject] properties are populated. Does that seem reasonable?