MCBDSHost - Professional Minecraft Bedrock Server Management

Settings Final Fix

Troubleshooting SETTINGS_FINAL_FIX.md

Settings Not Persisting - Final Fix

Issues Fixed

  1. Wrong appsettings.json path: API might have been saving to wrong file location
  2. Stale data after save: UI was reloading from API immediately after save, getting old values
  3. Missing logging: No visibility into what was happening

Root Cause

The issue had two parts:

1. File Path Issue

The API was using AppContext.BaseDirectory which might point to the wrong location (build output vs source directory). Now it tries multiple paths:

var possiblePaths = new[]
{
    Path.Combine(_environment.ContentRootPath, "appsettings.json"),  //  Most reliable
    Path.Combine(AppContext.BaseDirectory, "appsettings.json"),
    Path.Combine(Directory.GetCurrentDirectory(), "appsettings.json")
};

2. Stale Data After Save

The UI was calling await LoadSettings() immediately after saving, which would:

  1. Send request to API
  2. Get values from IOptionsMonitor which hadn't reloaded yet
  3. Return old values (30, 10)
  4. UI displays old values

Fix: Don't reload - keep the values that were just saved.

Changes Made

BackupController.cs

  1. Added IWebHostEnvironment to find correct content root
  2. Added multiple path checking for appsettings.json
  3. Added extensive logging for debugging
  4. Logs before/after save to verify values
// Log what we're saving
_logger.LogInformation("UpdateConfiguration called: Freq={Freq}, Dir={Dir}, Max={Max}");

// Try multiple paths
var possiblePaths = new[] { ... };

// Log after reload to verify
_logger.LogInformation("After reload: Freq={Freq}, Dir={Dir}, Max={Max}");

BackupConfig.razor

Removed the LoadSettings() call after save:

Before:

if (success)
{
    SetStatus("Settings saved!");
    await Task.Delay(500);
    await LoadSettings();  //  This was getting stale data!
}

After:

if (success)
{
    // Keep the values we just saved (don't reload)
    frequencyMinutes = savedFrequency;
    backupDirectory = savedDirectory;
    maxBackupsToKeep = savedMaxBackups;
    lastLoadTime = DateTime.Now;
    
    SetStatus("Settings saved!");
}

BedrockApiService.cs

Updated to return saved config in response:

public async Task<(bool success, string message, BackupConfigResponse savedConfig)> 
    UpdateBackupConfigAsync(...)

Testing

Check API Logs

When you save, you should see in the API logs:

[BackupController] UpdateConfiguration called: Freq=60, Dir=C:\Backups, Max=5
[BackupController] Checking path: C:\...\MCBDS.API\appsettings.json, Exists: True
[BackupController] Using appsettings.json at: C:\...\MCBDS.API\appsettings.json
[BackupController] Writing to ...: {"Logging":...,"Backup":{"FrequencyMinutes":60,...}}
[BackupController] Backup configuration saved to file
[BackupController] Configuration reloaded
[BackupController] After reload: Freq=60, Dir=C:\Backups, Max=5

Check UI Console

[BackupConfig] Saving: Freq=60, Dir=C:\Backups, Max=5
[BackupConfig] Save successful

Verify appsettings.json

After saving, check the file:

{
  "Backup": {
    "FrequencyMinutes": 60,       Should be your new value
    "BackupDirectory": "C:\\Backups",
    "MaxBackupsToKeep": 5
  }
}

Before vs After

Before (Buggy)

1. User sets frequency to 60
2. Clicks Save
3. API saves to file
4. UI calls LoadSettings()
5. API returns 30 (IOptionsMonitor hasn't reloaded)
6. UI shows 30 

After (Fixed)

1. User sets frequency to 60
2. Clicks Save
3. API saves to file
4. UI keeps value 60 (doesn't reload)
5. UI shows 60 

If Still Not Working

Check 1: API Logs

Look for these log messages:

  • "UpdateConfiguration called" - Was the request received
  • "Using appsettings.json at" - Which file is being updated
  • "Backup configuration saved to file" - Did the save succeed
  • "After reload" - What values are loaded after reload

Check 2: File Location

The API response now includes savedTo field:

{
  "message": "Configuration updated successfully!",
  "savedTo": "C:\\Users\\joshua\\source\\repos\\...\\MCBDS.API\\appsettings.json"
}

Verify this is the correct file.

Check 3: File Contents

Open the file at savedTo path and verify the values are actually updated.

Check 4: Permissions

Make sure the API has write access to appsettings.json.

Summary

The fix ensures:

  1. API finds the correct appsettings.json file
  2. API logs what it's doing for debugging
  3. UI keeps saved values instead of reloading stale data
  4. Clear error messages if something goes wrong

Status

Build: Successful
Path Resolution: Fixed
Stale Data: Fixed
Logging: Added
Ready for: Testing

Run the API and check the logs when you save settings. The logs will tell you exactly what's happening!

Support MCBDSHost Development

Help us continue developing and improving MCBDSHost. Your support makes a difference!