G.Maltby
G.Maltby

Reputation: 66

Xamrin forms System.TypeInitializationException

Hi I am getting this exception when using Azure mobile services offline store when debugging in Android. It works fine when using windows though.

Here is the code, just based on the tod0list example

    static StaffManager defaultInstance = new StaffManager();
    MobileServiceClient client;

    IMobileServiceSyncTable<Staff> staffTable;

    private StaffManager()
    {
        client = new MobileServiceClient(Constants.ApplicationURL);

            var store = new MobileServiceSQLiteStore("localstore1.db");
            store.DefineTable<Staff>();

            this.client.SyncContext.InitializeAsync(store);

            this.staffTable = client.GetSyncTable<Staff>();

    }

This is the exception:

System.TypeInitializationException: The type initializer for 'OrchardApp.StaffManager' threw an exception.

09-26 11:05:48.991 D/Mono ( 1684): DllImport attempting to load: '/system/lib/liblog.so'. 09-26 11:05:48.991 D/Mono ( 1684): DllImport loaded library '/system/lib/liblog.so'. 09-26 11:05:48.991 D/Mono ( 1684): DllImport searching in: '/system/lib/liblog.so' ('/system/lib/liblog.so'). 09-26 11:05:48.991 D/Mono ( 1684): Searching for '__android_log_print'. 09-26 11:05:48.991 D/Mono ( 1684): Probing '__android_log_print'. 09-26 11:05:48.991 D/Mono ( 1684): Found as '__android_log_print'. 09-26 11:05:48.991 I/MonoDroid( 1684): UNHANDLED EXCEPTION: 09-26 11:05:48.991 I/MonoDroid( 1684): System.TypeInitializationException: The type initializer for 'OrchardApp.StaffManager' threw an exception. ---> Microsoft.WindowsAzure.MobileServices.SQLiteStore.SQLiteException: Error executing SQLite command: 'unable to open database file'. 09-26 11:05:48.991 I/MonoDroid( 1684): at Microsoft.WindowsAzure.MobileServices.SQLiteStore.SQLitePCLRawHelpers.VerifySQLiteResponse (Int32 result, Int32 expectedResult, SQLitePCL.sqlite3 db) [0x00024] in :0 09-26 11:05:48.991 I/MonoDroid( 1684): at Microsoft.WindowsAzure.MobileServices.SQLiteStore.SQLitePCLRawHelpers.GetSqliteConnection (System.String filename) [0x0000d] in :0 09-26 11:05:48.991 I/MonoDroid( 1684): at Microsoft.WindowsAzure.MobileServices.SQLiteStore.MobileServiceSQLiteStore..ctor (System.String fileName) [0x00039] in :0 09-26 11:05:48.991 I/MonoDroid( 1684): at OrchardApp.StaffManager..ctor () [0x0001e] in C:\OPAC\OrchardApp\OrchardApp\OrchardApp\Services\StaffManager.cs:27 09-26 11:05:48.991 I/MonoDroid( 1684): at OrchardApp.StaffManager..cctor () [0x00000] in C:\OPAC\OrchardApp\OrchardApp\OrchardApp\Services\StaffManager.cs:16 09-26 11:05:48.991 I/MonoDroid( 1684): --- End of inner exception stack trace --- 09-26 11:05:48.991 I/MonoDroid( 1684): at OrchardApp.TodoList..ctor () [0x0000f] in C:\OPAC\OrchardApp\OrchardApp\OrchardApp\Views\TodoList.xaml.cs:18 09-26 11:05:48.991 I/MonoDroid( 1684): at OrchardApp.HomePage+<<-ctor>b__0_1>d.MoveNext () [0x0000f] in C:\OPAC\OrchardApp\OrchardApp\OrchardApp\Views\HomePage.xaml.cs:26 09-26 11:05:48.991 I/MonoDroid( 1684): --- End of stack trace from previous location where exception was thrown --- 09-26 11:05:48.991 I/MonoDroid( 1684): at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw () [0x0000c] in /Users/builder/data/lanes/3415/7db2aac3/source/mono/external/referencesource/mscorlib/system/runtime/exceptionservices/exceptionservicescommon.cs:143 09-26 11:05:48.995 I/MonoDroid( 1684): at System.Runtime.CompilerServices.AsyncMethodBuilderCore.m__0 (System.Object state) [0x00000] in /Users/builder/data/lanes/3415/7db2aac3/source/mono/external/referencesource/mscorlib/system/runtime/compilerservices/AsyncMethodBuilder.cs:1018 09-26 11:05:48.995 I/MonoDroid( 1684): at Android.App.SyncContext+c__AnonStorey0.<>m__0 () [0x00000] in /Users/builder/data/lanes/3415/7db2aac3/source/monodroid/src/Mono.Android/src/Android.App/SyncContext.cs:18 09-26 11:05:48.995 I/MonoDroid( 1684): at Java.Lang.Thread+RunnableImplementor.Run () [0x0000b] in /Users/builder/data/lanes/3415/7db2aac3/source/monodroid/src/Mono.Android/src/Java.Lang/Thread.cs:36 09-26 11:05:48.995 I/MonoDroid( 1684): at Java.Lang.IRunnableInvoker.n_Run (IntPtr jnienv, IntPtr native__this) [0x00009] in /Users/builder/data/lanes/3415/7db2aac3/source/monodroid/src/Mono.Android/platforms/android-23/src/generated/Java.Lang.IRunnable.cs:81 09-26 11:05:48.995 I/MonoDroid( 1684): at (wrapper dynamic-method) System.Object:578fa2b5-486e-424f-9b2b-bb0dcaf1a772 (intptr,intptr) 09-26 11:05:49.003 D/Mono ( 1684): DllImport searching in: '__Internal' ('(null)'). 09-26 11:05:49.003 D/Mono ( 1684): Searching for 'java_interop_jnienv_throw'. 09-26 11:05:49.003 D/Mono ( 1684): Probing 'java_interop_jnienv_throw'. 09-26 11:05:49.003 D/Mono ( 1684): Found as 'java_interop_jnienv_throw'. An unhandled exception occured.

Upvotes: 2

Views: 944

Answers (2)

fileman
fileman

Reputation: 33

Similar problem with version 3.0.0 in PCL project I have resolved with this workaround

PCL Project App.cs

        public static string Path = "MyLocalStore.db";

Android Project MainActivity.cs nel metodo OnCreate

        //Call this in each platform before intializing your Mobile Client
        SQLitePCL.Batteries.Init();

        App.Path = Path.Combine(System.Environment.GetFolderPath(System.Environment.SpecialFolder.Personal), App.Path);

        if (!File.Exists(App.Path))
        {
            File.Create(App.Path).Dispose();
        }

Finally

        var store = new MobileServiceSQLiteStore(App.Path);

Upvotes: 0

G.Maltby
G.Maltby

Reputation: 66

See comment from Thomas Hagstrom. https://forums.xamarin.com/discussion/78936/microsoft-azure-mobile-client-sqlitestore-upgraded-to-3-0-get-error-unable-to-open-database-file

Downgrading to version 2.1.1 solved the problem.

Upvotes: 1

Related Questions