LostBeard / SpawnDev.BlazorJS

Full Blazor WebAssembly and Javascript interop. Supports all Javascript data types and web browser APIs.
https://blazorjs.spawndev.com
MIT License
111 stars 7 forks source link

The non-public property '_callback' on type 'SpawnDev.BlazorJS.Callback' is annotated with 'JsonIncludeAttribute' which is invalid. #39

Closed PineCone5 closed 3 months ago

PineCone5 commented 3 months ago

Describe the bug According to the instructions, the following snippet is added to the 'Counter' component :

   protected override Task OnInitializedAsync()
    {
        Js.Set("testCallback", Callback.Create((string strArg) => {
            Console.WriteLine($"Javascript sent: {strArg}");
            // this prints "Hello callback!"
        }));
        return base.OnInitializedAsync();
    }

But when run it, get an error with the following message:

Microsoft.AspNetCore.Components.WebAssembly.Rendering.WebAssemblyRenderer[100]
      Unhandled exception rendering component: The non-public property '_callback' on type 'SpawnDev.BlazorJS.Callback' is annotated with 'JsonIncludeAttribute' which is invalid.
System.InvalidOperationException: The non-public property '_callback' on type 'SpawnDev.BlazorJS.Callback' is annotated with 'JsonIncludeAttribute' which is invalid.
   at System.Text.Json.ThrowHelper.ThrowInvalidOperationException_JsonIncludeOnNonPublicInvalid(String memberName, Type declaringType)
   at System.Text.Json.Serialization.Metadata.JsonTypeInfo..ctor(Type type, JsonConverter converter, Type runtimeType, JsonSerializerOptions options)
   at System.Text.Json.Serialization.Metadata.JsonTypeInfo..ctor(Type type, JsonSerializerOptions options)
   at System.Text.Json.JsonSerializerOptions.<InitializeForReflectionSerializer>g__CreateJsonTypeInfo|112_0(Type type, JsonSerializerOptions options)
   at System.Text.Json.JsonSerializerOptions.GetClassFromContextOrCreate(Type type)
   at System.Text.Json.JsonSerializerOptions.GetOrAddClass(Type type)
   at System.Text.Json.WriteStackFrame.InitializeReEntry(Type type, JsonSerializerOptions options)
   at System.Text.Json.Serialization.JsonConverter`1[[System.Object, System.Private.CoreLib, Version=6.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e]].TryWrite(Utf8JsonWriter writer, Object& value, JsonSerializerOptions options, WriteStack& state)
   at System.Text.Json.Serialization.Converters.ArrayConverter`2[[System.Object[], System.Private.CoreLib, Version=6.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e],[System.Object, System.Private.CoreLib, Version=6.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e]].OnWriteResume(Utf8JsonWriter writer, Object[] array, JsonSerializerOptions options, WriteStack& state)
   at System.Text.Json.Serialization.JsonCollectionConverter`2[[System.Object[], System.Private.CoreLib, Version=6.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e],[System.Object, System.Private.CoreLib, Version=6.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e]].OnTryWrite(Utf8JsonWriter writer, Object[] value, JsonSerializerOptions options, WriteStack& state)
   at System.Text.Json.Serialization.JsonConverter`1[[System.Object[], System.Private.CoreLib, Version=6.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e]].TryWrite(Utf8JsonWriter writer, Object[]& value, JsonSerializerOptions options, WriteStack& state)
   at System.Text.Json.Serialization.JsonConverter`1[[System.Object[], System.Private.CoreLib, Version=6.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e]].TryWriteAsObject(Utf8JsonWriter writer, Object value, JsonSerializerOptions options, WriteStack& state)
   at System.Text.Json.Serialization.JsonConverter`1[[System.Object, System.Private.CoreLib, Version=6.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e]].TryWrite(Utf8JsonWriter writer, Object& value, JsonSerializerOptions options, WriteStack& state)
   at System.Text.Json.Serialization.Converters.ArrayConverter`2[[System.Object[], System.Private.CoreLib, Version=6.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e],[System.Object, System.Private.CoreLib, Version=6.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e]].OnWriteResume(Utf8JsonWriter writer, Object[] array, JsonSerializerOptions options, WriteStack& state)
   at System.Text.Json.Serialization.JsonCollectionConverter`2[[System.Object[], System.Private.CoreLib, Version=6.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e],[System.Object, System.Private.CoreLib, Version=6.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e]].OnTryWrite(Utf8JsonWriter writer, Object[] value, JsonSerializerOptions options, WriteStack& state)
   at System.Text.Json.Serialization.JsonConverter`1[[System.Object[], System.Private.CoreLib, Version=6.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e]].TryWrite(Utf8JsonWriter writer, Object[]& value, JsonSerializerOptions options, WriteStack& state)
   at System.Text.Json.Serialization.JsonConverter`1[[System.Object[], System.Private.CoreLib, Version=6.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e]].WriteCore(Utf8JsonWriter writer, Object[]& value, JsonSerializerOptions options, WriteStack& state)
   at System.Text.Json.JsonSerializer.WriteUsingSerializer[Object[]](Utf8JsonWriter writer, Object[]& value, JsonTypeInfo jsonTypeInfo)
   at System.Text.Json.JsonSerializer.WriteStringUsingSerializer[Object[]](Object[]& value, JsonTypeInfo jsonTypeInfo)
   at System.Text.Json.JsonSerializer.Serialize[Object[]](Object[] value, JsonSerializerOptions options)
   at Microsoft.JSInterop.JSInProcessRuntime.Invoke[IJSVoidResult](String identifier, Int64 targetInstanceId, Object[] args)
   at Microsoft.JSInterop.JSInProcessRuntime.Invoke[IJSVoidResult](String identifier, Object[] args)
   at Microsoft.JSInterop.JSInProcessRuntimeExtensions.InvokeVoid(IJSInProcessRuntime jsRuntime, String identifier, Object[] args)
   at SpawnDev.BlazorJS.BlazorJSInterop._invokeVoid(String fnName, Object[] args) in D:\users\tj\Projects\SpawnDev.BlazorJS\SpawnDev.BlazorJS\SpawnDev.BlazorJS\BlazorJSInterop.cs:line 290
   at SpawnDev.BlazorJS.BlazorJSInterop.invokeVoid(String fnName, Object arg1, Object arg2) in D:\users\tj\Projects\SpawnDev.BlazorJS\SpawnDev.BlazorJS\SpawnDev.BlazorJS\BlazorJSInterop.cs:line 274
   at SpawnDev.BlazorJS.BlazorJSInterop.GlobalPropertySet(Object key, Object value) in D:\users\tj\Projects\SpawnDev.BlazorJS\SpawnDev.BlazorJS\SpawnDev.BlazorJS\BlazorJSInterop.cs:line 85
   at SpawnDev.BlazorJS.BlazorJSRuntime.Set(String key, Object value) in D:\users\tj\Projects\SpawnDev.BlazorJS\SpawnDev.BlazorJS\SpawnDev.BlazorJS\BlazorJSRuntimeExt.cs:line 55
   at BlazorApp2.Client.Pages.Counter.OnInitializedAsync() in C:\Users\admin\source\repos\BlazorApp2\BlazorApp2\Client\Pages\Counter.razor:line 16
   at Microsoft.AspNetCore.Components.ComponentBase.RunInitAndSetParametersAsync()

To Reproduce Steps to reproduce the behavior:

  1. Create a Blazor WebAssembly App using visual studio 2022.
  2. Add 'SpawnDev.BlazorJS' nuget package.
  3. Override 'OnInitializedAsync' in the 'Counter' component .
  4. Run the app.
  5. See error

Expected behavior Follow the instructions to work properly.

Screenshots image

image

Desktop (please complete the following information):

Smartphone (please complete the following information):

Additional context Add any other context about the problem here.

LostBeard commented 3 months ago

Thank you. As you found, JsonIncludeAtribute is not valid on non-public properties in .Net 6 (and apparently also .Net 7.) I did not catch it because it does not throw a build error and it is valid on non-public properties in .Net 8.... which is where I have been working with Blazor JS the most.

I will get this fixed now, and check if I made this same mistake in any other places in the project.

Thank you for reporting this. 🚀

LostBeard commented 3 months ago

I found where this is mentioned. Adding for reference.

JsonIncludeAttribute class

In .NET 6 and .NET 7, non-public properties aren't supported.

Fixed in version 2.5.7

PineCone5 commented 3 months ago

Thanks for your help! I have updated the version I used, and the program works fine. Thanks!