title: Blazor Server - 'Code Behind' pattern: OnInitializedAsync(): no suitable method found to override
body: <p>I have a Blazor (Server) application which runs perfectly fine, and which adheres to all rules set by <code>Microsoft.CodeAnalysis.FxCopAnalyzers</code> and <code>StyleCop.Analyzers</code>.</p>
  <p>A heavily cut-down razor page is as follows:</p>
  <pre><code>@inherits OwningComponentBase&lt;MyService&gt;
  @inject IModalService ModalService
  @inject IJSRuntime JSRuntime
  
  // UI code
  
  @code
  {
      private readonly CancellationTokenSource TokenSource = new CancellationTokenSource();
      ElementReference myElementReferenceName;
  
      protected override async Task OnAfterRenderAsync(bool firstRender)
      {
          await this.myElementReferenceName.FocusAsync(this.JSRuntime);
      }
  
      protected override async Task OnInitializedAsync()
      {
          ....
      }
  
      public void Dispose()
      {
          this.TokenSource.Cancel();
      }
  
      protected void ShowModalEdit(object someObject)
      {
          .....
          Modal.Show&lt;MyPage&gt;(&quot;Edit&quot;, parameters);
      }
  }
  </code></pre>
  <p>Note#1: I used <code>@inherits OwningComponentBase&lt;MyService&gt;</code> based on <a href="https://github.com/aspnet/AspNetCore/issues/10448" rel="noreferrer">Daniel Roth's suggestion</a></p>
  <p>Note#2: I am using the <a href="https://github.com/Blazored/Modal" rel="noreferrer">Chris Sainty's Modal component</a> component</p>
  <p>However, when I try to move all the code from the <code>@code {...}</code> section to a&quot;Code Behind&quot; partial class (&quot;MyPage.razor.cs&quot;), then I run into the following errors....</p>
  <blockquote>
  <p>'MyPage' does not contain a definition for 'Service' and no accessible
  extension method 'Service' accepting .....</p>
  <p>'MyPage.OnAfterRenderAsync(bool)': no suitable method found to override</p>
  <p>'MyPage.OnInitializedAsync()': no suitable method found to override</p>
  <p>The type 'MyPage' cannot be used as type parameter 'T' in the generic
  type or method 'IModalService.Show(string, ModalParameters,
  ModalOptions)'. There is no implicit reference conversion from
  'MyPage' to 'Microsoft.AspNetCore.Components.ComponentBase'.</p>
  </blockquote>
  <p>Suggestions?</p>