Skip to content

Conversation

Copilot
Copy link
Contributor

@Copilot Copilot AI commented Sep 25, 2025

Summary

Adds a new Roslyn analyzer rule BL0010 that detects when both @ref and @rendermode are used on the same Blazor component call site, preventing a confusing runtime InvalidCastException.

Problem

When developers use both @ref and @rendermode on the same component, they encounter an unhelpful runtime error:

// This code compiles but fails at runtime
<Counter @ref="counterRef" @rendermode="InteractiveServer" />

Runtime exception:

System.InvalidCastException: Unable to cast object of type 'Microsoft.AspNetCore.Components.Endpoints.SSRRenderModeBoundary' to type 'MyApp.Counter'.

This happens because when @rendermode is specified, ASP.NET Core wraps the component in an SSRRenderModeBoundary, so the @ref points to the boundary instead of the actual component.

Solution

The new ComponentRefAndRenderModeAnalyzer detects this pattern at compile time by analyzing BuildRenderTree methods for simultaneous calls to:

  • AddComponentReferenceCapture (generated by @ref)
  • AddComponentRenderMode (generated by @rendermode)

When both are used on the same component, it reports diagnostic BL0010 with clear guidance:

Error Message:

Component cannot use both @ref and @rendermode on the same element. The @ref will point to an SSRRenderModeBoundary instead of the component.

Description:

Using both @ref and @rendermode on the same component will cause the reference to point to an SSRRenderModeBoundary instead of the component itself. Remove @rendermode or use a separate component without @rendermode for the reference.

Implementation Details

  • Diagnostic ID: BL0010
  • Severity: Error (prevents compilation)
  • Scope: Only analyzes component BuildRenderTree methods for performance
  • Detection: Groups render tree calls by component blocks and checks for conflicting usage patterns

Testing

Added comprehensive test coverage with 5 test cases:

  • ✅ Component with both @ref and @rendermode → Reports diagnostic
  • ✅ Component with only @ref → No diagnostic
  • ✅ Component with only @rendermode → No diagnostic
  • ✅ Multiple components with mixed usage → Reports only problematic ones
  • ✅ Non-component methods → No analysis

All existing analyzer tests continue to pass, ensuring no regressions.

Fixes issue where using @ref and @rendermode together produces an unhelpful error message.

Original prompt

This section details on the original issue you should resolve

<issue_title>Using @ref and @rendermode on the same call site produces an unhelpful error message</issue_title>
<issue_description>### Is there an existing issue for this?

  • I have searched the existing issues

Describe the bug

Just done a simple test to get how the things are working

new template

[Parameter]
 public int MyProperty { get; set; }

 private void IncrementCount()
 {
     currentCount++;
     MyProperty = currentCount;

and

<BlazorApp8.Client.Pages.Counter @ref="Counter1" />
@Counter1?.MyProperty

@code{
    BlazorApp8.Client.Pages.Counter Counter1;
}

Expected Behavior

see the counter value

my intention was to use server side and embed a WASM part and how to communicate between the components

Steps To Reproduce

No response

Exceptions (if any)

System.InvalidCastException: Unable to cast object of type 'Microsoft.AspNetCore.Components.Endpoints.SSRRenderModeBoundary' to type 'BlazorApp8.Client.Pages.Counter'.

.NET Version

8.0.100-rc.1.23455.8

Anything else?

No response</issue_description>

<agent_instructions>We should add a new analyzer rule that inspects the usages of https://github.com/dotnet/aspnetcore/blob/063ba34bff5a3b6ee2f54cafe83b04bd3d4a5c06/src/Components/Components/src/Rendering/RenderTreeBuilder.cs#L630C17-L630C39 and detects when it is used alongside

public void AddComponentReferenceCapture(int sequence, Action<object> componentReferenceCaptureAction)
on the same component tree</agent_instructions>

Comments on the Issue (you are @copilot in this section)

@ Thanks for contacting us.

We're moving this issue to the .NET 9 Planning milestone for future evaluation / consideration. We would like to keep this around to collect more feedback, which can help us with prioritizing this work. We will re-evaluate this issue, during our next planning meeting(s).
If we later determine, that the issue has no community involvement, or it's very rare and low-impact issue, we will close it - so that the team can focus on more important and high impact issues.
To learn more about what to expect next and how this issue will be handled you can read more about our triage process here.</comment_new>
<comment_new>@
Thanks for contacting us.

We're moving this issue to the .NET 9 Planning milestone for future evaluation / consideration. We would like to keep this around to collect more feedback, which can help us with prioritizing this work. We will re-evaluate this issue, during our next planning meeting(s).
If we later determine, that the issue has no community involvement, or it's very rare and low-impact issue, we will close it - so that the team can focus on more important and high impact issues.
To learn more about what to expect next and how this issue will be handled you can read more about our triage process here.</comment_new>
<comment_new>@danroth27
@mkArtakMSFT Does this issue need to move to dotnet/razor?</comment_new>

Fixes #51151

💬 Share your feedback on Copilot coding agent for the chance to win a $200 gift card! Click here to start the survey.

Copy link
Contributor

Greetings human! You've submitted a PR that modifies code that is shared with https://github.com/dotnet/runtime . Please make sure you synchronize this code with the changes in that repo!

@Copilot Copilot AI changed the title [WIP] Using @ref and @rendermode on the same call site produces an unhelpful error message Add analyzer to detect @ref and @rendermode used together (BL0010) Sep 25, 2025
@Copilot Copilot AI requested a review from javiercn September 25, 2025 10:33
Copilot finished work on behalf of javiercn September 25, 2025 10:33
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Using @ref and @rendermode on the same call site produces an unhelpful error message
2 participants