“No suitable method found to override” error in Blazor/Razor pages

I’ve run into an issue several times recently while working in Blazor where I’ll innocently try and run the application after some minor changes, only to be hit by a wall of errors seemingly out of nowhere:

ExamplePage.razor.cs(7, 29): [CS0115] 'ExamplePage.OnInitializedAsync()': no suitable method found to override
ExamplePage.razor.cs(12, 29): [CS0115] 'ExamplePage.OnAfterRenderAsync(bool)': no suitable method found to override
ExamplePage.razor.cs(17, 29): [CS0115] 'ExamplePage.OnParametersSetAsync()': no suitable method found to override
ExamplePage.razor.cs(22, 26): [CS0115] 'ExamplePage.SetParametersAsync(ParameterView)': no suitable method found to override

All of a sudden every single Razor component lifecycle function throughout the application is exploding and I’m left wondering how I managed to break things so spectacularly.

My approach to fixing this historically was:

  1. Take a look at my local changes in Git
  2. Try to tweak things to get it working again
  3. Fail
  4. Roll back everything and re-implement the change

It was frustrating and had wasted enough of my time over the past few months that I decided that I was going to figure it out once and for all, by gum.

Note: If you’re in a hurry the fix is quick and simple, so feel free to jump straight down to that section to skip my self-indulgent waffling.

The Problem

It was a pretty hard one to debug since it happened infrequently/randomly and only seemed to be resolvable by completely resetting back to a known good state. Thankfully the last time it went wrong I managed to make enough headway that I’ve since been able to reproduce the error in a completely new solution!

The long and short of it is that if you have a file reference in your .csproj that no longer exists, it’ll trigger something internally that causes the above error block to be spat out. As an example, if I drop the following into ExampleProject.csproj (assuming the referenced page doesn’t exist), it’ll kick right off:

<ItemGroup>
    <AdditionalFiles Include="Pages\MissingPage.razor" />
</ItemGroup>

(It seems that <AdditionalFiles> is used to give analyzers “access to information that is not available through normal compiler inputs” in case you were wondering)

I realised that the issue happened when I deleted a page that had existed. In particular, the following seem to be the steps involved:

  1. A page exists in a given project
  2. The .csproj refers to it using the <AdditionalFiles> tag
  3. The file is deleted
  4. The .csproj reference still exists
  5. 💣

I’ve reproduced this issue so far in Rider on both Mac and Windows but haven’t had a chance to see if it’s also a problem in Visual Studio.

The Fix

Despite the potentially thousands of errors this kicks out, the fix is surprisingly easy. Since the problem is caused by your .csproj having a reference to a file that no longer exists, clearing that reference should be enough to get it working. If you’re not sure which file is the culprit, scroll to the bottom of the error output; the last item should name the particular file that can’t be found:

Source file '<FilePath>\Pages\MissingPage.razor' could not be found.

I only discovered this after reproducing the issue in a new solution 🙃

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

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