question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

AddRazorPages() on new ServiceCollection > ApplicationPartManager missing parts

See original GitHub issue

Describe 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:

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:closed
  • Created 4 years ago
  • Comments:8 (5 by maintainers)

github_iconTop GitHub Comments

1reaction
brunolins16commented, Dec 16, 2021

@dazinator

What happens if the entry assembly isn’t provided (i.e assuming it’s a new optional api and the developer isn’t aware of it’s existence) and the IWebHostEnvironment isn’t found, would you then throw an exception? Would just be keen to see we don’t see the same problem of silent failure in that scenario.

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.

  • i.e so the Razor Pages middleware sees an incoming request for /Home rather than /T1/Home. If that is the case then I don’t see any issues with that! Typically I wouldn’t actually need to do anything as I tend not to use reqeust path as a mechanism to do this mapping, I would typically actually do this mapping via subdomain or hostname - e.g http://t1.localhost.com/*--> t1 container, http://t2.localhost.com/* --> t2 container.

Subdomains or hostnames should work perfectly!

0reactions
brunolins16commented, Jan 4, 2022

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.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Share controllers, views, Razor Pages and more with ...
Application Parts allow ASP.NET Core to discover controllers, view components, tag helpers, Razor Pages, razor compilation sources, and more.
Read more >
I cannot get my Razor Pages to display in my ASP.NET 5 ...
I have a new web application written in the newest .NET Core framework, .NET 5.0. I have a pg folder with two Razor...
Read more >
When ASP.NET Core can't find your controller: debugging ...
In this post I describe application parts, how ASP.NET Core uses them to find the controllers in your app ... Debugging a missing...
Read more >
Untitled
AddRazorRuntimeCompilation () to the 4 line. builder. ... 2019 · AddRazorPages () on new ServiceCollection > ApplicationPartManager missing parts · Issue ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found