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.

Be First to Comment

Leave a Reply

Your email address will not be published. Required fields are marked *

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