BlazorDB icon indicating copy to clipboard operation
BlazorDB copied to clipboard

Getting IndexOutOfRange exception when initializing BlazorDB

Open ebekker opened this issue 5 years ago • 8 comments

After adding two records to a StorageSet, during setup of the BrowserServiceProvider, and calling AddBlazorDB, getting an IndexOutOfRangeException:

Here's the full stack trace:

MonoPlatform.ts:70 Uncaught (in promise) Error: System.IndexOutOfRangeException: Index was outside the bounds of the array.
  at BlazorDB.Storage.StorageManagerLoad.GetIdFromString (System.String stringToSearch, System.Int32 startFrom) <0x1b9bc28 + 0x0003c> in <64e9d65131cb416192abd90ed76d00cb>:0 
  at BlazorDB.Storage.StorageManagerLoad.FindIdInSerializedModel (System.String serializedModel) <0x1b9b6c8 + 0x0002a> in <64e9d65131cb416192abd90ed76d00cb>:0 
  at BlazorDB.Storage.StorageManagerLoad.LoadStringModels (System.Type contextType, System.Collections.Generic.IEnumerable`1[T] storageSets) <0x1b79508 + 0x000ea> in <64e9d65131cb416192abd90ed76d00cb>:0 
  at BlazorDB.Storage.StorageManagerLoad.LoadContextFromStorageOrCreateNew (Microsoft.Extensions.DependencyInjection.IServiceCollection serviceCollection, System.Type contextType) <0x1af14b0 + 0x0002c> in <64e9d65131cb416192abd90ed76d00cb>:0 
  at BlazorDB.Storage.StorageManager.LoadContextFromStorageOrCreateNew (Microsoft.Extensions.DependencyInjection.IServiceCollection serviceCollection, System.Type contextType) <0x1af0fa8 + 0x00020> in <64e9d65131cb416192abd90ed76d00cb>:0 
  at BlazorDB.ServiceCollectionExtensions.RegisterBlazorDb (Microsoft.Extensions.DependencyInjection.IServiceCollection serviceCollection, System.Collections.Generic.IEnumerable`1[T] types) <0x1af08c0 + 0x0004e> in <64e9d65131cb416192abd90ed76d00cb>:0 
  at BlazorDB.ServiceCollectionExtensions.Scan (Microsoft.Extensions.DependencyInjection.IServiceCollection serviceCollection, System.Reflection.Assembly assembly) <0x1a3c9a0 + 0x00022> in <64e9d65131cb416192abd90ed76d00cb>:0 
  at BlazorDB.ServiceCollectionExtensions.AddBlazorDB (Microsoft.Extensions.DependencyInjection.IServiceCollection serviceCollection, System.Action`1[T] configure) <0x1a47f28 + 0x0006e> in <64e9d65131cb416192abd90ed76d00cb>:0 
  at ACMEBlazor.Program+<>c.<Main>b__2_0 (Microsoft.Extensions.DependencyInjection.IServiceCollection services) <0x1a47448 + 0x0003c> in <dbc4da5cd36641039edcb92d67a310f6>:0 
  at Microsoft.AspNetCore.Blazor.Browser.Services.BrowserServiceProvider..ctor (System.Action`1[T] configure) <0x18c3fd8 + 0x0003a> in <7da830e0eabd429898584695ab9fd9e5>:0 
  at ACMEBlazor.Program.Main (System.String[] args) <0x18c3928 + 0x00026> in <dbc4da5cd36641039edcb92d67a310f6>:0 
  at BlazorDB.Storage.StorageManagerLoad.GetIdFromString (System.String stringToSearch, System.Int32 startFrom) <0x1b9bc28 + 0x0003c> in <64e9d65131cb416192abd90ed76d00cb>:0 
  at BlazorDB.Storage.StorageManagerLoad.FindIdInSerializedModel (System.String serializedModel) <0x1b9b6c8 + 0x0002a> in <64e9d65131cb416192abd90ed76d00cb>:0 
  at BlazorDB.Storage.StorageManagerLoad.LoadStringModels (System.Type contextType, System.Collections.Generic.IEnumerable`1[T] storageSets) <0x1b79508 + 0x000ea> in <64e9d65131cb416192abd90ed76d00cb>:0 
  at BlazorDB.Storage.StorageManagerLoad.LoadContextFromStorageOrCreateNew (Microsoft.Extensions.DependencyInjection.IServiceCollection serviceCollection, System.Type contextType) <0x1af14b0 + 0x0002c> in <64e9d65131cb416192abd90ed76d00cb>:0 
  at BlazorDB.Storage.StorageManager.LoadContextFromStorageOrCreateNew (Microsoft.Extensions.DependencyInjection.IServiceCollection serviceCollection, System.Type contextType) <0x1af0fa8 + 0x00020> in <64e9d65131cb416192abd90ed76d00cb>:0 
  at BlazorDB.ServiceCollectionExtensions.RegisterBlazorDb (Microsoft.Extensions.DependencyInjection.IServiceCollection serviceCollection, System.Collections.Generic.IEnumerable`1[T] types) <0x1af08c0 + 0x0004e> in <64e9d65131cb416192abd90ed76d00cb>:0 
  at BlazorDB.ServiceCollectionExtensions.Scan (Microsoft.Extensions.DependencyInjection.IServiceCollection serviceCollection, System.Reflection.Assembly assembly) <0x1a3c9a0 + 0x00022> in <64e9d65131cb416192abd90ed76d00cb>:0 
  at BlazorDB.ServiceCollectionExtensions.AddBlazorDB (Microsoft.Extensions.DependencyInjection.IServiceCollection serviceCollection, System.Action`1[T] configure) <0x1a47f28 + 0x0006e> in <64e9d65131cb416192abd90ed76d00cb>:0 
  at ACMEBlazor.Program+<>c.<Main>b__2_0 (Microsoft.Extensions.DependencyInjection.IServiceCollection services) <0x1a47448 + 0x0003c> in <dbc4da5cd36641039edcb92d67a310f6>:0 
  at Microsoft.AspNetCore.Blazor.Browser.Services.BrowserServiceProvider..ctor (System.Action`1[T] configure) <0x18c3fd8 + 0x0003a> in <7da830e0eabd429898584695ab9fd9e5>:0 
  at ACMEBlazor.Program.Main (System.String[] args) <0x18c3928 + 0x00026> in <dbc4da5cd36641039edcb92d67a310f6>:0 
    at Object.callMethod (MonoPlatform.ts:70)
    at Object.callEntryPoint (MonoPlatform.ts:47)
    at Boot.ts:38
    at step (UriHelper.ts:99)
    at Object.next (UriHelper.ts:99)
    at fulfilled (UriHelper.ts:99)

ebekker avatar Jul 12 '18 13:07 ebekker

Hi @ebekker - first thanks for using BlazorDB! Second, can you send me a small code sample to recreate the problem?

chanan avatar Jul 12 '18 14:07 chanan

Some more details about my setup, I'm using the latest BlazorDB at the time, referenced from NuGet: 0.2.0

I basically followed along the sample from the project's readme with my own model objects. Here are my relevant classes:

Context:

    public class Context : StorageContext
    {
        public StorageSet<BlazorAccount> Accounts { get; set; }
        public StorageSet<BlazorOrder> Orders { get; set; }
    }

BlazorAccount:

    public class BlazorAccount
    {
        public int Id { get; set; }

        [Required]
        public AccountDetails Details { get; set; }
    }

BlazorOrder:

   public class BlazorOrder
    {
        public int Id { get; set; }

        [Required]
        public BlazorAccount Account { get; set; }

        [Required]
        public OrderDetails Details { get; set; }
    }

To keep things simple, I haven't included namespaces or the specifics of the nested Details type classes, but if that will help or you think may be part of the problem, I'll be happy to include.

ebekker avatar Jul 12 '18 14:07 ebekker

Actually, you can easily get at the type info for AccountDetails and OrderDetails in the originating repo.

Not sure if it's significant, but these types are themselves simple DTO objects, but they are marked up with Newtonsoft attributes to control serialization.

ebekker avatar Jul 12 '18 14:07 ebekker

@chanan, you beat me to it! Was still working on providing details. Let me know if you need more details, I can actually commit the code base at this point if you want to see what I'm working with?

ebekker avatar Jul 12 '18 14:07 ebekker

@ebekker thanks for the repo details, I am travelling today, so I will look at it tomorrow.

chanan avatar Jul 12 '18 14:07 chanan

Sounds good -- I actually just committed whatever I had at this point so you can see exactly what I was doing, you can see the commit here.

Additionally, in case this is useful, I'm running this in Chrome and here's a snapshot of what my Local Storage Keys look like:

Local Storage Keys
ACMEBlazor.Storage.Context-ACMEBlazor.Storage.BlazorAccount-41d906b8-62fb-4ddf-beb7-c0a9c38d7c6d
ACMEBlazor.Storage.Context-ACMEBlazor.Storage.BlazorAccount-e22820c3-f859-4bc9-905e-38676088283c
ACMEBlazor.Storage.Context-ACMEBlazor.Storage.BlazorAccount-metadata
ACMEBlazor.Storage.Context-ACMEBlazor.Storage.BlazorOrder-metadata

ebekker avatar Jul 12 '18 14:07 ebekker

Looks like this PR in Blazor 0.4.0: https://github.com/aspnet/Blazor/pull/746 camelCased all json property names

Currently I need to string parse the json to do the object relationships, therefore, all the string manipulations broke after 0.4.0. As an aside, it would be great to not do string parsing, but since the only other way I can think about doing it is with Dynamic and that doesn't currently work in Blazor.

Anyway, will fix BlazorDB to adhere to the new Json format and publish a new version.

chanan avatar Jul 15 '18 18:07 chanan

This is taking a bit longer than I thought. I beleive I fixed the initial problem. The issue now is that somethings in the sample apps that I use to test the functions don't work in V0.4 anymore. So, now fixing the sample apps.

chanan avatar Jul 17 '18 19:07 chanan