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

System.InvalidCastException: Arg_InvalidCastException after upgrading blazor webassembly from .net 6 to .net 7 #78864

Closed
UserIsAmitHasan opened this issue Nov 26, 2022 · 14 comments
Labels
area-System.Text.Json needs-author-action An issue or pull request that requires more info or actions from the author. no-recent-activity

Comments

@UserIsAmitHasan
Copy link

Description

This exception only shows in production randomly. Blazor Webassembly app throws this exception randomly when an API request is made. Most of the time it works. Please check the stacktraces. All API call in browser is handled by the following method:

public abstract class BaseController
 {
     protected readonly string ControllerName;
     protected readonly string BaseLink = "api";
     protected readonly HttpClient http;

     public BaseController(string ControllerName, HttpClient client)
     {
         this.ControllerName = ControllerName;
         http = client;
     }

     protected async ValueTask<TRet> Request<TRet>(string actionName, object input)
     {
         JsonSerializerOptions options = new JsonSerializerOptions()
         {
             ReferenceHandler = System.Text.Json.Serialization.ReferenceHandler.IgnoreCycles,
         };
         HttpResponseMessage response = await http.PostAsJsonAsync($"{BaseLink}/{ControllerName}/{actionName}", input, options);
         response.EnsureSuccessStatusCode();

         var result = await response.Content.ReadFromJsonAsync<TRet>();
         return result;
     }

 }

Reproduction Steps

I have not been able to reproduce this problem in development environment. Stacktrace should provide some clue.

Expected behavior

Code should work without InvalidCastException

Actual behavior

Here are the stacktraces from browser console:
Stacktrace 1:
Microsoft.AspNetCore.Components.WebAssembly.Rendering.WebAssemblyRenderer[100]
Unhandled exception rendering component: Arg_InvalidCastException
System.InvalidCastException: Arg_InvalidCastException
at System.Object.CustomSerialNoGetter(Object )
at System.Text.Json.Serialization.JsonConverter1[[Calventory.Web.Domain.Item, Calventory.Web.Domain, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null]].WriteCore(Utf8JsonWriter , Item& , JsonSerializerOptions , WriteStack& ) at System.Text.Json.JsonSerializer.<WriteStreamAsync>d__1211[[System.Object, System.Private.CoreLib, Version=7.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e]].MoveNext()
at System.Object.CustomSerialNoGetter(Object )
at System.Text.Json.Serialization.JsonConverter1[[Calventory.Web.Domain.Item, Calventory.Web.Domain, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null]].WriteCore(Utf8JsonWriter , Item& , JsonSerializerOptions , WriteStack& ) at System.Text.Json.JsonSerializer.<WriteStreamAsync>d__1211[[System.Object, System.Private.CoreLib, Version=7.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e]].MoveNext()
at System.Object.CustomSerialNoGetter(Object )
at System.Text.Json.Serialization.JsonConverter1[[Calventory.Web.Domain.Item, Calventory.Web.Domain, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null]].WriteCore(Utf8JsonWriter , Item& , JsonSerializerOptions , WriteStack& ) at System.Text.Json.JsonSerializer.<WriteStreamAsync>d__1211[[System.Object, System.Private.CoreLib, Version=7.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e]].MoveNext()
at System.Object.CustomSerialNoGetter(Object )
at System.Text.Json.Serialization.JsonConverter1[[Calventory.Web.Domain.Item, Calventory.Web.Domain, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null]].WriteCore(Utf8JsonWriter , Item& , JsonSerializerOptions , WriteStack& ) at System.Net.Http.Json.JsonContent.SerializeToStreamAsyncCore(Stream , Boolean , CancellationToken ) at System.Object.CustomSerialNoGetter(Object ) at System.Text.Json.Serialization.JsonConverter1[[Calventory.Web.Domain.Item, Calventory.Web.Domain, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null]].WriteCore(Utf8JsonWriter , Item& , JsonSerializerOptions , WriteStack& )
at System.Net.Http.HttpContent.LoadIntoBufferAsyncCore(Task , MemoryStream )
at System.Object.CustomSerialNoGetter(Object )
at System.Text.Json.Serialization.JsonConverter1[[Calventory.Web.Domain.Item, Calventory.Web.Domain, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null]].WriteCore(Utf8JsonWriter , Item& , JsonSerializerOptions , WriteStack& ) at System.Net.Http.HttpContent.<WaitAndReturnAsync>d__822[[System.Net.Http.HttpContent, System.Net.Http, Version=7.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a],[System.Byte[], System.Private.CoreLib, Version=7.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e]].MoveNext()
at System.Object.CustomSerialNoGetter(Object )
at System.Text.Json.Serialization.JsonConverter1[[Calventory.Web.Domain.Item, Calventory.Web.Domain, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null]].WriteCore(Utf8JsonWriter , Item& , JsonSerializerOptions , WriteStack& ) at System.Net.Http.BrowserHttpHandler.CallFetch(HttpRequestMessage , CancellationToken , Nullable1 )
at System.Object.CustomSerialNoGetter(Object )
at System.Text.Json.Serialization.JsonConverter1[[Calventory.Web.Domain.Item, Calventory.Web.Domain, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null]].WriteCore(Utf8JsonWriter , Item& , JsonSerializerOptions , WriteStack& ) at System.Net.Http.BrowserHttpHandler.<SendAsync>g__Impl|55_0(HttpRequestMessage , CancellationToken , Nullable1 )
at System.Object.CustomSerialNoGetter(Object )
at System.Text.Json.Serialization.JsonConverter1[[Calventory.Web.Domain.Item, Calventory.Web.Domain, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null]].WriteCore(Utf8JsonWriter , Item& , JsonSerializerOptions , WriteStack& ) at System.Net.Http.HttpClient.<SendAsync>g__Core|83_0(HttpRequestMessage , HttpCompletionOption , CancellationTokenSource , Boolean , CancellationTokenSource , CancellationToken ) at System.Object.CustomSerialNoGetter(Object ) at System.Text.Json.Serialization.JsonConverter1[[Calventory.Web.Domain.Item, Calventory.Web.Domain, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null]].WriteCore(Utf8JsonWriter , Item& , JsonSerializerOptions , WriteStack& )
at Calventory.Web.Client.BaseController.d__51[[System.Int32, System.Private.CoreLib, Version=7.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e]].MoveNext() at System.Object.CustomSerialNoGetter(Object ) at System.Text.Json.Serialization.JsonConverter1[[Calventory.Web.Domain.Item, Calventory.Web.Domain, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null]].WriteCore(Utf8JsonWriter , Item& , JsonSerializerOptions , WriteStack& )
at Calventory.Web.Client.ItemState.CreateOrUpdate()
at System.Object.CustomSerialNoGetter(Object )
at System.Text.Json.Serialization.JsonConverter1[[Calventory.Web.Domain.Item, Calventory.Web.Domain, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null]].WriteCore(Utf8JsonWriter , Item& , JsonSerializerOptions , WriteStack& ) at Microsoft.AspNetCore.Components.ComponentBase.CallStateHasChangedOnAsyncCompletion(Task task) at System.Object.CustomSerialNoGetter(Object ) at System.Text.Json.Serialization.JsonConverter1[[Calventory.Web.Domain.Item, Calventory.Web.Domain, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null]].WriteCore(Utf8JsonWriter , Item& , JsonSerializerOptions , WriteStack& )
at Microsoft.AspNetCore.Components.Forms.EditForm.HandleSubmitAsync()
at System.Object.CustomSerialNoGetter(Object )
at System.Text.Json.Serialization.JsonConverter1[[Calventory.Web.Domain.Item, Calventory.Web.Domain, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null]].WriteCore(Utf8JsonWriter , Item& , JsonSerializerOptions , WriteStack& ) at Microsoft.AspNetCore.Components.ComponentBase.CallStateHasChangedOnAsyncCompletion(Task task) at System.Object.CustomSerialNoGetter(Object ) at System.Text.Json.Serialization.JsonConverter1[[Calventory.Web.Domain.Item, Calventory.Web.Domain, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null]].WriteCore(Utf8JsonWriter , Item& , JsonSerializerOptions , WriteStack& )
at Microsoft.AspNetCore.Components.RenderTree.Renderer.GetErrorHandledTask(Task , ComponentState )

Stacktrace 2:
Microsoft.AspNetCore.Components.WebAssembly.Rendering.WebAssemblyRenderer[100]
Unhandled exception rendering component: Arg_InvalidCastException
System.InvalidCastException: Arg_InvalidCastException
at System.Object.GuidGetter(Object )
at System.Text.Json.Serialization.JsonConverter1[[Calventory.Web.Domain.Account, Calventory.Web.Domain, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null]].WriteCore(Utf8JsonWriter , Account& , JsonSerializerOptions , WriteStack& ) at System.Text.Json.JsonSerializer.<WriteStreamAsync>d__1211[[System.Object, System.Private.CoreLib, Version=7.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e]].MoveNext()
at System.Object.GuidGetter(Object )
at System.Text.Json.Serialization.JsonConverter1[[Calventory.Web.Domain.Account, Calventory.Web.Domain, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null]].WriteCore(Utf8JsonWriter , Account& , JsonSerializerOptions , WriteStack& ) at System.Text.Json.JsonSerializer.<WriteStreamAsync>d__1211[[System.Object, System.Private.CoreLib, Version=7.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e]].MoveNext()
at System.Object.GuidGetter(Object )
at System.Text.Json.Serialization.JsonConverter1[[Calventory.Web.Domain.Account, Calventory.Web.Domain, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null]].WriteCore(Utf8JsonWriter , Account& , JsonSerializerOptions , WriteStack& ) at System.Text.Json.JsonSerializer.<WriteStreamAsync>d__1211[[System.Object, System.Private.CoreLib, Version=7.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e]].MoveNext()
at System.Object.GuidGetter(Object )
at System.Text.Json.Serialization.JsonConverter1[[Calventory.Web.Domain.Account, Calventory.Web.Domain, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null]].WriteCore(Utf8JsonWriter , Account& , JsonSerializerOptions , WriteStack& ) at System.Net.Http.Json.JsonContent.SerializeToStreamAsyncCore(Stream , Boolean , CancellationToken ) at System.Object.GuidGetter(Object ) at System.Text.Json.Serialization.JsonConverter1[[Calventory.Web.Domain.Account, Calventory.Web.Domain, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null]].WriteCore(Utf8JsonWriter , Account& , JsonSerializerOptions , WriteStack& )
at System.Net.Http.HttpContent.LoadIntoBufferAsyncCore(Task , MemoryStream )
at System.Object.GuidGetter(Object )
at System.Text.Json.Serialization.JsonConverter1[[Calventory.Web.Domain.Account, Calventory.Web.Domain, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null]].WriteCore(Utf8JsonWriter , Account& , JsonSerializerOptions , WriteStack& ) at System.Net.Http.HttpContent.<WaitAndReturnAsync>d__822[[System.Net.Http.HttpContent, System.Net.Http, Version=7.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a],[System.Byte[], System.Private.CoreLib, Version=7.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e]].MoveNext()
at System.Object.GuidGetter(Object )
at System.Text.Json.Serialization.JsonConverter1[[Calventory.Web.Domain.Account, Calventory.Web.Domain, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null]].WriteCore(Utf8JsonWriter , Account& , JsonSerializerOptions , WriteStack& ) at System.Net.Http.BrowserHttpHandler.CallFetch(HttpRequestMessage , CancellationToken , Nullable1 )
at System.Object.GuidGetter(Object )
at System.Text.Json.Serialization.JsonConverter1[[Calventory.Web.Domain.Account, Calventory.Web.Domain, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null]].WriteCore(Utf8JsonWriter , Account& , JsonSerializerOptions , WriteStack& ) at System.Net.Http.BrowserHttpHandler.<SendAsync>g__Impl|55_0(HttpRequestMessage , CancellationToken , Nullable1 )
at System.Object.GuidGetter(Object )
at System.Text.Json.Serialization.JsonConverter1[[Calventory.Web.Domain.Account, Calventory.Web.Domain, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null]].WriteCore(Utf8JsonWriter , Account& , JsonSerializerOptions , WriteStack& ) at System.Net.Http.HttpClient.<SendAsync>g__Core|83_0(HttpRequestMessage , HttpCompletionOption , CancellationTokenSource , Boolean , CancellationTokenSource , CancellationToken ) at System.Object.GuidGetter(Object ) at System.Text.Json.Serialization.JsonConverter1[[Calventory.Web.Domain.Account, Calventory.Web.Domain, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null]].WriteCore(Utf8JsonWriter , Account& , JsonSerializerOptions , WriteStack& )
at Calventory.Web.Client.BaseController.d__51[[System.Int32, System.Private.CoreLib, Version=7.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e]].MoveNext() at System.Object.GuidGetter(Object ) at System.Text.Json.Serialization.JsonConverter1[[Calventory.Web.Domain.Account, Calventory.Web.Domain, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null]].WriteCore(Utf8JsonWriter , Account& , JsonSerializerOptions , WriteStack& )
at Calventory.Web.Client.AccountDialog.CreateOrUpdate()
at System.Object.GuidGetter(Object )
at System.Text.Json.Serialization.JsonConverter1[[Calventory.Web.Domain.Account, Calventory.Web.Domain, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null]].WriteCore(Utf8JsonWriter , Account& , JsonSerializerOptions , WriteStack& ) at Microsoft.AspNetCore.Components.ComponentBase.CallStateHasChangedOnAsyncCompletion(Task task) at System.Object.GuidGetter(Object ) at System.Text.Json.Serialization.JsonConverter1[[Calventory.Web.Domain.Account, Calventory.Web.Domain, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null]].WriteCore(Utf8JsonWriter , Account& , JsonSerializerOptions , WriteStack& )
at MudBlazor.MudBaseButton.OnClickHandler(MouseEventArgs ev)
at System.Object.GuidGetter(Object )
at System.Text.Json.Serialization.JsonConverter1[[Calventory.Web.Domain.Account, Calventory.Web.Domain, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null]].WriteCore(Utf8JsonWriter , Account& , JsonSerializerOptions , WriteStack& ) at Microsoft.AspNetCore.Components.ComponentBase.CallStateHasChangedOnAsyncCompletion(Task task) at System.Object.GuidGetter(Object ) at System.Text.Json.Serialization.JsonConverter1[[Calventory.Web.Domain.Account, Calventory.Web.Domain, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null]].WriteCore(Utf8JsonWriter , Account& , JsonSerializerOptions , WriteStack& )
at Microsoft.AspNetCore.Components.RenderTree.Renderer.GetErrorHandledTask(Task , ComponentState )

Regression?

It works in .net 6. It also works in .net 7 most of the time.

Known Workarounds

Reloading the page solves the problem.

Configuration

.Net Version : .net 7,
Browser : Chrome, Edge

Other information

No response

@ghost ghost added the untriaged New issue has not been triaged by the area owner label Nov 26, 2022
@ghost
Copy link

ghost commented Nov 26, 2022

Tagging subscribers to this area: @dotnet/area-system-text-json, @gregsdennis
See info in area-owners.md if you want to be subscribed.

Issue Details

Description

This exception only shows in production randomly. Blazor Webassembly app throws this exception randomly when an API request is made. Most of the time it works. Please check the stacktraces. All API call in browser is handled by the following method:

public abstract class BaseController
 {
     protected readonly string ControllerName;
     protected readonly string BaseLink = "api";
     protected readonly HttpClient http;

     public BaseController(string ControllerName, HttpClient client)
     {
         this.ControllerName = ControllerName;
         http = client;
     }

     protected async ValueTask<TRet> Request<TRet>(string actionName, object input)
     {
         JsonSerializerOptions options = new JsonSerializerOptions()
         {
             ReferenceHandler = System.Text.Json.Serialization.ReferenceHandler.IgnoreCycles,
         };
         HttpResponseMessage response = await http.PostAsJsonAsync($"{BaseLink}/{ControllerName}/{actionName}", input, options);
         response.EnsureSuccessStatusCode();

         var result = await response.Content.ReadFromJsonAsync<TRet>();
         return result;
     }

 }

Reproduction Steps

I have not been able to reproduce this problem in development environment. Stacktrace should provide some clue.

Expected behavior

Code should work without InvalidCastException

Actual behavior

Here are the stacktraces from browser console:
Stacktrace 1:
Microsoft.AspNetCore.Components.WebAssembly.Rendering.WebAssemblyRenderer[100]
Unhandled exception rendering component: Arg_InvalidCastException
System.InvalidCastException: Arg_InvalidCastException
at System.Object.CustomSerialNoGetter(Object )
at System.Text.Json.Serialization.JsonConverter1[[Calventory.Web.Domain.Item, Calventory.Web.Domain, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null]].WriteCore(Utf8JsonWriter , Item& , JsonSerializerOptions , WriteStack& ) at System.Text.Json.JsonSerializer.<WriteStreamAsync>d__1211[[System.Object, System.Private.CoreLib, Version=7.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e]].MoveNext()
at System.Object.CustomSerialNoGetter(Object )
at System.Text.Json.Serialization.JsonConverter1[[Calventory.Web.Domain.Item, Calventory.Web.Domain, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null]].WriteCore(Utf8JsonWriter , Item& , JsonSerializerOptions , WriteStack& ) at System.Text.Json.JsonSerializer.<WriteStreamAsync>d__1211[[System.Object, System.Private.CoreLib, Version=7.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e]].MoveNext()
at System.Object.CustomSerialNoGetter(Object )
at System.Text.Json.Serialization.JsonConverter1[[Calventory.Web.Domain.Item, Calventory.Web.Domain, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null]].WriteCore(Utf8JsonWriter , Item& , JsonSerializerOptions , WriteStack& ) at System.Text.Json.JsonSerializer.<WriteStreamAsync>d__1211[[System.Object, System.Private.CoreLib, Version=7.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e]].MoveNext()
at System.Object.CustomSerialNoGetter(Object )
at System.Text.Json.Serialization.JsonConverter1[[Calventory.Web.Domain.Item, Calventory.Web.Domain, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null]].WriteCore(Utf8JsonWriter , Item& , JsonSerializerOptions , WriteStack& ) at System.Net.Http.Json.JsonContent.SerializeToStreamAsyncCore(Stream , Boolean , CancellationToken ) at System.Object.CustomSerialNoGetter(Object ) at System.Text.Json.Serialization.JsonConverter1[[Calventory.Web.Domain.Item, Calventory.Web.Domain, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null]].WriteCore(Utf8JsonWriter , Item& , JsonSerializerOptions , WriteStack& )
at System.Net.Http.HttpContent.LoadIntoBufferAsyncCore(Task , MemoryStream )
at System.Object.CustomSerialNoGetter(Object )
at System.Text.Json.Serialization.JsonConverter1[[Calventory.Web.Domain.Item, Calventory.Web.Domain, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null]].WriteCore(Utf8JsonWriter , Item& , JsonSerializerOptions , WriteStack& ) at System.Net.Http.HttpContent.<WaitAndReturnAsync>d__822[[System.Net.Http.HttpContent, System.Net.Http, Version=7.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a],[System.Byte[], System.Private.CoreLib, Version=7.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e]].MoveNext()
at System.Object.CustomSerialNoGetter(Object )
at System.Text.Json.Serialization.JsonConverter1[[Calventory.Web.Domain.Item, Calventory.Web.Domain, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null]].WriteCore(Utf8JsonWriter , Item& , JsonSerializerOptions , WriteStack& ) at System.Net.Http.BrowserHttpHandler.CallFetch(HttpRequestMessage , CancellationToken , Nullable1 )
at System.Object.CustomSerialNoGetter(Object )
at System.Text.Json.Serialization.JsonConverter1[[Calventory.Web.Domain.Item, Calventory.Web.Domain, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null]].WriteCore(Utf8JsonWriter , Item& , JsonSerializerOptions , WriteStack& ) at System.Net.Http.BrowserHttpHandler.<SendAsync>g__Impl|55_0(HttpRequestMessage , CancellationToken , Nullable1 )
at System.Object.CustomSerialNoGetter(Object )
at System.Text.Json.Serialization.JsonConverter1[[Calventory.Web.Domain.Item, Calventory.Web.Domain, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null]].WriteCore(Utf8JsonWriter , Item& , JsonSerializerOptions , WriteStack& ) at System.Net.Http.HttpClient.<SendAsync>g__Core|83_0(HttpRequestMessage , HttpCompletionOption , CancellationTokenSource , Boolean , CancellationTokenSource , CancellationToken ) at System.Object.CustomSerialNoGetter(Object ) at System.Text.Json.Serialization.JsonConverter1[[Calventory.Web.Domain.Item, Calventory.Web.Domain, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null]].WriteCore(Utf8JsonWriter , Item& , JsonSerializerOptions , WriteStack& )
at Calventory.Web.Client.BaseController.d__51[[System.Int32, System.Private.CoreLib, Version=7.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e]].MoveNext() at System.Object.CustomSerialNoGetter(Object ) at System.Text.Json.Serialization.JsonConverter1[[Calventory.Web.Domain.Item, Calventory.Web.Domain, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null]].WriteCore(Utf8JsonWriter , Item& , JsonSerializerOptions , WriteStack& )
at Calventory.Web.Client.ItemState.CreateOrUpdate()
at System.Object.CustomSerialNoGetter(Object )
at System.Text.Json.Serialization.JsonConverter1[[Calventory.Web.Domain.Item, Calventory.Web.Domain, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null]].WriteCore(Utf8JsonWriter , Item& , JsonSerializerOptions , WriteStack& ) at Microsoft.AspNetCore.Components.ComponentBase.CallStateHasChangedOnAsyncCompletion(Task task) at System.Object.CustomSerialNoGetter(Object ) at System.Text.Json.Serialization.JsonConverter1[[Calventory.Web.Domain.Item, Calventory.Web.Domain, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null]].WriteCore(Utf8JsonWriter , Item& , JsonSerializerOptions , WriteStack& )
at Microsoft.AspNetCore.Components.Forms.EditForm.HandleSubmitAsync()
at System.Object.CustomSerialNoGetter(Object )
at System.Text.Json.Serialization.JsonConverter1[[Calventory.Web.Domain.Item, Calventory.Web.Domain, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null]].WriteCore(Utf8JsonWriter , Item& , JsonSerializerOptions , WriteStack& ) at Microsoft.AspNetCore.Components.ComponentBase.CallStateHasChangedOnAsyncCompletion(Task task) at System.Object.CustomSerialNoGetter(Object ) at System.Text.Json.Serialization.JsonConverter1[[Calventory.Web.Domain.Item, Calventory.Web.Domain, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null]].WriteCore(Utf8JsonWriter , Item& , JsonSerializerOptions , WriteStack& )
at Microsoft.AspNetCore.Components.RenderTree.Renderer.GetErrorHandledTask(Task , ComponentState )

Stacktrace 2:
Microsoft.AspNetCore.Components.WebAssembly.Rendering.WebAssemblyRenderer[100]
Unhandled exception rendering component: Arg_InvalidCastException
System.InvalidCastException: Arg_InvalidCastException
at System.Object.GuidGetter(Object )
at System.Text.Json.Serialization.JsonConverter1[[Calventory.Web.Domain.Account, Calventory.Web.Domain, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null]].WriteCore(Utf8JsonWriter , Account& , JsonSerializerOptions , WriteStack& ) at System.Text.Json.JsonSerializer.<WriteStreamAsync>d__1211[[System.Object, System.Private.CoreLib, Version=7.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e]].MoveNext()
at System.Object.GuidGetter(Object )
at System.Text.Json.Serialization.JsonConverter1[[Calventory.Web.Domain.Account, Calventory.Web.Domain, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null]].WriteCore(Utf8JsonWriter , Account& , JsonSerializerOptions , WriteStack& ) at System.Text.Json.JsonSerializer.<WriteStreamAsync>d__1211[[System.Object, System.Private.CoreLib, Version=7.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e]].MoveNext()
at System.Object.GuidGetter(Object )
at System.Text.Json.Serialization.JsonConverter1[[Calventory.Web.Domain.Account, Calventory.Web.Domain, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null]].WriteCore(Utf8JsonWriter , Account& , JsonSerializerOptions , WriteStack& ) at System.Text.Json.JsonSerializer.<WriteStreamAsync>d__1211[[System.Object, System.Private.CoreLib, Version=7.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e]].MoveNext()
at System.Object.GuidGetter(Object )
at System.Text.Json.Serialization.JsonConverter1[[Calventory.Web.Domain.Account, Calventory.Web.Domain, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null]].WriteCore(Utf8JsonWriter , Account& , JsonSerializerOptions , WriteStack& ) at System.Net.Http.Json.JsonContent.SerializeToStreamAsyncCore(Stream , Boolean , CancellationToken ) at System.Object.GuidGetter(Object ) at System.Text.Json.Serialization.JsonConverter1[[Calventory.Web.Domain.Account, Calventory.Web.Domain, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null]].WriteCore(Utf8JsonWriter , Account& , JsonSerializerOptions , WriteStack& )
at System.Net.Http.HttpContent.LoadIntoBufferAsyncCore(Task , MemoryStream )
at System.Object.GuidGetter(Object )
at System.Text.Json.Serialization.JsonConverter1[[Calventory.Web.Domain.Account, Calventory.Web.Domain, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null]].WriteCore(Utf8JsonWriter , Account& , JsonSerializerOptions , WriteStack& ) at System.Net.Http.HttpContent.<WaitAndReturnAsync>d__822[[System.Net.Http.HttpContent, System.Net.Http, Version=7.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a],[System.Byte[], System.Private.CoreLib, Version=7.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e]].MoveNext()
at System.Object.GuidGetter(Object )
at System.Text.Json.Serialization.JsonConverter1[[Calventory.Web.Domain.Account, Calventory.Web.Domain, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null]].WriteCore(Utf8JsonWriter , Account& , JsonSerializerOptions , WriteStack& ) at System.Net.Http.BrowserHttpHandler.CallFetch(HttpRequestMessage , CancellationToken , Nullable1 )
at System.Object.GuidGetter(Object )
at System.Text.Json.Serialization.JsonConverter1[[Calventory.Web.Domain.Account, Calventory.Web.Domain, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null]].WriteCore(Utf8JsonWriter , Account& , JsonSerializerOptions , WriteStack& ) at System.Net.Http.BrowserHttpHandler.<SendAsync>g__Impl|55_0(HttpRequestMessage , CancellationToken , Nullable1 )
at System.Object.GuidGetter(Object )
at System.Text.Json.Serialization.JsonConverter1[[Calventory.Web.Domain.Account, Calventory.Web.Domain, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null]].WriteCore(Utf8JsonWriter , Account& , JsonSerializerOptions , WriteStack& ) at System.Net.Http.HttpClient.<SendAsync>g__Core|83_0(HttpRequestMessage , HttpCompletionOption , CancellationTokenSource , Boolean , CancellationTokenSource , CancellationToken ) at System.Object.GuidGetter(Object ) at System.Text.Json.Serialization.JsonConverter1[[Calventory.Web.Domain.Account, Calventory.Web.Domain, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null]].WriteCore(Utf8JsonWriter , Account& , JsonSerializerOptions , WriteStack& )
at Calventory.Web.Client.BaseController.d__51[[System.Int32, System.Private.CoreLib, Version=7.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e]].MoveNext() at System.Object.GuidGetter(Object ) at System.Text.Json.Serialization.JsonConverter1[[Calventory.Web.Domain.Account, Calventory.Web.Domain, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null]].WriteCore(Utf8JsonWriter , Account& , JsonSerializerOptions , WriteStack& )
at Calventory.Web.Client.AccountDialog.CreateOrUpdate()
at System.Object.GuidGetter(Object )
at System.Text.Json.Serialization.JsonConverter1[[Calventory.Web.Domain.Account, Calventory.Web.Domain, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null]].WriteCore(Utf8JsonWriter , Account& , JsonSerializerOptions , WriteStack& ) at Microsoft.AspNetCore.Components.ComponentBase.CallStateHasChangedOnAsyncCompletion(Task task) at System.Object.GuidGetter(Object ) at System.Text.Json.Serialization.JsonConverter1[[Calventory.Web.Domain.Account, Calventory.Web.Domain, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null]].WriteCore(Utf8JsonWriter , Account& , JsonSerializerOptions , WriteStack& )
at MudBlazor.MudBaseButton.OnClickHandler(MouseEventArgs ev)
at System.Object.GuidGetter(Object )
at System.Text.Json.Serialization.JsonConverter1[[Calventory.Web.Domain.Account, Calventory.Web.Domain, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null]].WriteCore(Utf8JsonWriter , Account& , JsonSerializerOptions , WriteStack& ) at Microsoft.AspNetCore.Components.ComponentBase.CallStateHasChangedOnAsyncCompletion(Task task) at System.Object.GuidGetter(Object ) at System.Text.Json.Serialization.JsonConverter1[[Calventory.Web.Domain.Account, Calventory.Web.Domain, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null]].WriteCore(Utf8JsonWriter , Account& , JsonSerializerOptions , WriteStack& )
at Microsoft.AspNetCore.Components.RenderTree.Renderer.GetErrorHandledTask(Task , ComponentState )

Regression?

It works in .net 6. It also works in .net 7 most of the time.

Known Workarounds

Reloading the page solves the problem.

Configuration

.Net Version : .net 7,
Browser : Chrome, Edge

Other information

No response

Author: UserIsAmitHasan
Assignees: -
Labels:

area-System.Text.Json, untriaged

Milestone: -

@eiriktsarpalis
Copy link
Member

We would need a minimal repro in order to take a better look at this, unfortunately.

@eiriktsarpalis eiriktsarpalis added the needs-author-action An issue or pull request that requires more info or actions from the author. label Dec 2, 2022
@ghost
Copy link

ghost commented Dec 2, 2022

This issue has been marked needs-author-action and may be missing some important information.

@ghost ghost removed the untriaged New issue has not been triaged by the area owner label Dec 2, 2022
@wafn007
Copy link

wafn007 commented Dec 8, 2022

Same thing here, I have struggled with this for a week now 😕It happens randomly but only if I publish a release build with these parameters: --configuration "Release" --runtime win-x64 --self-contained
It started about the same time as I upgraded to latest .NET. I have tried a million different JsonSerializerOptions without result.

Code:
` T t = JsonSerializer.Deserialize(json,
new JsonSerializerOptions()
{
PropertyNameCaseInsensitive = true,
DefaultIgnoreCondition = System.Text.Json.Serialization.JsonIgnoreCondition.WhenWritingNull,
IgnoreReadOnlyProperties = true,
});

catch (InvalidCastException e)
{
Console.WriteLine($"{url} {e.Message} {json}");
}
`

output in browser:
(my url) Arg_InvalidCastException (valid json checked with several different validators)

@eiriktsarpalis
Copy link
Member

eiriktsarpalis commented Dec 9, 2022

@UserIsAmitHasan or @wafn007, would it be possible to share a standalone project that demonstrates the issue? A zip file or a repo containing a Blazor project, including the inputs that are triggering the failure.

See also https://github.com/dotnet/runtime/blob/main/CONTRIBUTING.md#how-to-create-a-minimal-reproduction

@wafn007
Copy link

wafn007 commented Dec 10, 2022

I think I have a fix for my issue or at least a work around. The problem is the construction of the JsonSerializerOptions which seems to be a bit strange. Using a static JsonSerializerOptions everything works as expected, no errors yet at least. I found below link suggesting using a single instance of the options object between calls for performance reasons. Maybe something strange is happening inside the framework classes since they cannot cope with a slow options construction??
https://learn.microsoft.com/en-us/dotnet/standard/serialization/system-text-json/configure-options?pivots=dotnet-7-0

    private static readonly JsonSerializerOptions deserializerOptions = new()
    {
        PropertyNameCaseInsensitive = true,
        DefaultIgnoreCondition = System.Text.Json.Serialization.JsonIgnoreCondition.WhenWritingNull,
        IgnoreReadOnlyProperties = true,
    };

    T t = JsonSerializer.Deserialize<T>(json, deserializerOptions);

@eiriktsarpalis
Copy link
Member

We would need a bit more information, including the type of T and the json payload used when deserializing.

@UserIsAmitHasan
Copy link
Author

UserIsAmitHasan commented Dec 12, 2022

@eiriktsarpalis T is a class and the problem happens at property getters of type string.

Here : CustomSerialNo is a property of type string
System.InvalidCastException: Arg_InvalidCastException
at System.Object.CustomSerialNoGetter(Object )

Here : Guid is a property of type string
System.InvalidCastException: Arg_InvalidCastException
at System.Object.GuidGetter(Object )

I had to go back to .net 6 to make the app reliable.

@ghost ghost added needs-further-triage Issue has been initially triaged, but needs deeper consideration or reconsideration and removed needs-author-action An issue or pull request that requires more info or actions from the author. labels Dec 12, 2022
@eiriktsarpalis
Copy link
Member

In order for us to take a closer look, please share a minimal, self-contained project that demonstrates the issue.

@eiriktsarpalis eiriktsarpalis added needs-author-action An issue or pull request that requires more info or actions from the author. and removed needs-further-triage Issue has been initially triaged, but needs deeper consideration or reconsideration labels Dec 12, 2022
@ghost
Copy link

ghost commented Dec 12, 2022

This issue has been marked needs-author-action and may be missing some important information.

@UserIsAmitHasan
Copy link
Author

@eiriktsarpalis thanks for your quick reply. Issue is tricky and occurs randomly. Let me try to produce a minimum repo. I will get back to you when done; may take a couple of days due to the nature of the problem. Thanks again.

@ghost ghost added needs-further-triage Issue has been initially triaged, but needs deeper consideration or reconsideration and removed needs-author-action An issue or pull request that requires more info or actions from the author. labels Dec 12, 2022
@eiriktsarpalis eiriktsarpalis added needs-author-action An issue or pull request that requires more info or actions from the author. and removed needs-further-triage Issue has been initially triaged, but needs deeper consideration or reconsideration labels Dec 12, 2022
@ghost
Copy link

ghost commented Dec 12, 2022

This issue has been marked needs-author-action and may be missing some important information.

@ghost ghost added the no-recent-activity label Dec 26, 2022
@ghost
Copy link

ghost commented Dec 26, 2022

This issue has been automatically marked no-recent-activity because it has not had any activity for 14 days. It will be closed if no further activity occurs within 14 more days. Any new comment (by anyone, not necessarily the author) will remove no-recent-activity.

@ghost
Copy link

ghost commented Jan 9, 2023

This issue will now be closed since it had been marked no-recent-activity but received no further activity in the past 14 days. It is still possible to reopen or comment on the issue, but please note that the issue will be locked if it remains inactive for another 30 days.

@ghost ghost closed this as completed Jan 9, 2023
@dotnet dotnet locked as resolved and limited conversation to collaborators Feb 8, 2023
This issue was closed.
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
area-System.Text.Json needs-author-action An issue or pull request that requires more info or actions from the author. no-recent-activity
Projects
None yet
Development

No branches or pull requests

3 participants