question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

MAUI. In AppShell ctor Current.Services is null.

See original GitHub issue

Package

Sentry.Maui

.NET Version

6.0.0

SDK Version

3.22.0-preview.3

Steps to Reproduce

  1. Create empty MAUI project.
  2. Create and register empty ViewModel.
  3. Create ServiceHelper:
public static class ServiceHelper
{
    public static TService GetService<TService>() => Current.GetService<TService>();

    public static IServiceProvider Current =>
#if WINDOWS10_0_17763_0_OR_GREATER
            MauiWinUIApplication.Current.Services;
#elif ANDROID
            MauiApplication.Current.Services;
#elif IOS || MACCATALYST
            MauiUIApplicationDelegate.Current.Services;
#else
            null;
#endif
}
  1. In AppShell ctor add this:
var vm = ServiceHelper.GetService<ViewModel>();
BindingContext = vm;
  1. Add NuGet:
dotnet add package Sentry.Maui --prerelease
Install-Package Sentry.Maui -AllowPrereleaseVersions
  1. Start your app on Windows or Android (local/emulator).

Expected Result

image

Actual Result

image OS: Android, Windows

Can be used as a workaround

image No exception is thrown in this method. However, it can be called many times, you need to do a check.

Repo

You can ckeck on this repo: https://github.com/Nick4nik/Sentry-AppShell-Issue

Issue Analytics

  • State:closed
  • Created a year ago
  • Comments:11 (9 by maintainers)

github_iconTop GitHub Comments

1reaction
mattjohnsonpintcommented, Oct 19, 2022

Thanks, I will investigate. Though off hand, I don’t see how Sentry would be interfering with that. We don’t unregister anything in DI.

0reactions
mattjohnsonpintcommented, Oct 22, 2022

See #2006 for details of the fix.

Also, FYI - you can simplify your ServiceHelper by using IPlatformApplication.Current?.Services instead of separating by platform manually.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Dependency Injection is Returning Null
I tried adjusting a few of my methods and it still returns null. Any help is much appreciated. I am using .net MAUI....
Read more >
NullReferenceException When Using Maui Class Library ...
Hi there, I am attempting to dynamically resolve and load my Maui pages and viewmodels and also use Devexpress controls like the DXPopup....
Read more >
Transfer data from android activity to maui MainPage
I have tried [QueryProperty] with Shell.Current.GoToAsync and creating a different activity class, but that all doesn't work. So, how can I ...
Read more >
7.2 and 7.1 Maui Android crashes in Release mode, but works ...
Core.DependencyResolutionException: An exception was thrown while invoking the constructor 'Void .ctor(ABP_7_2.Maui.ViewModels.ShellViewModel)' ...
Read more >
KeepScreenOn throws error on Android - Microsoft Q&A
NullReferenceException: The current Activity can not be detected. Ensure that you have called Init in your Activity or Application class. at ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found