From 2a020af9b173cfb8409db71d0d2ab6fa143a6100 Mon Sep 17 00:00:00 2001 From: bean1352 Date: Wed, 13 Aug 2025 13:41:08 +0200 Subject: [PATCH 1/2] Added more verbose logging and moved db instantiation to MainWindow --- demos/WPF/App.xaml | 3 +- demos/WPF/App.xaml.cs | 78 ++++++++++++++++--- demos/WPF/MainWindow.xaml.cs | 47 ++++++++++- .../PublishProfiles/FolderProfile.pubxml | 16 ++++ demos/WPF/ViewModels/MainWindowViewModel.cs | 39 ++++++++-- demos/WPF/WPF.csproj | 6 ++ 6 files changed, 167 insertions(+), 22 deletions(-) create mode 100644 demos/WPF/Properties/PublishProfiles/FolderProfile.pubxml diff --git a/demos/WPF/App.xaml b/demos/WPF/App.xaml index bc72abf..cbaaee4 100644 --- a/demos/WPF/App.xaml +++ b/demos/WPF/App.xaml @@ -1,7 +1,8 @@  + xmlns:converters="clr-namespace:PowersyncDotnetTodoList.Converters" + DispatcherUnhandledException="Application_DispatcherUnhandledException"> diff --git a/demos/WPF/App.xaml.cs b/demos/WPF/App.xaml.cs index 0479cf1..01dc981 100644 --- a/demos/WPF/App.xaml.cs +++ b/demos/WPF/App.xaml.cs @@ -14,33 +14,64 @@ public partial class App : Application { public static IServiceProvider? Services { get; private set; } - protected override async void OnStartup(StartupEventArgs e) + protected override void OnStartup(StartupEventArgs e) { base.OnStartup(e); + // Handle async initialization synchronously or with proper error handling + try + { + InitializeApplicationAsync().GetAwaiter().GetResult(); + } + catch (Exception ex) + { + MessageBox.Show( + $"Application startup failed: {ex.Message}\n\nFull error: {ex}", + "Startup Error", + MessageBoxButton.OK, + MessageBoxImage.Error + ); + + // Log to console if available + Console.WriteLine($"=== APPLICATION STARTUP FAILED ==="); + Console.WriteLine($"Exception: {ex}"); + + // Shut down the application + Current.Shutdown(1); + return; + } + } + + private async Task InitializeApplicationAsync() + { + Console.WriteLine("DEBUG: Starting application initialization..."); + var services = new ServiceCollection(); ConfigureServices(services); + Console.WriteLine("DEBUG: Services configured"); // Build the service provider Services = services.BuildServiceProvider(); - - // Initialize the database and connector - var db = Services.GetRequiredService(); - var connector = Services.GetRequiredService(); - await db.Init(); - await db.Connect(connector); - await db.WaitForFirstSync(); - var mainWindow = Services.GetRequiredService(); + Console.WriteLine("DEBUG: Got MainWindow"); + Console.WriteLine("DEBUG: Getting NavigationService..."); var navigationService = Services.GetRequiredService(); + Console.WriteLine("DEBUG: Got NavigationService"); + + Console.WriteLine("DEBUG: Navigating to TodoListViewModel..."); navigationService.Navigate(); + Console.WriteLine("DEBUG: Navigation completed"); + Console.WriteLine("DEBUG: Showing main window..."); mainWindow.Show(); + Console.WriteLine("DEBUG: Main window shown - startup complete!"); } private void ConfigureServices(IServiceCollection services) { + Console.WriteLine("DEBUG: Configuring services..."); + ILoggerFactory loggerFactory = LoggerFactory.Create(builder => { builder.AddConsole(); @@ -50,6 +81,7 @@ private void ConfigureServices(IServiceCollection services) // Register PowerSyncDatabase services.AddSingleton(sp => { + Console.WriteLine("DEBUG: Creating PowerSyncDatabase..."); var logger = loggerFactory.CreateLogger("PowerSyncLogger"); return new PowerSyncDatabase( new PowerSyncDatabaseOptions @@ -67,7 +99,11 @@ private void ConfigureServices(IServiceCollection services) ); // Register PowerSyncConnector - services.AddSingleton(); + services.AddSingleton(sp => + { + Console.WriteLine("DEBUG: Creating PowerSyncConnector..."); + return new PowerSyncConnector(); + }); // Register ViewModels and Views services.AddTransient(); @@ -82,9 +118,31 @@ private void ConfigureServices(IServiceCollection services) services.AddSingleton(sp => { + Console.WriteLine("DEBUG: Creating NavigationService..."); var mainWindow = sp.GetRequiredService(); return new NavigationService(mainWindow.MainFrame, sp); }); + + Console.WriteLine("DEBUG: Service configuration completed"); + } + + // Add global exception handler + private void Application_DispatcherUnhandledException( + object sender, + System.Windows.Threading.DispatcherUnhandledExceptionEventArgs e + ) + { + Console.WriteLine($"=== UNHANDLED EXCEPTION ==="); + Console.WriteLine($"Exception: {e.Exception}"); + + MessageBox.Show( + $"An unhandled exception occurred: {e.Exception.Message}\n\nFull error: {e.Exception}", + "Unhandled Exception", + MessageBoxButton.OK, + MessageBoxImage.Error + ); + + e.Handled = true; // Prevent application crash } } } diff --git a/demos/WPF/MainWindow.xaml.cs b/demos/WPF/MainWindow.xaml.cs index 26047c2..8f85986 100644 --- a/demos/WPF/MainWindow.xaml.cs +++ b/demos/WPF/MainWindow.xaml.cs @@ -1,14 +1,55 @@ -using System.Windows; +using PowerSync.Common.Client; +using PowersyncDotnetTodoList.Services; using PowersyncDotnetTodoList.ViewModels; namespace PowersyncDotnetTodoList; public partial class MainWindow : Window { - public MainWindow(MainWindowViewModel viewModel) + private readonly MainWindowViewModel _viewModel; + private readonly PowerSyncConnector _connector; + private readonly PowerSyncDatabase _db; // Replace with your actual database interface/type + + public MainWindow( + MainWindowViewModel viewModel, + PowerSyncConnector connector, + PowerSyncDatabase db + ) { InitializeComponent(); - this.DataContext = viewModel; + _viewModel = viewModel; + _connector = connector; + _db = db; + + this.DataContext = _viewModel; + + // Start the async initialization + InitializeAsync(); + } + + private async void InitializeAsync() + { + try + { + Console.WriteLine("DEBUG: Initializing database..."); + await _db.Init(); + Console.WriteLine("DEBUG: Database initialized"); + + Console.WriteLine("DEBUG: Connecting to database..."); + await _db.Connect(_connector); + Console.WriteLine("DEBUG: Database connected"); + } + catch (Exception ex) + { + Console.WriteLine($"ERROR: Failed to initialize or connect to database: {ex.Message}"); + // Optionally show an error message to the user + MessageBox.Show( + $"Failed to initialize database: {ex.Message}", + "Error", + MessageBoxButton.OK, + MessageBoxImage.Error + ); + } } } diff --git a/demos/WPF/Properties/PublishProfiles/FolderProfile.pubxml b/demos/WPF/Properties/PublishProfiles/FolderProfile.pubxml new file mode 100644 index 0000000..a31548a --- /dev/null +++ b/demos/WPF/Properties/PublishProfiles/FolderProfile.pubxml @@ -0,0 +1,16 @@ + + + + + Release + Any CPU + bin\Release\net9.0-windows\publish\win-x64\ + FileSystem + <_TargetId>Folder + net9.0-windows + win-x64 + true + false + false + + \ No newline at end of file diff --git a/demos/WPF/ViewModels/MainWindowViewModel.cs b/demos/WPF/ViewModels/MainWindowViewModel.cs index 7b596d4..3aa6806 100644 --- a/demos/WPF/ViewModels/MainWindowViewModel.cs +++ b/demos/WPF/ViewModels/MainWindowViewModel.cs @@ -29,17 +29,40 @@ public bool Connected #region Constructor public MainWindowViewModel(PowerSyncDatabase db) { - _db = db; - // Set up the listener to track the status changes - _db.RunListener( - (update) => + try + { + Console.WriteLine("MainWindowViewModel constructor started"); + + if (db == null) { - if (update.StatusChanged != null) + Console.WriteLine("ERROR: PowerSyncDatabase is null!"); + throw new ArgumentNullException(nameof(db)); + } + + _db = db; + Console.WriteLine("PowerSyncDatabase assigned successfully"); + + // Set up the listener to track the status changes + Console.WriteLine("Setting up RunListener..."); + _db.RunListener( + (update) => { - Connected = update.StatusChanged.Connected; + Console.WriteLine( + $"Listener callback triggered: StatusChanged = {update.StatusChanged?.Connected}" + ); + if (update.StatusChanged != null) + { + Connected = update.StatusChanged.Connected; + } } - } - ); + ); + Console.WriteLine("RunListener setup complete"); + } + catch (Exception ex) + { + Console.WriteLine($"ERROR in MainWindowViewModel constructor: {ex}"); + throw; + } } #endregion } diff --git a/demos/WPF/WPF.csproj b/demos/WPF/WPF.csproj index 89e7de2..019b0cb 100644 --- a/demos/WPF/WPF.csproj +++ b/demos/WPF/WPF.csproj @@ -19,4 +19,10 @@ + + + + Always + + From c4357d42121cd454d8b57c1144ead83e78221e7b Mon Sep 17 00:00:00 2001 From: bean1352 Date: Wed, 13 Aug 2025 13:48:48 +0200 Subject: [PATCH 2/2] Removed unnecessary console.logs --- demos/WPF/App.xaml.cs | 26 --------------------- demos/WPF/MainWindow.xaml.cs | 9 +------ demos/WPF/ViewModels/MainWindowViewModel.cs | 5 ---- 3 files changed, 1 insertion(+), 39 deletions(-) diff --git a/demos/WPF/App.xaml.cs b/demos/WPF/App.xaml.cs index 01dc981..629d3a7 100644 --- a/demos/WPF/App.xaml.cs +++ b/demos/WPF/App.xaml.cs @@ -32,10 +32,6 @@ protected override void OnStartup(StartupEventArgs e) MessageBoxImage.Error ); - // Log to console if available - Console.WriteLine($"=== APPLICATION STARTUP FAILED ==="); - Console.WriteLine($"Exception: {ex}"); - // Shut down the application Current.Shutdown(1); return; @@ -44,34 +40,20 @@ protected override void OnStartup(StartupEventArgs e) private async Task InitializeApplicationAsync() { - Console.WriteLine("DEBUG: Starting application initialization..."); - var services = new ServiceCollection(); ConfigureServices(services); - Console.WriteLine("DEBUG: Services configured"); // Build the service provider Services = services.BuildServiceProvider(); var mainWindow = Services.GetRequiredService(); - Console.WriteLine("DEBUG: Got MainWindow"); - - Console.WriteLine("DEBUG: Getting NavigationService..."); var navigationService = Services.GetRequiredService(); - Console.WriteLine("DEBUG: Got NavigationService"); - Console.WriteLine("DEBUG: Navigating to TodoListViewModel..."); navigationService.Navigate(); - Console.WriteLine("DEBUG: Navigation completed"); - - Console.WriteLine("DEBUG: Showing main window..."); mainWindow.Show(); - Console.WriteLine("DEBUG: Main window shown - startup complete!"); } private void ConfigureServices(IServiceCollection services) { - Console.WriteLine("DEBUG: Configuring services..."); - ILoggerFactory loggerFactory = LoggerFactory.Create(builder => { builder.AddConsole(); @@ -81,7 +63,6 @@ private void ConfigureServices(IServiceCollection services) // Register PowerSyncDatabase services.AddSingleton(sp => { - Console.WriteLine("DEBUG: Creating PowerSyncDatabase..."); var logger = loggerFactory.CreateLogger("PowerSyncLogger"); return new PowerSyncDatabase( new PowerSyncDatabaseOptions @@ -101,7 +82,6 @@ private void ConfigureServices(IServiceCollection services) // Register PowerSyncConnector services.AddSingleton(sp => { - Console.WriteLine("DEBUG: Creating PowerSyncConnector..."); return new PowerSyncConnector(); }); @@ -118,12 +98,9 @@ private void ConfigureServices(IServiceCollection services) services.AddSingleton(sp => { - Console.WriteLine("DEBUG: Creating NavigationService..."); var mainWindow = sp.GetRequiredService(); return new NavigationService(mainWindow.MainFrame, sp); }); - - Console.WriteLine("DEBUG: Service configuration completed"); } // Add global exception handler @@ -132,9 +109,6 @@ private void Application_DispatcherUnhandledException( System.Windows.Threading.DispatcherUnhandledExceptionEventArgs e ) { - Console.WriteLine($"=== UNHANDLED EXCEPTION ==="); - Console.WriteLine($"Exception: {e.Exception}"); - MessageBox.Show( $"An unhandled exception occurred: {e.Exception.Message}\n\nFull error: {e.Exception}", "Unhandled Exception", diff --git a/demos/WPF/MainWindow.xaml.cs b/demos/WPF/MainWindow.xaml.cs index 8f85986..8509953 100644 --- a/demos/WPF/MainWindow.xaml.cs +++ b/demos/WPF/MainWindow.xaml.cs @@ -8,7 +8,7 @@ public partial class MainWindow : Window { private readonly MainWindowViewModel _viewModel; private readonly PowerSyncConnector _connector; - private readonly PowerSyncDatabase _db; // Replace with your actual database interface/type + private readonly PowerSyncDatabase _db; public MainWindow( MainWindowViewModel viewModel, @@ -32,18 +32,11 @@ private async void InitializeAsync() { try { - Console.WriteLine("DEBUG: Initializing database..."); await _db.Init(); - Console.WriteLine("DEBUG: Database initialized"); - - Console.WriteLine("DEBUG: Connecting to database..."); await _db.Connect(_connector); - Console.WriteLine("DEBUG: Database connected"); } catch (Exception ex) { - Console.WriteLine($"ERROR: Failed to initialize or connect to database: {ex.Message}"); - // Optionally show an error message to the user MessageBox.Show( $"Failed to initialize database: {ex.Message}", "Error", diff --git a/demos/WPF/ViewModels/MainWindowViewModel.cs b/demos/WPF/ViewModels/MainWindowViewModel.cs index 3aa6806..368ee16 100644 --- a/demos/WPF/ViewModels/MainWindowViewModel.cs +++ b/demos/WPF/ViewModels/MainWindowViewModel.cs @@ -31,7 +31,6 @@ public MainWindowViewModel(PowerSyncDatabase db) { try { - Console.WriteLine("MainWindowViewModel constructor started"); if (db == null) { @@ -41,9 +40,6 @@ public MainWindowViewModel(PowerSyncDatabase db) _db = db; Console.WriteLine("PowerSyncDatabase assigned successfully"); - - // Set up the listener to track the status changes - Console.WriteLine("Setting up RunListener..."); _db.RunListener( (update) => { @@ -56,7 +52,6 @@ public MainWindowViewModel(PowerSyncDatabase db) } } ); - Console.WriteLine("RunListener setup complete"); } catch (Exception ex) {