Open alzubitariq opened 9 months ago
Yes, this used to work in .NET MAUI
You mention this worked before and therefore is a regression. Do you know in what version it worked? Does it work with the exact same code?
@jonathanpeppers does anything stand out to you immediately with this?
@jfversluis Yes it was working with .NET MAUI 7.0.49 if I am not mistaken
Does it work with the exact same code?
It was different code but with foreground service and when I try to reopen the application it opens without loading the assemblies like it is live inside the memory "Very fast"
We've added this issue to our backlog, and we will work to address it as time and resources allow. If you have any additional information or questions about this issue, please leave a comment. For additional info about issue management, please read our Triage Process.
Between .NET 7 and 8, Android API 34 was released. What is the error message you are getting? adb logcat
output would show it:
You could try targeting API 33 with this line in your AndroidManifest.xml
(it will trigger a warning):
<uses-sdk android:targetSdkVersion="33" />
If the problem goes away when doing this, it's an Android OS behavior change that is the cause.
Hi @alzubitariq. We have added the "s/needs-info" label to this issue, which indicates that we have an open question for you before we can take further action. This issue will be closed automatically in 7 days if we do not hear back from you by then - please feel free to re-open it if you come back to this issue after that time.
I'm having the same issue, in MAUI Android 8.0.100 targeting SDK 33 or 34. This is only occuring with a Blazor Hybrid app not a normal MAUI app.
Log attached, I can't see an obvious error, possibly this one 02-23 15:57:30.940 549 2815 E TaskPersister: File error accessing recents directory (directory doesn't exist?). logcat.txt
My reproduction repo https://github.com/greg73/MauiHybridForegroundService
Duplicate issue I logged (closed) https://github.com/dotnet/maui/issues/20812
I encountered the same issue upgrading my maui blazor hybrid app from .net 7 to .net 8.
Tried a few scenarios - all blazor hybrid: .NET 7 Targetting API33 on 33 Device - works .NET 8 Targetting API33 on 33 Device - does not work - freezes at startup screen Targetting API33 on 34 Device - does not work - freezes at startup screen Targetting API34 on 34 Device - does not work - freezes at startup screen
I encountered the same issue upgrading my maui blazor hybrid app from dotnet 7 to dotnet 8.
Everything was is fine in dotnet 7 but when upgrade my project to dotnet 8 (with exact same code), In first start everything seems fine but when i switch my app or close it (when my foreground service is running) and reopen the app, The app will freeze in splash screen.
Anyone have any solution for this?
Verified this issue with Visual Studio 17.10.0 Preview 1. Can repro on Android platform.
Confirm this issue still exists with these NuGet Packages :
<ItemGroup>
<PackageReference Include="Microsoft.Maui.Controls" Version="8.0.14" />
<PackageReference Include="Microsoft.Maui.Controls.Compatibility" Version="8.0.14" />
<PackageReference Include="Microsoft.AspNetCore.Components.WebView.Maui" Version="8.0.14" />
<PackageReference Include="Microsoft.Extensions.Logging.Debug" Version="8.0.0" />
</ItemGroup>
I ran into a similar if not identical issue in my Xamarin Android project. Whenever I navigate between two fragments or re-open the app from background, there is a chance that the app gets frozen.
(Edit: Not related to this issue)
I still have this issue with any version of .NET 8 but only on a Blazor App. On a normal .NET Maui App with .NET 8 it works well. It happens every time I open the app with a foreground service already running.
On MainActivity, the place where the app gets stuck is:
protected override void OnStart()
{
base.OnStart(); <------ Here
}
The work around I found for now is to stop the service and restart the app:
protected override void OnStart()
{
var serviceIntent = new Intent(this, typeof(MyBackgroundService));
var stopped = StopService(serviceIntent);
if (stopped)
{
FinishAndRemoveTask();
Intent i = new Intent(this.ApplicationContext, typeof(MainActivity));
i.SetFlags(ActivityFlags.ClearTop | ActivityFlags.NewTask);
MainApplication.Current.StartActivity(i);
// Throw exception here to avoid reaching the base.OnStart() that actually causes the problem.
throw new Exception("Forcing a crash to work around the Foreground Service running at startup issue!");
}
base.OnStart();
}
Initially I was having the same problem in a normal .Net Maui App but the reason was because there was an issue on my OnDestroy() method.
Same, but in iOS simulator 🥺 in Windows and Android Emulator its working (the app is new from visual studio template MAUI Blazor hybrid)
I still have this issue with any version of .NET 8 but only on a Blazor App. On a normal .NET Maui App with .NET 8 it works well. It happens every time I open the app with a foreground service already running.
On MainActivity, the place where the app gets stuck is:
protected override void OnStart() { base.OnStart(); <------ Here }
The work around I found for now is to stop the service and restart the app:
protected override void OnStart() { var serviceIntent = new Intent(this, typeof(MyBackgroundService)); var stopped = StopService(serviceIntent); if (stopped) { FinishAndRemoveTask(); Intent i = new Intent(this.ApplicationContext, typeof(MainActivity)); i.SetFlags(ActivityFlags.ClearTop | ActivityFlags.NewTask); MainApplication.Current.StartActivity(i); // Throw exception here to avoid reaching the base.OnStart() that actually causes the problem. throw new Exception("Forcing a crash to work around the Foreground Service running at startup issue!"); } base.OnStart(); }
Initially I was having the same problem in a normal .Net Maui App but the reason was because there was an issue on my OnDestroy() method.
I tested your solution but after stop the service application still freeze on base.OnStart();
We had this issue on our Maui Blazor Hybrid app. We found that when the BlazorWebView was not present in MainPage.xaml the app resumed fine.
As a workaround, we removed the BlazorWebView from MainPage.xaml inside the OnDisappearing method to effectively force the webview to close. We utilized a StackLayout to do this. Code example below.
MainPage.xaml:
<StackLayout x:Name="stackLayout" BackgroundColor="DarkSlateGrey">
<BlazorWebView x:Name="blazorWebView" HostPage="wwwroot/index.html" HorizontalOptions="FillAndExpand" VerticalOptions="FillAndExpand">
<BlazorWebView.RootComponents>
<RootComponent Selector="#app" ComponentType="{x:Type local:Main}" />
</BlazorWebView.RootComponents>
</BlazorWebView>
</StackLayout>
MainPage.xaml.cs:
protected override void OnAppearing()
{
base.OnAppearing();
if (stackLayout.Children.Count == 0)
{
blazorWebView = new()
{
HostPage = "wwwroot/index.html",
HorizontalOptions = LayoutOptions.FillAndExpand,
VerticalOptions = LayoutOptions.FillAndExpand,
};
RootComponent rootComponent = new()
{
Selector = "#app",
ComponentType = typeof(Main),
};
blazorWebView.RootComponents.Add(rootComponent);
stackLayout.Children.Add(blazorWebView);
}
}
protected override void OnDisappearing()
{
stackLayout.Children.Remove(blazorWebView);
base.OnDisappearing();
}
@andrewpbaer Thanks for this, do you have a sample repo you could share? I tried to implement your workaround, I changed Main to MainPage but this can't be right as now getting the error when reloading the app
[chromium] [INFO:CONSOLE(1)] "The type MainPage does not implement IComponent. (Parameter 'componentType')
Sorry please ignore that, I've got it working. Main is Routes in the default template. Thank you for the workaround
I hope it's fixed soon. I went with this workaround:
MainPage.xaml.cs:
#if ANDROID
protected override void OnAppearing()
{
if (contentPage.Content is null)
{
var previousRootComponents = blazorWebView.RootComponents;
blazorWebView = new() { HostPage = blazorWebView.HostPage };
foreach (var rootComponent in previousRootComponents)
{
blazorWebView.RootComponents.Add(rootComponent);
}
contentPage.Content = blazorWebView;
}
base.OnAppearing();
}
protected override void OnDisappearing()
{
base.OnDisappearing();
contentPage.Content = null;
}
#endif
and added x:Name="contentPage"
to the ContentPage element in MainPage.xaml. Requires no stack or grid layout.
As far as I can see this has nothing to do with the foreground service itself. The error must be related to BlazorWebView. I recreated the error in a blank blazor app (made from the VS template).
I also tested the workaround mentioned by @gdar91 and it worked fine.
We had this issue on our Maui Blazor Hybrid app. We found that when the BlazorWebView was not present in MainPage.xaml the app resumed fine.
As a workaround, we removed the BlazorWebView from MainPage.xaml inside the OnDisappearing method to effectively force the webview to close. We utilized a StackLayout to do this. Code example below.
MainPage.xaml:
<StackLayout x:Name="stackLayout" BackgroundColor="DarkSlateGrey"> <BlazorWebView x:Name="blazorWebView" HostPage="wwwroot/index.html" HorizontalOptions="FillAndExpand" VerticalOptions="FillAndExpand"> <BlazorWebView.RootComponents> <RootComponent Selector="#app" ComponentType="{x:Type local:Main}" /> </BlazorWebView.RootComponents> </BlazorWebView> </StackLayout>
MainPage.xaml.cs:
protected override void OnAppearing() { base.OnAppearing(); if (stackLayout.Children.Count == 0) { blazorWebView = new() { HostPage = "wwwroot/index.html", HorizontalOptions = LayoutOptions.FillAndExpand, VerticalOptions = LayoutOptions.FillAndExpand, }; RootComponent rootComponent = new() { Selector = "#app", ComponentType = typeof(Main), }; blazorWebView.RootComponents.Add(rootComponent); stackLayout.Children.Add(blazorWebView); } } protected override void OnDisappearing() { stackLayout.Children.Remove(blazorWebView); base.OnDisappearing(); }
That also worked for me, but not sure why it happens just in combination with a foreground service.
Is there any update on this? I'm facing this issue on MAUI 8.0.82 SR8.2
This workaround seems to get around the problem for me. I just wanted to contribute to the solution, though. If you have other native pages that get pushed on top of the BlazorWebView (for example, a page that uses a camera plugin) you'll want to track whether the page is disappearing because it's being covered by another page, and only remove the view if it's disappearing for another reason.
Description
I am running a foreground service on .Net Maui Blazor. Once I got the foreground service to start and exit the app, when I try to reopen the application. It is stuck on the SplashScreen
Steps to Reproduce
I already provided a repository
Link to public reproduction project repository
https://github.com/alzubitariq/MauiAppServiceIssue
Version with bug
8.0.7 SR2
Is this a regression from previous behavior?
Yes, this used to work in .NET MAUI
Last version that worked well
Unknown/Other
Affected platforms
Android
Affected platform versions
Android 33
Did you find any workaround?
No
Relevant log output
No response