Skip to content

ObjectDisposedException for IServiceProvider in different Places #33923

@mblamauer

Description

@mblamauer

Description

Since updating our App to .NET 10 we have several occurences of this type of exception (originating from different Handlers/Places):

android.runtime.JavaProxyThrowable: [System.ObjectDisposedException]: Cannot access a disposed object. Object name: 'IServiceProvider'. at Microsoft.Extensions.DependencyInjection.ServiceLookup.ThrowHelper.ThrowObjectDisposedException + 0x0(Unknown Source) at Microsoft.Extensions.DependencyInjection.ServiceLookup.ServiceProviderEngineScope.GetService + 0x8(Unknown Source) at Microsoft.Maui.MauiContext+WrappedServiceProvider.GetService(/_/src/Core/src/MauiContext.cs:87) at Microsoft.Maui.MauiContext+WrappedServiceProvider.GetService(/_/src/Core/src/MauiContext.cs:87) at Microsoft.Extensions.DependencyInjection.ServiceProviderServiceExtensions.GetService + 0xb(Unknown Source) at Microsoft.Maui.ElementHandlerExtensions.GetService(/_/src/Core/src/Handlers/ElementHandlerExtensions.cs:49) at Microsoft.Maui.Controls.InputView.MapIsFocused(/_/src/Controls/src/Core/InputView/InputView.Platform.cs:12) at Microsoft.Maui.PropertyMapperExtensions+<>c__DisplayClass3_0'2[[Microsoft.Maui.IEntry, Microsoft.Maui, Version=10.0.0.0, Culture=neutral, PublicKeyToken=null],[Microsoft.Maui.Handlers.IEntryHandler, Microsoft.Maui, Version=10.0.0.0, Culture=neutral, PublicKeyToken=null]].<AppendToMapping>b__0(/_/src/Core/src/PropertyMapperExtensions.cs:84) at Microsoft.Maui.PropertyMapperExtensions+<>c__DisplayClass0_0'2[[Microsoft.Maui.IEntry, Microsoft.Maui, Version=10.0.0.0, Culture=neutral, PublicKeyToken=null],[Microsoft.Maui.Handlers.IEntryHandler, Microsoft.Maui, Version=10.0.0.0, Culture=neutral, PublicKeyToken=null]].<ModifyMapping>g__newMethod|0(/_/src/Core/src/PropertyMapperExtensions.cs:23) at Microsoft.Maui.PropertyMapper'2+<>c__DisplayClass5_0[[Microsoft.Maui.IEntry, Microsoft.Maui, Version=10.0.0.0, Culture=neutral, PublicKeyToken=null],[Microsoft.Maui.Handlers.IEntryHandler, Microsoft.Maui, Version=10.0.0.0, Culture=neutral, PublicKeyToken=null]].<Add>b__0(/_/src/Core/src/PropertyMapper.cs:239) at Microsoft.Maui.PropertyMapper.TryUpdatePropertyCore(/_/src/Core/src/PropertyMapper.cs:63) at Microsoft.Maui.PropertyMapper.UpdateProperty(/_/src/Core/src/PropertyMapper.cs:114) at Microsoft.Maui.Handlers.ElementHandler.UpdateValue(/_/src/Core/src/Handlers/Element/ElementHandler.cs:106) at Microsoft.Maui.Controls.Element.UpdateHandlerValue(/_/src/Controls/src/Core/Element/Element.cs:722) at Microsoft.Maui.Controls.VisualElement.UpdateHandlerValue(/_/src/Controls/src/Core/VisualElement/VisualElement.cs:2108) at Microsoft.Maui.Controls.Element.OnBindablePropertySet(/_/src/Controls/src/Core/Element/Element.cs:680) at Microsoft.Maui.Controls.BindableObject.SetValueActual(/_/src/Controls/src/Core/BindableObject.cs:682) at Microsoft.Maui.Controls.BindableObject.SetValueCore(/_/src/Controls/src/Core/BindableObject.cs:610) at Microsoft.Maui.Controls.BindableObject.SetValue(/_/src/Controls/src/Core/BindableObject.cs:552) at Microsoft.Maui.Controls.VisualElement.Microsoft.Maui.IView.set_IsFocused(/_/src/Controls/src/Core/VisualElement/VisualElement.cs:2067) at Microsoft.Maui.Handlers.ViewHandler.OnPlatformViewFocusChange(/_/src/Core/src/Handlers/View/ViewHandler.Android.cs:255) at Unknown.Unknown(Unknown Source) at Android.Views.View+IOnFocusChangeListenerImplementor.OnFocusChange + 0xa(Unknown Source) at Android.Views.View+IOnFocusChangeListenerInvoker.n_OnFocusChange_Landroid_view_View_Z + 0xe(Unknown Source) at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw + 0x11(Unknown Source) at Java.Interop.JniEnvironment+InstanceMethods.CallNonvirtualVoidMethod + 0x86(Unknown Source) at Java.Interop.JniPeerMembers+JniInstanceMethods.InvokeVirtualVoidMethod + 0x65(Unknown Source) at Android.App.Activity.OnDestroy + 0x0(Unknown Source) at Microsoft.Maui.MauiAppCompatActivity.OnDestroy(/_/src/Core/src/Platform/Android/MauiAppCompatActivity.cs:59) at Android.App.Activity.n_OnDestroy + 0xe(Unknown Source) at crc6488302ad6e9e4df1a.MauiAppCompatActivity.n_onDestroy(Native Method) at crc6488302ad6e9e4df1a.MauiAppCompatActivity.onDestroy(MauiAppCompatActivity.java:59) at android.app.Activity.performDestroy(Activity.java:9628) at android.app.Instrumentation.callActivityOnDestroy(Instrumentation.java:1576) at android.app.ActivityThread.performDestroyActivity(ActivityThread.java:6523) at android.app.ActivityThread.handleDestroyActivity(ActivityThread.java:6568) at android.app.servertransaction.DestroyActivityItem.execute(DestroyActivityItem.java:52) at android.app.servertransaction.ActivityTransactionItem.execute(ActivityTransactionItem.java:63) at android.app.servertransaction.TransactionExecutor.executeLifecycleItem(TransactionExecutor.java:169) at android.app.servertransaction.TransactionExecutor.executeTransactionItems(TransactionExecutor.java:101) at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:80) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2961) at android.os.Handler.dispatchMessage(Handler.java:132) at android.os.Looper.dispatchMessage(Looper.java:333) at android.os.Looper.loopOnce(Looper.java:263) at android.os.Looper.loop(Looper.java:367) at android.app.ActivityThread.main(ActivityThread.java:9287) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:566) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:929)

This exception happened for example when closing the app on the Android Emulator (Android 16) through the phones task manager while an entry is focused and the keyboard is up.

We also see some of these exceptions in the Google Play Console for our Production App. Seems this is mostly but not exclusivly happening on Android 16.
Another origin of this exception appears to be the Flyoutpage:

Exception android.runtime.JavaProxyThrowable: [System.ObjectDisposedException]: ObjectDisposed_Generic ObjectDisposed_ObjectName_Name, IServiceProvider at Microsoft.Extensions.DependencyInjection.ServiceLookup.ThrowHelper.ThrowObjectDisposedException + 0x0 (Unknown Source) at Microsoft.Extensions.DependencyInjection.ServiceLookup.ServiceProviderEngineScope.GetService + 0x8 (Unknown Source) at Microsoft.Maui.MauiContext+WrappedServiceProvider.GetService + 0x2c (Unknown Source) at Microsoft.Extensions.DependencyInjection.ServiceProviderServiceExtensions.GetService + 0xb (Unknown Source) at Microsoft.Maui.Platform.MauiContextExtensions.GetFragmentManager + 0x6 (Unknown Source) at Microsoft.Maui.Handlers.FlyoutViewHandler.UpdateDetailsFragmentView + 0x9c (Unknown Source) at Microsoft.Maui.Handlers.FlyoutViewHandler.LayoutAsFlyout + 0x76 (Unknown Source) at Microsoft.Maui.Handlers.FlyoutViewHandler.LayoutViews + 0x1e (Unknown Source) at Microsoft.Maui.Handlers.FlyoutViewHandler.UpdateFlyoutBehavior + 0x39 (Unknown Source) at Microsoft.Maui.Handlers.FlyoutViewHandler.MapFlyoutBehavior + 0xa (Unknown Source)

and a Label:

ObjectDisposed_ObjectName_Name, IServiceProvider at Microsoft.Extensions.DependencyInjection.ServiceLookup.ThrowHelper.ThrowObjectDisposedException + 0x0 (Unknown Source) at Microsoft.Extensions.DependencyInjection.ServiceLookup.ServiceProviderEngineScope.GetService + 0x8 (Unknown Source) at Microsoft.Maui.MauiContext+WrappedServiceProvider.GetService + 0x2c (Unknown Source) at Microsoft.Maui.MauiContext+WrappedServiceProvider.GetService + 0x2c (Unknown Source) at Microsoft.Maui.MauiContext+WrappedServiceProvider.GetService + 0x2c (Unknown Source) at Microsoft.Extensions.DependencyInjection.ServiceProviderServiceExtensions.GetRequiredService + 0x28 (Unknown Source) at Microsoft.Extensions.DependencyInjection.ServiceProviderServiceExtensions.GetRequiredService + 0xb (Unknown Source) at Microsoft.Maui.ElementHandlerExtensions.GetRequiredService + 0x7 (Unknown Source) at Microsoft.Maui.Handlers.LabelHandler.MapFont + 0x0 (Unknown Source)

Steps to Reproduce

I have not really tried to verify this with a new/sample App. The only error I can reproduce in our App is the one when closing the app while an entry is focused.

Link to public reproduction project repository

No response

Version with bug

10.0.30

Is this a regression from previous behavior?

Yes, this used to work in .NET MAUI

Last version that worked well

9.0.110 SR11

Affected platforms

Android

Affected platform versions

Mostly Android 16

Did you find any workaround?

No response

Relevant log output

Metadata

Metadata

Assignees

No one assigned

    Labels

    s/needs-attentionIssue has more information and needs another lookt/bugSomething isn't working

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions