AddRazorPages() on new ServiceCollection > ApplicationPartManager missing parts
See original GitHub issueDescribe the bug
For background that lead to the discovery of this issue, see: https://github.com/aspnet/AspNetCore/issues/10469
services.AddRazorPages()
seems to have hidden dependencies - basically there are other services that need to be already present in the ServiceCollection
in order for ApplicationPartsManager
to establish parts needed for MapRazorPages()
to populate endpoint routes correctly. It’s not clear or explicit what these dependent services are though, which is why I am referring to them as “hidden”. All I can establish is that it must be one / some of the 60ish services that are already added for you to the IServiceCollection that gets passed into the Startup.ConfigureServices() method. The sample shows that if you add all 60ish of these services into a new IServiceCollection before then calling AddRazorPages() on that new IServiceCollection (and corresponding MapRazorPages) then ApplicationPartsManager correctly populates Razor parts which correctly drives endpoint routes.
If you don’t do this, then ApplicationPartsManager misses some parts and no endpoint routes are created for razor pages. Its imho a bug to allow AddRazorPages() to be added to a new service collection which due to missing services will result in no page routes ever being generated. At the least this could be detected and an exception thrown to indicate what the missing dependent services were. I’d prefer if it forced you to establish regiatration of any dependencies via the registration / builder API for adding razor pages but appreciate that could be tricky to get right.
Suppose your job is creating a multitenancy application where each tenant has it’s own IServiceCollection… you would think you could new up an IServiceCollection and add razor pages to it, but in reality you need to somehow figure out the hidden dependent services that are relied upon. It’s hard to tell precisely what services those are Adding all the 60ish default services for example does resolve the issue, but which ones of those does RazorPages actually need… who knows?
To Reproduce
Steps to reproduce the behavior:
- Run the sample here: https://github.com/dazinator/DemoRazorPagesMapped - and note, if you don’t clone the IServiceCollection passed in to configure, stuff won’t work.
Expected behavior
I expect to be able to Add Razor Pages to a new ServiceCollection and then use the RazorPages / endpoint middleware to serve razor pages. At the moment it only works if other unknown services are already in the ServiceCollection required for ApplicationPartManager be populate parts, and ultimately endpoint routes.
Screenshots
If applicable, add screenshots to help explain your problem.
Additional context
Add any other context about the problem here.
Include the output of dotnet --info
Issue Analytics
- State:
- Created 4 years ago
- Comments:8 (5 by maintainers)
@dazinator
The goal is to not fail silently anymore, and I am working to find the best approach here to throw an exception when the ApplicationParts are not configured correctly and should have an API Suggestion soon.
Subdomains or hostnames should work perfectly!
I am closing this issue since the API Suggestion (#39126) was approved and we will start work on it soon and we track from there. Thanks for submit the issue.