Skip to content

DotNetObjectRef.Create raises an exception when called from lifecycle events in preview6 #11159

Closed
@kvantetore

Description

@kvantetore

In preview6, there seems to be a regression in creating DotnetObjectRefs, such that when object refs are created during application lifecycle events, an InvalidOperationException is thrown, complaining that "JSRuntime must be set up correctly and must be an instance of JSRuntimeBase to use DotNetObjectRef.".

This happens even if I make sure prerendering has completed and ComponentContext.IsConnected is true.

If I modify app.razor from the default template to this:

@inject IComponentContext ComponentContext
@inject IJSRuntime JSRuntime

<CascadingAuthenticationState>
    <Router AppAssembly="typeof(Startup).Assembly">
        <NotFoundContent>
            <p>Sorry, there's nothing at this address.</p>
        </NotFoundContent>
    </Router>
</CascadingAuthenticationState>

<button @onclick="@InvokeJs">Invoke JS</button>

@code { 
protected override async Task OnAfterRenderAsync()
{
    if (ComponentContext.IsConnected)
    {
        await JSRuntime.InvokeAsync<object>("interopTest.log", DotNetObjectRef.Create(this));
    }
}

protected async Task InvokeJs()
{
    await JSRuntime.InvokeAsync<object>("interopTest.log", DotNetObjectRef.Create(this));
}
}

An exception is thrown during the DotNetObjectRef.Create call in OnAfterRenderAsync. However, if I disable that theOnAfterRenderAsync lifecycle event, and instead click on the button, the DotNetObjectRef.Create call succeeds as expected.

I need to register callbacks into dotnet-land at application startup (mediaQueryListener and similar), this issue breaks my use case.

Metadata

Metadata

Assignees

Labels

✔️ Resolution: DuplicateResolved as a duplicate of another issuearea-blazorIncludes: Blazor, Razor ComponentsbugThis issue describes a behavior which is not expected - a bug.component ecosystemIndicates an issue which also has impact on 3rd party component ecosystem

Type

No type

Projects

No projects

Relationships

None yet

Development

No branches or pull requests

Issue actions