MudBlazor / Templates

Ready to use Blazor Templates in different styles and layout with all the basic setup already done for MudBlazor.
MIT License
745 stars 165 forks source link

Project created with net 8 template throws InvalidOperationException on first start #467

Closed petergolde closed 5 months ago

petergolde commented 9 months ago

Bug type

Other

Component name

No response

What happened?

I created a new MudBlazor application using the template:

dotnet new mudblazor --auth Individual --int Server --all-interactive -o MudBlazorTest

Then opened the application in Visual Studio 2022 Community and hit run.

--> Execution stops in RemoteJSRuntime.BeginInvokeJS with InvalidOperationException. See screenshot attached. Screenshot MudBlazor

Expected behavior

No exception thrown.

Reproduction link

https://github.com/petergolde/MudBlazorBugReport

Reproduction steps

  1. Create new application from command line using template and command line shown above.
  2. Start Visual Studio 2022 Community 17.8
  3. Open project created by dotnet new
  4. Click "Run" button in toolbar.

Relevant log output

Exception thrown: 'System.InvalidOperationException' in Microsoft.AspNetCore.Components.Server.dll
Exception thrown: 'System.InvalidOperationException' in Microsoft.JSInterop.dll
Exception thrown: 'System.InvalidOperationException' in System.Private.CoreLib.dll
Exception thrown: 'System.InvalidOperationException' in System.Private.CoreLib.dll
An exception of type 'System.InvalidOperationException' occurred in System.Private.CoreLib.dll but was not handled in user code
JavaScript interop calls cannot be issued at this time. This is because the component is being statically rendered. When prerendering is enabled, JavaScript interop calls can only be performed during the OnAfterRenderAsync lifecycle method.

Version (bug)

6.16.0

Version (working)

No response

What browsers are you seeing the problem on?

Microsoft Edge

On what operating system are you experiencing the issue?

Windows

Pull Request

Code of Conduct

ScarletKuro commented 9 months ago

Hi. Weird, but I cloned your project, started it and not errors, as well navigated all pages but still no errors. Visual Studio Enterprise v17.8.3 Would be also nice if you could show "call stack". In addition update the libraries just in case to the latest libs the -rc libraries have some bugs (https://github.com/MudBlazor/Templates/issues/453#issuecomment-1937365664)

petergolde commented 9 months ago

It must have something to do with my Debug options. If I unchecked that "Break when this exception type is unhandled", then it continues and runs ok. After unchecking that, I can't get it to reproduce again.

Looking at the call stack, it seems like this is a sort-of expected exception to happen with JS interop during pre-rendering. It seems to me it would be better to somehow detect the pre-rendering and not do the JS interop at all, but I suppose its OK the way it is.

It is not clear to me why my Visual Studio thought the exception was unhandled. Strange.

I guess it is fine to close this, but it was a bad first impression for me with MudBlazor.

ScarletKuro commented 9 months ago

I'm not sure if it comes from our code or not, but Microsoft doesn't have any public API to identify what rendering mode is used, nor there is API to understand if the JsRuntime is available. You can understand it in a razor component (after the OnAfterRenderAsync lifecycle, and in our code we call JS in component when its available), but not in a service if it's using JS. There are also JS errors when circuit breaks JSDisconnectedException, TaskCanceledException and all we can do is to suppress those errors.

erbatista commented 9 months ago

I had a similar error with WebAssembly interactivity setup, and it went away by explicitly defining the MudBlazor version, and also make sure you're targeting the latest version of the .Net Core. In short, my project file went from:

   <PackageReference Include="Microsoft.AspNetCore.Components.WebAssembly" Version="8.0.0-rc.2.23480.2" />
   <PackageReference Include="MudBlazor" Version="6.*" />

to:

  <PackageReference Include="Microsoft.AspNetCore.Components.WebAssembly" Version="8.0.2" />
  <PackageReference Include="MudBlazor" Version="6.17.0" />
jperson2000 commented 6 months ago

Hi everyone, I am working on upgrading the templates for MudBlazor 7.0.0 in this PR: https://github.com/MudBlazor/Templates/pull/465. I haven't seen this error but I will test this project type for you.