microsoft / microsoft/WindowsAppSDK

LocalSettings throws an UnauthorizedAccessException after AppInstance.Restart

Open
#3,957 4 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

area-Lifecycle
Dominant language
C++
Stars
4.7k
Forks
471
Avg merge
3d 13h
Merged PRs (30d)
28

Description

Describe the bug

LocalSettings throws an UnauthorizedAccessException after AppInstance.Restart.
How can I fix this?

Expected: should be able to access LocalSettings after a restart.

Steps to reproduce the bug

Using Windows 10 and Windows App SDK, create a WinUI 3 desktop application using the template.

Change MainWindow.xaml to contain this:

<StackPanel Orientation="Vertical" HorizontalAlignment="Center" VerticalAlignment="Center">
    <TextBlock x:Name="TimeTextBlock">(unkown)</TextBlock>
    <Button x:Name="SetButton" Click="Set_Click">Set time</Button>
    <Button x:Name="GetButton" Click="Get_Click">Get time</Button>
    <Button x:Name="RestartButton" Click="Restart_Click">Restart</Button>
    <TextBlock x:Name="StatusTextBlock">Startup</TextBlock>
</StackPanel>

Change MainWindow.xaml.cs to contain this:

    public MainWindow()
    {
        this.InitializeComponent();
    }

    private Windows.Storage.ApplicationDataContainer LocalSettings => this._localSettings ??= Windows.Storage.ApplicationData.Current.LocalSettings;
    private Windows.Storage.ApplicationDataContainer? _localSettings;

    private void Set_Click(object sender, RoutedEventArgs args)
    {
        try {
            DateTimeOffset now = DateTimeOffset.Now;
            this.LocalSettings.Values["Time"] = now;
            this.StatusTextBlock.Text = "Set value " + now.ToString();
        } catch (Exception e) {
            this.StatusTextBlock.Text = $"Exception when setting value, type '{e.GetType().Name}' message '{e.Message}'.";
        }
    }

    private void Get_Click(object sender, RoutedEventArgs args)
    {
        try {
            DateTimeOffset? time;
            if (this.LocalSettings.Values.TryGetValue("Time", out object? value)) {
                time = (DateTimeOffset)value;
            } else {
                time = null;
            }
            this.TimeTextBlock.Text = time?.ToString() ?? "(null)";
            this.StatusTextBlock.Text = "Got value shown above from LocalSettings";
        } catch (Exception e) {
            this.StatusTextBlock.Text = $"Exception when getting value, type '{e.GetType().Name}' message '{e.Message}'.";
        }
    }

    private void Restart_Click(object sender, RoutedEventArgs e)
    {
        AppRestartFailureReason failure = Microsoft.Windows.AppLifecycle.AppInstance.Restart("");
        this.StatusTextBlock.Text = $"Restart failed, '{failure.ToString()}'";
    }

Run the app. Click on the Get and Set buttons, observe it works.
Then click on Restart. After the application restart, click on Get or Set and receive an UnauthorizedAccessException.

Expected behavior

Should be able to access LocalSettings after a restart.

Screenshots

No response

NuGet package version

None

Packaging type

Packaged (MSIX)

Windows version

Windows 10 version 22H2 (19045, 2022 Update)

IDE

Visual Studio 2022

Additional context

No response

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the reproduced MainWindow.xaml.cs flow, especially the LocalSettings property and AppInstance.Restart call, using the packaged MSIX WinUI 3 template on Windows 10. Reproduce the restart, trace the resulting UnauthorizedAccessException, and confirm that Get and Set can access LocalSettings after restart without the exception.

Written by the indexing model from the issue text.

Assessment

Tech stack
csharp
Domain
desktop
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.