Hi,
I'm trying to use Razor outside of MVC (actually I did), there was no problem with prev version, but after updating to .Net Core 3.0, I got following error and I have no idea how can I fix it:
Unable to resolve service for type 'System.Diagnostics.DiagnosticListener' while attempting to activate 'Microsoft.AspNetCore.Mvc.Razor.RazorViewEngine'
var environment = new WebHostEnvironment
{
WebRootFileProvider = new PhysicalFileProvider(rootPath),
ApplicationName = typeof(RazorRenderer).Assembly.GetName().Name,
ContentRootPath = rootPath,
WebRootPath = rootPath,
EnvironmentName = "DEVELOPMENT",
ContentRootFileProvider = new PhysicalFileProvider(rootPath)
};
services.AddSingleton<IWebHostEnvironment>(environment);
services.Configure<MvcRazorRuntimeCompilationOptions>(options =>
{
options.FileProviders.Clear();
options.FileProviders.Add(new PhysicalFileProvider(rootPath));
});
services.AddSingleton<ObjectPoolProvider, DefaultObjectPoolProvider>();
services.AddSingleton<ILoggerFactory>(this);
var diagnosticSource = new DiagnosticListener(environment.ApplicationName);
services.AddSingleton<DiagnosticSource>(diagnosticSource);
services.AddMvc();
The following answer @ SO is almost same as my code:
https://stackoverflow.com/a/54960162/3367974
Add this:
services.AddSingleton<DiagnosticListener>(diagnosticSource);
I think I found the solution
@davidfowl Would you explain why Razor can find views when I use this: services.AddControllersWithViews().AddRazorRuntimeCompilation(); but can not when I use this: services.AddControllersWithViews()
There's some information available here: https://docs.microsoft.com/en-us/aspnet/core/mvc/views/view-compilation?view=aspnetcore-2.2#runtime-compilation
If you want to compile views while the app is running, this requires AddRazorRuntimeCompilation. I've filed https://github.com/aspnet/AspNetCore.Docs/issues/14686 to track updating the docs to explain this more clearly.
@rynowak thanks for the link. I didn't find my answer there though, why Razor only can find views when I use AddRazorRuntimeCompilation();
Update:
Seems like there is no AllowRecompilingViewsOnFileChange property available at RazorViewEngineOptions in .net core 3.0, so the link is outdated, right?
I tried to answer that here:
If you want to compile views while the app is running, this requires AddRazorRuntimeCompilation.
I don't know your exact setup, so it's a guess...Do you expect your views to compile as part of the build? or when the app is running?
In a perfect world, I want both. in the real world the answer would be _when the app is running_.
For now I added AddRazorRuntimeCompilation, so when my app is running and I change the view, there is no need to rebuild and refreshing the browser is enough, and this is what I want.
But still I could not see my answer. atm Razor can find my views and everything works fine. but if I remove AddRazorRuntimeCompilation, my app stop working and error says Razor can not find the view. my question is why this happens? I mean I didn't change anything, just remove AddRazorRuntimeCompilation, why Razor can not find view in this case?
And I have another question, with AddRazorRuntimeCompilation in use, why the first run took ~3 seconds, but after that rebuild (on view chagne) took almost 0. if Razor rebuild the view, when it changes, why this rebuild is so quick, but first run/build is so slow? (I hope I could say my question clear)
if I remove AddRazorRuntimeCompilation, my app stop working and error says Razor can not find the view. my question is why this happens?
Can you provide a repro or something we can investigate? My guess is that you have some kind of custom setup that relies on runtime compilation.
And I have another question, with AddRazorRuntimeCompilation in use, why the first run took ~3 seconds, but after that rebuild (on view chagne) took almost 0. if Razor rebuild the view, when it changes, why this rebuild is so quick, but first run/build is so slow? (I hope I could say my question clear)
The 3 seconds are spend loading/jitting the compiler. Once the compiler is already warm (from compiling the first view) it's very fast.
Here you go: https://github.com/dehghani-mehdi/razor-test
I think the problem is related to providing custom file provider, but I could not find any way to tell razor about it when AddRazorRuntimeCompilation is not used.
_p.s: Any point that makes the code of razor-test better will be helpful, thanks_
Most helpful comment
There's some information available here: https://docs.microsoft.com/en-us/aspnet/core/mvc/views/view-compilation?view=aspnetcore-2.2#runtime-compilation
If you want to compile views while the app is running, this requires
AddRazorRuntimeCompilation. I've filed https://github.com/aspnet/AspNetCore.Docs/issues/14686 to track updating the docs to explain this more clearly.