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

Rectify the scopes in the builder #19932

Merged
merged 1 commit into from
Jan 24, 2024
Merged

Rectify the scopes in the builder #19932

merged 1 commit into from
Jan 24, 2024

Conversation

mattleibow
Copy link
Member

@mattleibow mattleibow commented Jan 16, 2024

Description of Change

An alternate alternative to #18492 and an alternative to #19593

Alteratives:

This PR makes sure that our scopes are correct:

  • The appliation is the root and is not a scope.
  • There is a unique scope for each window.

I would have preferred to have a singleton service for the app and a scoped service for window, but it appears the default DI container does not support the same type as BOTH a singleton AND a scoped. One thought would be to use keyed services but this does not yet appear to be 100% supported in the IoC container libraries that are used in a MAUI app. For now we just register a placeholder type to hold the singleton dispatcher and we use that.

The issue is that an app may not only have different dispatchers per window, but also for the app. In all existing apps today, there is a single dispatcher for all things. However, WinUI used to - and may in future - have different dispatchers for each window.

This could mean that at startup, the app and window have the same dispatcher, but if a new window was created, it would have a new, second dispatcher. And if the first window was closed, the app would retain the first dispatcher. As a result, we could have different dispatchers for the app and for the window. This is academic now since WinUI dropped the support for different window-threads.

Issues Fixed

@mattleibow mattleibow requested a review from a team as a code owner January 16, 2024 21:16
@mattleibow mattleibow force-pushed the dev/fix-scopes branch 3 times, most recently from c3e3c59 to a1fe5eb Compare January 17, 2024 15:34
Comment on lines -51 to +55
var window = new MauiWindow(Services.GetRequiredService<Page>())
var services = activationState!.Context.Services;
var window = new MauiWindow(services.GetRequiredService<Page>())
Copy link
Member Author

Choose a reason for hiding this comment

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

This has to be changed because the IServiceProvider in the app's ctor is actually the root povider and does not have a window dispatcher.

The page uses a VM that uses the dispatcher. However, the services coming in here via the activation state is a window provider and does have the dispatcher.

@mattleibow mattleibow force-pushed the dev/fix-scopes branch 5 times, most recently from 312a09f to 0994b7a Compare January 19, 2024 18:41
@Eilon Eilon added the area-core-hosting Extensions / Hosting / AppBuilder / Startup label Jan 20, 2024
@mattleibow mattleibow marked this pull request as draft January 22, 2024 13:48
@mattleibow
Copy link
Member Author

This PR needs to be merged AFTER #20014 as I want to use keyed services for most cases but also add a fallback for services that do not support keyed services

@mattleibow mattleibow force-pushed the dev/fix-scopes branch 2 times, most recently from 64fd3d6 to 03dfa96 Compare January 22, 2024 16:36
@mattleibow mattleibow marked this pull request as ready for review January 22, 2024 16:36
@mattleibow mattleibow requested a review from PureWeen January 22, 2024 16:38
Copy link
Member

@PureWeen PureWeen left a comment

Choose a reason for hiding this comment

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

I tested this PR against a version of autofac without keyedservices and I get this exception

image

You can reproduce here

dev/fix-scopes-autofac-test

@mattleibow
Copy link
Member Author

mattleibow commented Jan 23, 2024

ok, i guess for net 8 we can't even register keyed services then. Makes me sad, but we will have to use the internal/hidden thing for now.

I am keeping the lookup for keyed services so it is actually overridable - like in our tests - but it won't use it by default.

@mattleibow mattleibow force-pushed the dev/fix-scopes branch 4 times, most recently from 6e22a10 to 10e90b1 Compare January 23, 2024 17:40
return scopedContext;
}

public static IMauiContext MakeWindowScope(this IMauiContext mauiContext, NativeWindow platformWindow, out IServiceScope scope)
{
// Create the window-level scopes that will only be used for the lifetime of the window
// TODO: We need to dispose of these services once the window closes
Copy link
Member

Choose a reason for hiding this comment

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

FYI #8538

@mattleibow
Copy link
Member Author

/rebase

@PureWeen
Copy link
Member

/rebase

@PureWeen PureWeen enabled auto-merge (squash) January 24, 2024 12:01
@PureWeen PureWeen merged commit be96a72 into main Jan 24, 2024
47 checks passed
@PureWeen PureWeen deleted the dev/fix-scopes branch January 24, 2024 12:01
rmarinho added a commit that referenced this pull request Jan 29, 2024
* Add Obsolete tag for old IndexOf (#20068)

* Split the InputTransparency tests (#19925)

* Bump Xamarin.UITest to 4.3.4 (#20067)

* Bump Xamarin.UITest to 4.3.4

* Update NUnit

* Restore dotnet tool (#20106)

* Do not use underscores in the ApplicationId (#19377)

* Do not use underscores in the ApplicationId

Underscores are not supported on Windows

* Update DotnetInternal.cs

* [WinUI] Add workaround for Connectivity check on Win10  (#19261)

* Implemented a Win10 work-around for connectivity thread issue

Reimplement `ConnectivityImplementation.ConnectionProfiles` to use native .net core APIs

* Remove prop bag ID

* Replace network availability changed event with native .net API

* Remove explicit file include

* Fix file naming

* Implemented a Win10 work-around for connectivity thread issue

Reimplement `ConnectivityImplementation.ConnectionProfiles` to use native .net core APIs

* Remove prop bag ID

* Replace network availability changed event with native .net API

* Remove explicit file include

* Fix file naming

* * Revert change to remove `Windows.Networking.Connectivity.NetworkInformation.NetworkStatusChanged`

* Update Connectivity.uwp.cs

---------

Co-authored-by: Mike Corsaro <mikecorsaro@microsoft.com>
Co-authored-by: Matthew Leibowitz <mattleibow@live.com>

* [Windows] Adjust recycle check in ItemContentControl (#20079)

* Adjust recycle check in `ItemContentControl` to use Content property

* Fix confusing line

* Rectify the scopes in the builder (#19932)

* Update System.Drawing.Common (#20122)

* Enable building WASDK Self-Contained packaged apps (#20019)

* Translucent and Transparent NavigationBar on iOS (#19204)

* Changes for enabling translucent navigation bar ios

* Add UITest for NavigationPage Safe Area Translucence

* remove UIKit

* Move UITest from gallery to Issues

* push a new page for UITests, consolidate code, and save shadowimage

* Changes for enabling translucent navigation bar ios

* Add UITest for NavigationPage Safe Area Translucence

* remove UIKit

* Move UITest from gallery to Issues

* make the extension method

* use the background color alpha for translucent

* use same alpha logic for pre ios 15

* accidently added testing comments

* add more UITests for the different NavigationPage and Flyout Page scenarios

* use additionalsafeareainsets for the secondary toolbar

* missing new line

* only run the secondary toolbar offset when we have a secondary toolbar

* use existing childViewControllers

* remove the shadow stuff and simplify the expression with null

* style and fixes from merge conflicts

* standardAppearance and scrolledgeappearance should be kept in

* changes after talking with Shane

* change shell behavior to be more like navrenderer with preiOS13

* move code if we are transparent pre13 shell

* remove new lines

* add screenshot tests

* be able to remove and add secondarybar additionalsafeareas

* reset the xaml on the Sandboc MainPage (#20082)

* [Android] Fix gif animation initial state (#14295)

* Fix gifs initial animation state on Android

* Device tests

* Auto-format source code

* Updated sample

* Updated device tests

* Refactor code

* Update src/Core/src/Handlers/Image/ImageHandler.Android.cs

Co-authored-by: Matthew Leibowitz <mattleibow@live.com>

* Remove unnecessary change

* Fix build errors

* Merge branch 'main' into fix-7019

* Fix merge mistake

---------

Co-authored-by: GitHub Actions Autoformatter <autoformat@example.com>
Co-authored-by: Javier Suárez <6755973+jsuarezruiz@users.noreply.github.com>
Co-authored-by: Matthew Leibowitz <mattleibow@live.com>
Co-authored-by: Gerald Versluis <gerald.versluis@microsoft.com>

* Enable Windows Image device tests (#20167)

* [X] allow x:Type extension for BPConverter (#18540)

- fixes #18324

* Source Generator Performance Improvements (#19990)

* Source Generator Performance Improvements:
- Reversed lookup order (Extension second)
- Introduced type cache

```
PERF PROGRESS:
SourceGen - Maui.Controls.Sample (262 XAML files)

1) Unchanged:
- 15640 GetTypeByMetadata calls
- 223s

2) Extensions lookup in XmlTypeXamlExtensions.GetTypeReference() second
- 6232 GetTypeByMetadata calls (~60% reduction)
- 90s                          (~60% reduction)

3) With type cache
- 203 GetTypeByMetadata calls (~97% reduction => ~99% total reduction)
- 6s                          (~93% reduction => ~97% total reduction => 37 times faster!)
```

* - Set uinitial lookupNames capacity to 2 since there won't be more than 2

* Added appium UITest for FlyoutNavigationBetweenItemsWithNavigationStacks (#19444) Fixes #16675

* Add better exception for missing Maps on Windows (#19046)

* Add better exception for missing Maps on Windows

* Update AppHostBuilderExtensions.cs

* [ci] Add missing demands (#20183)

* Add comments to internal methods for XAML Hot Reload usage (#20215)

* Add comments to internal methods for XAML Hot Reload usage

* spacing

* Adding Mobile tag to MAUI project templates (#20191)

Co-authored-by: Luke Westendorf <lukewest@microsoft.com>

---------

Co-authored-by: Tim Miller <drasticactions@users.noreply.github.com>
Co-authored-by: Matthew Leibowitz <mattleibow@live.com>
Co-authored-by: Gerald Versluis <gerald.versluis@microsoft.com>
Co-authored-by: Mike Corsaro <corsarom@gmail.com>
Co-authored-by: Mike Corsaro <mikecorsaro@microsoft.com>
Co-authored-by: TJ Lambert <50846373+tj-devel709@users.noreply.github.com>
Co-authored-by: Javier Suárez <javiersuarezruiz@hotmail.com>
Co-authored-by: GitHub Actions Autoformatter <autoformat@example.com>
Co-authored-by: Javier Suárez <6755973+jsuarezruiz@users.noreply.github.com>
Co-authored-by: Stephane Delcroix <stephane@delcroix.org>
Co-authored-by: Marco Goertz <mgoertz@microsoft.com>
Co-authored-by: MSLukeWest <42553283+MSLukeWest@users.noreply.github.com>
Co-authored-by: Luke Westendorf <lukewest@microsoft.com>
@github-actions github-actions bot locked and limited conversation to collaborators Feb 24, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Projects
None yet
4 participants