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

jsRuntime.InvokeAsync can't return null as Nullable<Guid> #30366

Open
Regenhardt opened this issue Feb 22, 2021 · 8 comments · May be fixed by #60850
Open

jsRuntime.InvokeAsync can't return null as Nullable<Guid> #30366

Regenhardt opened this issue Feb 22, 2021 · 8 comments · May be fixed by #60850
Labels
area-blazor Includes: Blazor, Razor Components bug This issue describes a behavior which is not expected - a bug. cost: S Will take up to 2 days to complete feature-blazor-jsinterop This issue is related to JSInterop in Blazor help wanted Up for grabs. We would accept a PR to help resolve this issue Pillar: Technical Debt
Milestone

Comments

@Regenhardt
Copy link
Contributor

Describe the bug

Looks very similar to !10565 :

I'm trying to read a Guid from local storage, so I go return jsRuntime.InvokeAsync<Guid?>("localStorage.getItem", key);.
When there is a guid, this works fine.
But if there isn't a guid, I get this exception:
System.InvalidCastException: Null object cannot be converted to a value type.

Am I supposed to box my guid or what?

To Reproduce

Make a Blazor WASM project.
Add jsRuntime.InvokeAsync<Guid?>("localStorage.getItem", key); somewhere. init of Index.razor should be fine (in my project it happens somewhere 3 levels down asynchronously).
Execute.

Exceptions (if any)

crit: Microsoft.AspNetCore.Components.WebAssembly.Rendering.WebAssemblyRenderer[100]
      Unhandled exception rendering component: An exception occurred executing JS interop: Null object cannot be converted to a value type.. See InnerException for more details.
Microsoft.JSInterop.JSException: An exception occurred executing JS interop: Null object cannot be converted to a value type.. See InnerException for more details.
 ---> System.InvalidCastException: Null object cannot be converted to a value type.
   at System.Convert.ChangeType(Object value, Type conversionType, IFormatProvider provider)
   at System.Convert.ChangeType(Object value, Type conversionType)
   at Microsoft.JSInterop.Infrastructure.TaskGenericsUtil.TcsResultSetter`1[[System.Nullable`1[[System.Guid, System.Private.CoreLib, Version=5.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e]], System.Private.CoreLib, Version=5.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e]].SetResult(Object tcs, Object result)
   at Microsoft.JSInterop.Infrastructure.TaskGenericsUtil.SetTaskCompletionSourceResult(Object taskCompletionSource, Object result)
   at Microsoft.JSInterop.JSRuntime.EndInvokeJS(Int64 taskId, Boolean succeeded, Utf8JsonReader& jsonReader)
   --- End of inner exception stack trace ---
   at Microsoft.JSInterop.JSRuntime.<InvokeAsync>d__15`1[[System.Nullable`1[[System.Guid, System.Private.CoreLib, Version=5.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e]], System.Private.CoreLib, Version=5.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e]].MoveNext()
   at TeamBuilder_Website.Services.LocalStorage.<Get>d__3`1[[System.Nullable`1[[System.Guid, System.Private.CoreLib, Version=5.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e]], System.Private.CoreLib, Version=5.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e]].MoveNext()
   at TeamBuilder_Website.Services.AppState.RetrieveStoredToken()
   at TeamBuilder_Website.Services.AppState.<.ctor>b__13_0()
   at TeamBuilder_Website.Services.AppState.WaitForInit()
   at TeamBuilder_Website.Services.AccountService.TryLoginFromStoredToken()
   at TeamBuilder_Website.Services.AccountService.IsAuthorized(Guid loginToken)
   at TeamBuilder_Website.Pages.Index.OnInitializedAsync()
   at Microsoft.AspNetCore.Components.ComponentBase.RunInitAndSetParametersAsync()
   at Microsoft.AspNetCore.Components.RenderTree.Renderer.GetErrorHandledTask(Task taskToHandle)

Further technical details

  • SDK: Microsoft.NET.Sdk.BlazorWebAssembly
  • Target framework: net5.0
  • Include the output of dotnet --info
.NET SDK (reflecting any global.json):
 Version:   5.0.102
 Commit:    71365b4d42

Runtime Environment:
 OS Name:     Windows
 OS Version:  10.0.19042
 OS Platform: Windows
 RID:         win10-x64
 Base Path:   C:\Program Files\dotnet\sdk\5.0.102\

Host (useful for support):
  Version: 5.0.2
  Commit:  cb5f173b96

.NET SDKs installed:
  5.0.100 [C:\Program Files\dotnet\sdk]
  5.0.102 [C:\Program Files\dotnet\sdk]

.NET runtimes installed:
  Microsoft.AspNetCore.All 2.1.24 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.All]
  Microsoft.AspNetCore.App 2.1.24 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
  Microsoft.AspNetCore.App 3.1.11 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
  Microsoft.AspNetCore.App 5.0.0 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
  Microsoft.AspNetCore.App 5.0.2 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
  Microsoft.NETCore.App 2.1.24 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
  Microsoft.NETCore.App 3.1.11 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
  Microsoft.NETCore.App 5.0.0 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
  Microsoft.NETCore.App 5.0.2 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
  Microsoft.WindowsDesktop.App 3.1.11 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App]
  Microsoft.WindowsDesktop.App 5.0.0 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App]
  Microsoft.WindowsDesktop.App 5.0.2 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App]

To install additional .NET runtimes or SDKs:
  https://aka.ms/dotnet-download
  • The IDE (VS / VS Code/ VS4Mac) you're running on, and its version: VS2019 Version 16.8.4
@javiercn javiercn added the area-blazor Includes: Blazor, Razor Components label Feb 22, 2021
@mkArtakMSFT mkArtakMSFT added the bug This issue describes a behavior which is not expected - a bug. label Feb 22, 2021
@mkArtakMSFT mkArtakMSFT added this to the Next sprint planning milestone Feb 22, 2021
@ghost
Copy link

ghost commented Feb 22, 2021

Thanks for contacting us.
We're moving this issue to the Next sprint planning milestone for future evaluation / consideration. We will evaluate the request when we are planning the work for the next milestone. To learn more about what to expect next and how this issue will be handled you can read more about our triage process here.

@javiercn javiercn added the feature-blazor-jsinterop This issue is related to JSInterop in Blazor label Apr 20, 2021
@pranavkm pranavkm modified the milestones: Next sprint planning, Backlog Jul 9, 2021
@ghost
Copy link

ghost commented Jul 9, 2021

We've moved this issue to the Backlog milestone. This means that it is not going to be worked on for the coming release. We will reassess the backlog following the current release and consider this item at that time. To learn more about our issue management process and to have better expectation regarding different types of issues you can read our Triage Process.

@TanayParikh
Copy link
Contributor

This should be closely related to / encountered within #25677.

@TanayParikh TanayParikh added the cost: S Will take up to 2 days to complete label Jun 24, 2022
@Regenhardt
Copy link
Contributor Author

I think this issue could be solved by changing this line to use a safe cast instead of pattern matching, which seems to properly handle the conversion to a nullable value type:

image

Should I create a pull request to change it to

var typedResult = result as T ?? (T)Convert.ChangeType(result, typeof(T), CultureInfo.InvariantCulture)!;

?

@mkArtakMSFT
Copy link
Member

@Regenhardt yes, feel free to send us a PR, if you're still interested in this.

@mkArtakMSFT mkArtakMSFT added the help wanted Up for grabs. We would accept a PR to help resolve this issue label Dec 18, 2023
@ghost
Copy link

ghost commented Dec 18, 2023

Looks like this issue has been identified as a candidate for community contribution. If you're considering sending a PR for this issue, look for the Summary Comment link in the issue description. That comment has been left by an engineer on our team to help you get started with handling this issue. You can learn more about our Help Wanted process here

@mkArtakMSFT mkArtakMSFT modified the milestones: Planning: WebUI, Backlog Dec 18, 2023
@ghost
Copy link

ghost commented Dec 18, 2023

We've moved this issue to the Backlog milestone. This means that it is not going to be worked on for the coming release. We will reassess the backlog following the current release and consider this item at that time. To learn more about our issue management process and to have better expectation regarding different types of issues you can read our Triage Process.

Regenhardt added a commit to Regenhardt/aspnetcore that referenced this issue Dec 19, 2023
When a result value is null, and the return type is a nullable value type, Convert.ChangeType throws instead of returning the null value.

dotnet#30366
@Regenhardt
Copy link
Contributor Author

@mkArtakMSFT absolutely, I made a PR. I did realize on the way that my initial idea wouldn't work either because Convert.ChangeType would of course be called on null ??. I do wonder if this should be a change in Convert.ChangeType instead though.

Regenhardt added a commit to Regenhardt/aspnetcore that referenced this issue Dec 19, 2023
When a result value is null, and the return type is a nullable value type, Convert.ChangeType throws instead of returning the null value.

dotnet#30366
Regenhardt added a commit to Regenhardt/aspnetcore that referenced this issue Dec 19, 2023
When a result value is null, and the return type is a nullable value type, Convert.ChangeType throws instead of returning the null value.

dotnet#30366
Regenhardt added a commit to Regenhardt/aspnetcore that referenced this issue Dec 19, 2023
When a result value is null, and the return type is a nullable value type, Convert.ChangeType throws instead of returning the null value.

dotnet#30366
Regenhardt added a commit to Regenhardt/aspnetcore that referenced this issue Dec 29, 2023
When a result value is null, and the return type is a nullable value type, Convert.ChangeType throws instead of returning the null value.

dotnet#30366
Regenhardt added a commit to Regenhardt/aspnetcore that referenced this issue Mar 10, 2025
When a result value is null, and the return type is a nullable value type, Convert.ChangeType throws instead of returning the null value.

dotnet#30366
@Regenhardt Regenhardt linked a pull request Mar 10, 2025 that will close this issue
4 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area-blazor Includes: Blazor, Razor Components bug This issue describes a behavior which is not expected - a bug. cost: S Will take up to 2 days to complete feature-blazor-jsinterop This issue is related to JSInterop in Blazor help wanted Up for grabs. We would accept a PR to help resolve this issue Pillar: Technical Debt
Projects
None yet
7 participants
@pranavkm @javiercn @Regenhardt @TanayParikh @mkArtakMSFT @cerasamson and others