Aspnetcore: Update Razor compiler to use global:: more liberally

Created on 3 Feb 2020  路  7Comments  路  Source: dotnet/aspnetcore

Issue: In a Razor Component any variables named "Microsoft" reproduce the error (variable type on the error message changes accordingly):

Error CS1061 'string' does not contain a definition for 'AspNetCore' and no accessible extension method 'AspNetCore' accepting a first argument of type 'string' could be found (are you missing a using directive or an assembly reference?)

Question: Is this an issue or by design?

Project details and files: This is a Razor Class Library project.

.csproj

<Project Sdk="Microsoft.NET.Sdk.Razor">

  <PropertyGroup>
    <TargetFramework>netstandard2.1</TargetFramework>
    <RazorLangVersion>3.0</RazorLangVersion>
    <LangVersion>8</LangVersion>
    <Nullable>enable</Nullable>
  </PropertyGroup>


  <ItemGroup>
    <PackageReference Include="Microsoft.AspNetCore.Components" Version="3.1.1" />
    <PackageReference Include="Microsoft.AspNetCore.Components.Web" Version="3.1.1" />
  </ItemGroup>

</Project>

LoginFormUserComponent.razor

@using SBL.Models.User.Login.OpenId

<div class="login">
    <div class="page-title">
        Login
    </div>

    <p>
        To login you must use one of the available Social Login options below:
    </p>

    <div class="social-login__container">
        <SocialButtonLoginComponent Provider="@Facebook" Image="/images/social/brands/facebook_37x37.png" />
        <SocialButtonLoginComponent Provider="@Google" Image="/images/social/brands/google_37x37.png" />
        @*<SocialButtonLoginComponent Provider="@Microsoft" Image="/images/social/brands/microsoft_37x37.png" />*@
        <SocialButtonLoginComponent Provider="@Twitter" Image="/images/social/brands/twitter_37x37.png" />
    </div>
</div>

@code {
    [Parameter]
    public ProviderOpenIdModel? Facebook { get; set; } = null;

    [Parameter]
    public ProviderOpenIdModel? Google { get; set; } = null;

    //[Parameter]
    //public ProviderOpenIdModel? Microsoft { get; set; } = null; // ERROR

    [Parameter]
    public ProviderOpenIdModel? Twitter { get; set; } = null;

    public string Microsoft { get; set; } // ERROR
}

Comments:
I started getting this random error and found out that apparently I cannot write "Microsoft" as a variable name in a Razor component.

I was unable to find the search keywords for anything similar to this.

I initially thought that there was a reference issue on my project when I realized the commented parameter was the issue. I then tried using a native variable type to see if the issue persisted - and it did.

It's not really an issue I need fixing per say - just wondering why this happens.

Thanks in advance!

affected-few area-blazor bug severity-major

All 7 comments

Thanks for the issue report @MichelArendt. This looks like a bug in Razor's code generation. Unfortunately there isn't a very good workaround for this outside of using a different member name.

A fix for this would be to globally qualify the Microsoft.AspNetCore.Components.CompilerServices.RuntimeHelpers.TypeCheck call.

protected override void BuildRenderTree(Microsoft.AspNetCore.Components.Rendering.RenderTreeBuilder __builder) 
        {
            __builder.AddMarkupContent(0, "<h1>Hello, world!</h1>\r\n\r\nWelcome to your new app.\r\n\r\n");
            __builder.OpenComponent<b.Pages.Counter>(1);
            __builder.AddAttribute(2, "Model", Microsoft.AspNetCore.Components.CompilerServices.RuntimeHelpers.TypeCheck<b.MyModel>( // here
                Microsoft
            ));
            __builder.CloseComponent();
        }

    public MyModel Microsoft { get; set; } // ERROR

@pranavkm I can make a doc issue of this and add Microsoft to Razor reserved keywords. Let me know if you'd like to go that route.

@Rick-Anderson this is not a reserved keyword though. Presumably once we fix the underlying issue, this should work.

We've moved this issue to the Backlog milestone. This means that it is not going to be worked on for the coming release. We will reassess the backlog following the current release and consider this item at that time. To learn more about our issue management process and to have better expectation regarding different types of issues you can read our Triage Process.

@pranavkm the problem with this issue is that there is no workaround other than changing the offending type declaration to object or dynamic. It's not possible to simply rename a type I might not own or refactor an entire application changing the namespace whenever there's a conflict.

Whilst it's a rare issue, when it occurs it is a showstopper. Any chance this can be pulled out of the backlog and shipped as part of .NET 5?

@diegofrata at this point we're close to wrapping up changes for 5.0. I'll have a look to see how trivial this change is, but I suspect it's fairly involved would make it more likely this would only be fixed in the next major release.

Was this page helpful?
0 / 5 - 0 ratings