Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Failure to load in unpackaged MAUI apps #141

Closed
SittenSpynne opened this issue Apr 10, 2024 · 1 comment · Fixed by #143
Closed

Failure to load in unpackaged MAUI apps #141

SittenSpynne opened this issue Apr 10, 2024 · 1 comment · Fixed by #143

Comments

@SittenSpynne
Copy link

SittenSpynne commented Apr 10, 2024

I'm trying to create an unpackaged MAUI app for a certain deployment scenario. Our application uses MonkeyCache. When I start the application, it fails with an InvalidOperationException. The call stack implies it's failing when it uses Windows.Storage.ApplicationData.get_Current(), which I presume requires MSIX packaging to work properly.

I validated this by doing as follows:

  1. Create a new MAUI application using the VS templates.
  2. Edit the .csproj to produce an unpackaged app by adding <WindowsPackageType>None</WindowsPackageType>.
  3. Update the launchSettings.json file to not use package actions by changing command to "Project".
  4. Add a bit of code to MainPage.xaml.cs:
        protected override void OnAppearing()
        {
            base.OnAppearing();

            try
            {
                MonkeyCache.SQLite.Barrel.ApplicationId = "MauiApp1";

                var currentBarrel = Barrel.Current;
                
            }
            catch (Exception ex)
            {
                DisplayAlert("Error", ex.Message, "OK");
                throw;
            }
        }

The call stack:

System.InvalidOperationException: Operation is not valid due to the current state of the object.
   at WinRT.ExceptionHelpers.<ThrowExceptionForHR>g__Throw|39_0(Int32 hr)
   at WinRT.ExceptionHelpers.ThrowExceptionForHR(Int32 hr)
   at ABI.Windows.Storage.IApplicationDataStaticsMethods.get_Current(IObjectReference _obj)
   at Windows.Storage.ApplicationData.get_Current()
   at MonkeyCache.BarrelUtils.GetBasePath(String applicationId)
   at MonkeyCache.SQLite.MonkeyBarrel.<>c.<.cctor>b__32_0()
   at System.Lazy`1.ViaFactory(LazyThreadSafetyMode mode)
   at System.Lazy`1.ExecutionAndPublication(LazyHelper executionAndPublication, Boolean useDefaultConstructor)
   at System.Lazy`1.CreateValue()
   at MonkeyCache.SQLite.MonkeyBarrel..ctor(String cacheDirectory)
   at MonkeyCache.SQLite.MonkeyBarrel.get_Current()
   at [my redacted call site]

I'm looking at the code and the most inelegant fix would be a try..catch in the #if WINDOWS section. If it used the Environment.GetFolderPath() method as a fallback that would work here. I'm not sure if there's a way this code could tell if it's been packaged or not so that might be the solution?

@jamesmontemagno
Copy link
Owner

work around is to set your own path

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants