Skip to content

Support keyed service for variant service provider#606

Merged
zhiyuanliang-ms merged 6 commits into
mainfrom
zhiyuanliang/keyed-vsp
Jun 2, 2026
Merged

Support keyed service for variant service provider#606
zhiyuanliang-ms merged 6 commits into
mainfrom
zhiyuanliang/keyed-vsp

Conversation

@zhiyuanliang-ms
Copy link
Copy Markdown
Member

@zhiyuanliang-ms zhiyuanliang-ms commented May 27, 2026

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.

Copy link
Copy Markdown

@Stepami Stepami May 28, 2026

Choose a reason for hiding this comment

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

This disrupts backward compatibility.

Let's say I am net8 variant sp user. Till now my services were registered like this:

services.AddSingleton<IFeature, Variant1>();
services.AddSingleton<IFeature, Variant2>();

After I get the update my setup won't work as variant sp will use keyed di because it's available by default in net8. This will make me change DI setup to keyed just to have it working.

Am I wrong in BC definition?

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 just realized i read code wrong, sorry for misunderstanding

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

No worries. How do you like the current implementation? :)

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Honestly, good enough to me! I think it's great you guys found the right spot to have it both: keyed di and fallback.

The thing i'd like to improve is code readability. IMHO VariantServiceProvider's complexity is increasing now so it's crucial to be able mantaining it well. You saw how i got it wrong first time reading :)

So i updated #605 to give my small ideas on how to do it

Copy link
Copy Markdown
Member Author

@zhiyuanliang-ms zhiyuanliang-ms May 29, 2026

Choose a reason for hiding this comment

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

Updated my PR and also includes the change to support "feature status service". Check the updated PR description please.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Hey, @Stepami

I reverted the change of adding VariantServiceProviderOptions. Whether to add a new API WithFeaturedService is still not decided (our internal dicussion is still not finalized). This PR will only be scoped at supporting keyed service.

The thing i'd like to improve is code readability. IMHO VariantServiceProvider's complexity is increasing now so it's crucial to be able mantaining it well. You saw how i got it wrong first time reading :)

I added some comments for better readability.

@zhiyuanliang-ms zhiyuanliang-ms merged commit 02f4c2d into main Jun 2, 2026
5 checks passed
@Stepami Stepami mentioned this pull request Jun 4, 2026
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.

3 participants