ltrzesniewski / RazorBlade

Compile Razor templates at build-time without a dependency on ASP.NET.
MIT License
149 stars 7 forks source link

Getting IDE error #14

Closed babula38 closed 6 months ago

babula38 commented 6 months ago

I am getting error when open the .cshtml file in both vscode and visual studio. could you please have a look at it or provide an work around.

Error: cannot change access modifiers when overriding 'protected internal' inherited member 'RazorTemplate.ExecuteAsync()

babula38 commented 6 months ago

I am using this application in .Net 7 application and version of library is RazorBlade" Version="0.6.0"

ltrzesniewski commented 6 months ago

I cannot reproduce this. I assume you're not using ReSharper? Are all of your tools up to date?

Can you please provide a simple repro project?

babula38 commented 6 months ago

Project Config:

image

vscode IDE Error:

image

Visual studio Version 17.8.6 IDE Error

image

ltrzesniewski commented 6 months ago

Well, I don't have this issue ¯\_(ツ)_/¯

image

Can you update your VS version? v1.9.6 is currently the latest.

Also, the class name doesn't seem right. RazorBlade would generate a PurchaseOrderDocumentView class, not one named Utility_Templates_PurchaseOrderDocumentView. Do you use ASP.NET in your project? If so, you should use ASP.NET directly to render your template. RazorBlade is not compatible with ASP.NET.

babula38 commented 6 months ago

@ltrzesniewski I am currently using Aspnet core webapi. It is part of Infrastructure layer.

So you suggest not using this library in it?

ltrzesniewski commented 6 months ago

I suppose you have AddRazorPages()/MapRazorPages() in your app? In that case I'd use ASP.NET's Razor instead of RazorBlade: you'll get more features and a single implementation. Otherwise, both libraries will try to process all the .cshtml files, and incompatibilities may arise.

The whole point of RazorBlade is to provide Razor templates when you're not using ASP.NET.

I should probably emit a warning when you try to use RazorBlade along with ASP.NET, because of the confusing behavior you get in that case.

babula38 commented 6 months ago

@ltrzesniewski, It's surprising that we're not utilizing AddRazorPages()/MapRazorPages(). This API is designed to serve only JSON. However, we have requirements for generating HTML documents as strings using Razor for certain document generation purposes.

Interestingly, the generated class name is different from what is shown in the IDE error.

image

ltrzesniewski commented 6 months ago

Hmm, that's what RazorBlade generates, but I suppose the IDE shows you errors which would happen if ASP.NET were the underlying engine. I'll try to reproduce the problem in an ASP.NET app in order to understand it better.

If you'd like to use RazorBlade, maybe you could try to isolate your Razor templates in a separate project which does not reference ASP.NET at all. Hopefully, the IDE won't get confused in that case 🤞. Since the generated templates are internal, you'll have to add a wrapper in order to call them, or maybe just an InternalsVisibleTo attribute if you just want to quickly test the IDE behavior.

ltrzesniewski commented 6 months ago

Ok, I understand the problem, and it's very simple. It occurs because of @inherits RazorBlade.HtmlTemplate:

RazorBlade expects a protected method to override, while ASP.NET expects a public one. If you run ASP.NET's source generator on a template which uses RazorBlade.HtmlTemplate, you'll get the "cannot change access modifiers" error that you see. That comes from Microsoft.CodeAnalysis.Razor.Compiler/Microsoft.NET.Sdk.Razor.SourceGenerators.RazorSourceGenerator, not from RazorBlade itself.

I don't want to make the ExecuteAsync method public in RazorBlade, since it's not suited to be called directly by the user. The RazorBlade library provides wrapper methods such as RenderAsync which set up the necessary context.

I also tested what happens when you move your RazorBlade template into a separate "Class Library" project, and sure enough, the error disappears since the Razor source generator is no longer invoked.

babula38 commented 6 months ago

Thanks @ltrzesniewski . I think below third party libraries are creating problem. the moment I moved the template to a different project the errors disappeared.

image

ltrzesniewski commented 6 months ago

Good! 🙂

I added a warning when you try to use RazorBlade in an ASP.NET project. That should make the issue less confusing in the next release.