Skip to content

Support keyed service for variant service provider#606

Open
zhiyuanliang-ms wants to merge 1 commit into
mainfrom
zhiyuanliang/keyed-vsp
Open

Support keyed service for variant service provider#606
zhiyuanliang-ms wants to merge 1 commit into
mainfrom
zhiyuanliang/keyed-vsp

Conversation

@zhiyuanliang-ms
Copy link
Copy Markdown
Member

Why this PR?

#605 #564

VariantServiceProvider<TService> previously took IEnumerable, which forced the DI container to instantiate every registered implementation of TService at construction time — even variants that would never be selected. This PR changes it to take the IServiceProvider directly and, at runtime, checks whether a keyed service can be retrieved; if so, lazy on-demand instantiation is achieved.

Thanks @Stepami for bringing the keyed service implementation to us, and for clarifying and addressing our compatibility concerns.


private TService ResolveVariantService(string variantName)
{
if (_serviceProvider is IKeyedServiceProvider)
Copy link
Copy Markdown

@Stepami Stepami May 27, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it's behavioural BC. if there is keyed di compatible user he'll need to change his whole DI setup after update

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i think you forgot that useKeyedService option

Copy link
Copy Markdown
Member Author

@zhiyuanliang-ms zhiyuanliang-ms May 28, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes. I discussed with @jimmyca15. The discussion result is to not have useKeyedService toggle.

it's behavioural BC. if there is keyed di compatible user he'll need to change his whole DI setup after update

This is not a breaking change, because we have fallback logic, it will first try to get keyed service and if there is no keyed service match the variant name, it will still use the previous way (GetRequiredService<IEnumerable>()) to get all implementation and inspect them one by one.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants