OnInitializedAsync() – No suitable method to override

I’ve been working on a new Blazor project for a few weeks, and have been bothered by the number of seemingly odd errors reported in the Visual Studio error panel. The main reason they bothered me (other than the unhelpful fact that they all claim to be on line 1 character 1 of the relevant file, which isn’t very helpful) is that the code runs fine. Whilst this has been annoying me, as things were working, I didn’t spend too much time on it.

However, I just ran into another problem, and the solution turned out to solve this one as well.

When I created the project for this site, I forgot to say I wanted to use authentication. This wasn’t a problem, as I added the Identity stuff in myself (mainly in Startup.cs). However, I now want to customise some of the Identity pages, so decided to scaffold the code. This failed with a compilation error on most of my pages, claiming that it couldn’t override OnInitializedAsync(), as there was no suitable method to override (I wish Microsoft would learn some grammar, you can’t have “no” somethings!). Now this was one of the common errors I’d seen in the error panel, that had annoyed me, but not held me back as the code worked. Now it was causing me major problems.

After a bit of searching, I found that my .razor.cs files needed to inherit from ComponentBase. Even though this isn’t actually necessary, as the naming convention (ie Jim.razor and Jim.razor.cs) allows the compiler to work it out, leaving the ComponentBase part out caused a compiler error when scaffolding.

So, all that was necessary was to add it in…

public partial class Jim : ComponentBase {

…and I was able to scaffold.

Simple in retrospect, but very annoying. It should either work or not. Compiling fine when I run, and then not compiling when scaffolding doesn’t make a lot of sense.

5 Comments

  1. Chris said:

    Hi.
    I had exactly this and now my markup do not seems to see the variables in my code behind.
    Kindly tell me how to wire them up.
    Regards,
    Chris

    July 26, 2021
    Reply
    • Avrohom Yisroel Silver said:

      Sadly, VS seems to do this every now and then. The code usually works, and restarting VS often fixes the errors, but it’s annoying.

      December 22, 2021
      Reply
  2. Francesco said:

    I’m having the same problem.
    Strangely enough, it happens only when I upgrade one library (MudBazor from 6.0.2 to 6.0.3 or higher).

    It’s weird, and it happens with both the `dotnet` cli tool and Rider.

    February 21, 2022
    Reply
    • Avrohom Yisroel Silver said:

      Interesting, so it sounds like it’s not a VS thing then?

      Another bit of MS weirdness to keep us on our toes!

      February 21, 2022
      Reply
  3. Jean Poulin said:

    It’s the type of error that can have multiple causes. In my case it was so weird that I thought what I had changed recently and it was an install of more recent .Net 7 SDK. I uninstalled it leaving the original .NET installed with VS 2022 and everything now compiles instead of 69 error of this kind.

    May 27, 2023
    Reply

Leave a Reply

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.