Skip to content

JsRuntime is null on dispose on JsObjectWrapperBase #258

@cpiber

Description

@cpiber

When I dispose my JsObjectWrapperBase-derived object in the DisposeAsync of a component, I get an exception that JsRuntime is null. The problem is from there:

public class JsRuntimeObjectRef : IAsyncDisposable
  {
    internal IJSRuntime JsRuntime { get; set; }

    [JsonPropertyName("__jsObjectRefId")]
    public int JsObjectRefId { get; set; }

    public async ValueTask DisposeAsync()
    {
      JsRuntimeObjectRef runtimeObjectRef = this;
	  // HERE: runtimeObjectRef.JsRuntime is null in the next line
      await JSRuntimeExtensions.InvokeVoidAsync(runtimeObjectRef.JsRuntime, "browserInterop.removeObjectRef", new object[1]
      {
        (object) runtimeObjectRef.JsObjectRefId
      }).ConfigureAwait(false);
      GC.SuppressFinalize((object) runtimeObjectRef);
    }
  }

Not sure what might be causing this, it's not null in the object itself, so I added this method to my object:

    public new async ValueTask DisposeAsync()
    {
        // NOTE: For some reason JsObjectRef's JsRuntime is null in DisposeAsync, so this is copied from there
        await JsRuntime.InvokeVoidAsync("browserInterop.removeObjectRef", new object[1]
        {
            JsObjectRef.JsObjectRefId
        });
        GC.SuppressFinalize(JsObjectRef);
        GC.SuppressFinalize(this);
    }

I'm using Blazor WASM 6.0.6 on .NET 6.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions