I've been reading the book by Shaun Lawrence - Introducing .NET MAUI and following and typing code along with his examples.
When running the code of one of his examples on the Windows Machine (with debugging), it causes VS2022 to crash with an unhandled exception like this:
An unhandled win32 exception occurred in [25760] WidgetBoard.exe A debugger is attached to WidgetBoard.exe but nor configured to debug this unhandled exception. To debug this exception, detach the current debugger.
and prompts me to change the debugger. The bug occurs in the auto-generated file App.g.i.cs:
#pragma checksum "D:\...\Introducing-MAUI-main\ch08\WidgetBoard\WidgetBoard\Platforms\Windows\App.xaml" "{8829d00f-11b8-4213-878b-770e8597ac16}" "1E61AE04B24B204A36C0ADF07C903371B982367A1BF837AFFC79CF9371D7949D"
//------------------------------------------------------------------------------
// <auto-generated>
// This code was generated by a tool.
//
// Changes to this file may cause incorrect behavior and will be lost if
// the code is regenerated.
// </auto-generated>
//------------------------------------------------------------------------------
namespace WidgetBoard.WinUI
{
#if !DISABLE_XAML_GENERATED_MAIN
/// <summary>
/// Program class
/// </summary>
public static class Program
{
[global::System.Runtime.InteropServices.DllImport("Microsoft.ui.xaml.dll")]
private static extern void XamlCheckProcessRequirements();
[global::System.CodeDom.Compiler.GeneratedCodeAttribute("Microsoft.UI.Xaml.Markup.Compiler"," 1.0.0.0")]
[global::System.Diagnostics.DebuggerNonUserCodeAttribute()]
[global::System.STAThreadAttribute]
static void Main(string[] args)
{
XamlCheckProcessRequirements();
global::WinRT.ComWrappersSupport.InitializeComWrappers();
global::Microsoft.UI.Xaml.Application.Start((p) => {
var context = new global::Microsoft.UI.Dispatching.DispatcherQueueSynchronizationContext(global::Microsoft.UI.Dispatching.DispatcherQueue.GetForCurrentThread());
global::System.Threading.SynchronizationContext.SetSynchronizationContext(context);
new App();
});
}
}
#endif
partial class App : global::Microsoft.Maui.MauiWinUIApplication
{
[global::System.CodeDom.Compiler.GeneratedCodeAttribute("Microsoft.UI.Xaml.Markup.Compiler"," 1.0.0.0")]
private bool _contentLoaded;
/// <summary>
/// InitializeComponent()
/// </summary>
[global::System.CodeDom.Compiler.GeneratedCodeAttribute("Microsoft.UI.Xaml.Markup.Compiler"," 1.0.0.0")]
[global::System.Diagnostics.DebuggerNonUserCodeAttribute()]
public void InitializeComponent()
{
if (_contentLoaded)
return;
_contentLoaded = true;
global::System.Uri resourceLocator = new global::System.Uri("ms-appx:///Platforms/Windows/App.xaml");
global::Microsoft.UI.Xaml.Application.LoadComponent(this, resourceLocator);
#if DEBUG && !DISABLE_XAML_GENERATED_BINDING_DEBUG_OUTPUT
DebugSettings.BindingFailed += (sender, args) =>
{
global::System.Diagnostics.Debug.WriteLine(args.Message);
};
#endif
#if DEBUG && !DISABLE_XAML_GENERATED_BREAK_ON_UNHANDLED_EXCEPTION
UnhandledException += (sender, e) =>
{
if (global::System.Diagnostics.Debugger.IsAttached) global::System.Diagnostics.Debugger.Break(); // Crash happens here
};
#endif
}
}
}
I tried deleting obj and bin and restarting my PC. When running the code on the Windows Machine without debugging, the app simply doesn't start. But when running it on the android emulator it starts fine.
The best I can tell, by adding code step-by-step and running it, the problem starts after creating a new interface IBoardRepository.cs and adding it to an existing ViewModel like this:
private IBoardRepository boardRepository;
public BoardDetailsPageViewModel(
ISemanticScreenReader semanticScreenReader,
IBoardRepository boardRepository)
{
this.semanticScreenReader = semanticScreenReader;
SaveCommand = new Command(
() => Save(),
() => !string.IsNullOrWhiteSpace(BoardName));
}
Before adding the IBoardRepository to the constructor, the app works as expected, after adding it I get the crash.
If you need more details about the project, here's a link to the git repo of the author's own code: https://github.com/Apress/Introducing-MAUI/tree/main/ch09/WidgetBoard (I tried just downloading his code and running it and I get the same error)
At this point I have no idea if this is a MAUI or a code bug. I'd appreciate any help/explanation/further research resource.
I've downloaded and tested the sample code:https://github.com/Apress/Introducing-MAUI/tree/main/ch09/WidgetBoard on Windows.
It turns out that the issue is related with the way of registering the IBoardRepository
and LiteDBBoardRepository
in MauiProgram.cs. When changing it from AddTransient
to AddSingleton
like below, the issue can be fixed as expected.
builder.Services.AddSingleton<IBoardRepository, LiteDBBoardRepository>();
For more details, you can refer to Dependency injection and get to know the different ways of registering types and objects in the container through code.