[Hosting] Register ImageSource services as KeyedServices#19641
Closed
mdh1418 wants to merge 33 commits intodotnet:net9.0from
Closed
[Hosting] Register ImageSource services as KeyedServices#19641mdh1418 wants to merge 33 commits intodotnet:net9.0from
mdh1418 wants to merge 33 commits intodotnet:net9.0from
Conversation
|
Hey there @mdh1418! Thank you so much for your PR! Someone from the team will get assigned to your PR shortly and we'll get it reviewed. |
Member
|
/azp run |
|
Azure Pipelines successfully started running 3 pipeline(s). |
Member
|
Can we get a little more context about this on the PR description? |
Member
Author
|
@rmarinho Yes, sorry it is still a work in progress, and I haven't had time to fill out the description yet. |
Member
|
Can you target the |
eb696b4 to
d8e7c66
Compare
With keyed services introduced in net8.0, register the default IImageSourceServices as keyed services directly through the MauiApp's ServiceProvider's ServiceCollection in preparation to obviate ImageSourceServiceProvider logic.
Tests leverage ContextStub instead of MauiContext. With ImageSourceServices registered as keyed services, ContextStub needs to implement IKeyedServiceProvider in order for tests to properly grab keyed image sources.
Member
Author
|
/azp run |
|
Azure Pipelines successfully started running 3 pipeline(s). |
Comment on lines
+135
to
+136
| if (context.Services is not IKeyedServiceProvider provider) | ||
| throw new InvalidOperationException($"Context services {nameof(context.Services)} does not implement {nameof(IKeyedServiceProvider)}"); |
Member
There was a problem hiding this comment.
@PureWeen can/should we change context.Services to an IKeyedServiceProvider? (it extends IServiceProvider) Then we wouldn't need to cast/throw here?
The cast/throw occurs a couple places in this PR.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to subscribe to this conversation on GitHub.
Already have an account?
Sign in.
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Description of Change
This PR aims to overhaul the
ImageSourceServiceProvider-based ImageSource Service registration setup in favor of leveraging Keyed Services introduced in .NET 8 to achieve a more trimmer friendly solution.ImageSourceServiceProviderwas initially introduced to be able to retrieve various derivations of ImageSourceService of which each would interact with its own derivation of an ImageSource (i.e.FileImageSourceService : ImageSourceService, IImageSourceService<IFileImageSource>). Thus, for the ImageSourceServiceProvider to grab these services, it would need to resolve an ImageSource to the correspondingIImagesourceService<>generic that the service had been registered with via trimming unfriendlySystem.Type.MakeGenericTypemaui/src/Core/src/Hosting/ImageSources/ImageSourceServiceProvider.cs
Lines 25 to 59 in 61ca414
Instead, leveraging Keyed Services to register and retrieve each
ImageSourceServicewith a service key such as the underlying ImageSource typeIFileImageSource, renders the logic to extrapolate anIImageSourceService<>generic from an ImageSource type unnecessary (one could grab the ImageSourceService viaGetRequiredKeyedService<IImageSourceService>(typeof(IFileImageSource))). As a result, theImageSourceServices can be registered directly through the MauiApp's ServiceCollection, making the ImageSourceServiceProvider class unnecessary.This PR overhauls the
ImageSourceServiceProvider-based ImageSource Service registration setup by:GetRequiredKeyedService/GetKeyedServicecallsIssues Fixed
Fixes
ImageSourceServiceProvidertrimming warnings mentioned in #19397. Once this PR is merged, the number of warnings tracked byPublishNativeAOTCheckWarningsintroduced in #19194 should decrease.