Description
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.