couchbase / couchbase-lite-net

A lightweight, document-oriented (NoSQL), syncable database engine for .NET
http://developer.couchbase.com/mobile/
Apache License 2.0
438 stars 126 forks source link

instantiating a DB instance fails for 3.2+ #1679

Open tejs-code opened 1 week ago

tejs-code commented 1 week ago

Library Version

3.2.1

.NET Runtime

.Net 8

Operating System / Device Details

Windows 11 24H2

Details

I have a .net WPF project that uses couchbase and was attempting to upgrade it from 3.1.9 to 3.2.1 but creating the database object fails. The target framework for it is net8.0-windows10.0.19041.0.

Expected behavior

call to new Couchbase.Lite.Database("dbname") should succeed

Actual behavior

throws exception System.InvalidOperationException: 'Operation is not valid due to the current state of the object.'

Callstack [Exception] WinRT.Runtime.dll!WinRT.ExceptionHelpers.ThrowExceptionForHR.__Throw|38_0(int hr) Unknown [Exception] WinRT.Runtime.dll!WinRT.ExceptionHelpers.ThrowExceptionForHR(int hr) Unknown [Exception] Microsoft.Windows.SDK.NET.dll!ABI.Windows.Storage.IApplicationDataStaticsMethods.get_Current(WinRT.IObjectReference _obj) Unknown [Exception] Microsoft.Windows.SDK.NET.dll!Windows.Storage.ApplicationData.Current.get() Unknown [Exception] Couchbase.Lite.dll!Couchbase.Lite.Support.DefaultDirectoryResolver.DefaultDirectory() Unknown [Exception] Couchbase.Lite.dll!Couchbase.Lite.DatabaseConfiguration.DatabaseConfiguration() Unknown

I don't see the error if the targetframework is just 'net8.0-windows'

In my case, I know what the directory I want to specify, so I init the DB using something like this new Couchbase.Lite.Database(dbName, new Couchbase.Lite.DatabaseConfiguration() { Directory = directory }); _directory in DatabaseConfiguration could be made into a lazy eval to skip the logic completely unless a default is not provided.

Steps To Reproduce

  1. Create a blank WPF project
  2. Update target framework in csproj file to net8.0-windows10.0.19041.0
  3. add this line of code " var db = new Couchbase.Lite.Database("dbName");
  4. Run. Sample project attached. WpfApp1.zip
borrrden commented 1 week ago

I suppose this is because WPF creates "unpackaged" apps right? I've only ever tested WinUI with packaged apps. I can probably change the evaluation to be lazy but it won't fix the underlying problem for most people. I wonder if there is a way to detect packaged vs unpackaged or if I should just try and catch the exception....

borrrden commented 1 week ago

Opened CBL-6481 to address this.

tejs-code commented 1 week ago

Yup. WPF by default runs as unpackaged, though it can be packaged as well. https://learn.microsoft.com/en-us/windows/msix/detect-package-identity has some pointers on figuring out if the process is running as packaged.

Lazy eval would atleast bypass this exception when a default is already provided. I was unable to test out if anything else would break after since I couldn't get past DB init.

borrrden commented 1 week ago

There is one thing that you can try doing to get unblocked. Since the default directory resolver is a public interface you could register your own implementation of Couchbase.Lite.DI.IDefaultDirectoryResolver via Couchbase.Lite.DI.Service.Register. I haven't tried doing that but I think it is set up to allow late registrations. It could return any old thing if you are not using it.