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
Comments
Tagging subscribers to this area: @dotnet/area-system-text-json, @gregsdennis Issue DetailsDescriptionThis 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:
Reproduction StepsI have not been able to reproduce this problem in development environment. Stacktrace should provide some clue. Expected behaviorCode should work without InvalidCastException Actual behaviorHere are the stacktraces from browser console: Stacktrace 2: Regression?It works in .net 6. It also works in .net 7 most of the time. Known WorkaroundsReloading the page solves the problem. Configuration.Net Version : .net 7, Other informationNo response
|
We would need a minimal repro in order to take a better look at this, unfortunately. |
This issue has been marked |
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 Code: catch (InvalidCastException e) output in browser: |
@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 |
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??
|
We would need a bit more information, including the type of |
@eiriktsarpalis T is a class and the problem happens at property getters of type string. Here : CustomSerialNo is a property of type string Here : Guid is a property of type string I had to go back to .net 6 to make the app reliable. |
In order for us to take a closer look, please share a minimal, self-contained project that demonstrates the issue. |
This issue has been marked |
@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. |
This issue has been marked |
This issue has been automatically marked |
This issue will now be closed since it had been marked |
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:
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.JsonConverter
1[[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__121
1[[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.JsonConverter
1[[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__121
1[[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.JsonConverter
1[[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__121
1[[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.JsonConverter
1[[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.JsonConverter
1[[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.JsonConverter
1[[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__82
2[[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.JsonConverter
1[[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 , Nullable
1 )at System.Object.CustomSerialNoGetter(Object )
at System.Text.Json.Serialization.JsonConverter
1[[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 , Nullable
1 )at System.Object.CustomSerialNoGetter(Object )
at System.Text.Json.Serialization.JsonConverter
1[[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.JsonConverter
1[[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__5
1[[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.JsonConverter
1[[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.JsonConverter
1[[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.JsonConverter
1[[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.JsonConverter
1[[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.JsonConverter
1[[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.JsonConverter
1[[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__121
1[[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.JsonConverter
1[[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__121
1[[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.JsonConverter
1[[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__121
1[[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.JsonConverter
1[[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.JsonConverter
1[[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.JsonConverter
1[[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__82
2[[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.JsonConverter
1[[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 , Nullable
1 )at System.Object.GuidGetter(Object )
at System.Text.Json.Serialization.JsonConverter
1[[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 , Nullable
1 )at System.Object.GuidGetter(Object )
at System.Text.Json.Serialization.JsonConverter
1[[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.JsonConverter
1[[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__5
1[[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.JsonConverter
1[[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.JsonConverter
1[[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.JsonConverter
1[[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.JsonConverter
1[[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.JsonConverter
1[[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
The text was updated successfully, but these errors were encountered: