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.

Adding EntryAssembly to AddRazorPages/AddControllers*

See original GitHub issue

Background and Motivation

Currently, the default behavior in MVC to load the default ApplicationParts from the entryassembly is rely on the IWebHostEnvironment, however, as reported in the #10611, there are scenarios where the developer wants to just create a new ServiceCollection and configure as needed. This scenario is not supported today because some providers will not be loaded, eg. RazorCompiledItemProvider required in Razor Pages.

The proposal is to add a new overload to all Add* methods available to include Assembly entryAssembly parameter that will be used to load the default ApplicationParts from.

Related:

Proposed API

namespace Microsoft.Extensions.DependencyInjection
{
   public static class MvcServiceCollectionExtensions
   {
+      public static IMvcBuilder AddControllers(this IServiceCollection services, Assembly entryAssembly) 
+      {}

+      public static IMvcBuilder AddControllers(this IServiceCollection services, Assembly entryAssembly, Action<MvcOptions>? configure)
+      {}

+      public static IMvcBuilder AddControllersWithViews(this IServiceCollection services, Assembly entryAssembly) 
+      {}

+      public static IMvcBuilder AddControllersWithViews(this IServiceCollection services, Assembly entryAssembly, Action<MvcOptions>? configure)
+      {}

+      public static IMvcBuilder AddRazorPages(this IServiceCollection services, Assembly entryAssembly) 
+      {}

+      public static IMvcBuilder AddRazorPages(this IServiceCollection services, Assembly entryAssembly, Action<RazorPagesOptions>? configure)
+      {}

   }
}
namespace Microsoft.Extensions.DependencyInjection
{
   public static class MvcCoreServiceCollectionExtensions
   {
+      public static IMvcCoreBuilder AddMvcCore(this IServiceCollection services, Assembly entryAssembly)
+      {}
   }
}

Usage Examples

RazorPages

services.AddRazorPages(typeof(Startup).Assembly);

Controllers

services.AddControllers(typeof(Startup).Assembly);

Controllers with views

services.AddControllersWithViews(typeof(Startup).Assembly);

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:9 (8 by maintainers)

github_iconTop GitHub Comments

1reaction
brunolins16commented, Jan 18, 2022

@dazinator After some discussion we decided that this API is not totally correct, and we should not add it, right now. What I will add instead, for now, is just a log message informing that no actions were detected and could be caused by a misconfiguration. Also point to this documentation Share controllers, views, Razor Pages and more with Application Parts that explains the usage of AddApplicationPart and ConfigureApplicationPartManager

0reactions
dazinatorcommented, Jan 5, 2022

I really don’t think we should change the default/idiom here (including the template). Exposing that concept so early and for so many seems like a bad fit. For the vast majority of cases in an app, don’t we just want this to be the app assembly itself, which is the entry assembly already?

In my opinion, the way MVC detects the entry assembly is obscure and hard to find what is going on when errors similar to the reported in the issue happens, however, I think this behavior is there for so many years (i am not sure if the users complained or not before) that I agree with @DamianEdwards that is better no change the default and instead just add the new overload and we maybe we can include in the docs scenarios where they are a

Makes sense - just wanted to mention that if previous method remains, then it may be prudent to throw an exception when that method cannot locate the entry assembly due to missing prerequisite services, perhaps with the error indicating that you can call the new overload to provide the entry assembly as a param explicitly - that should catch cases like the issue linked?

Read more comments on GitHub >

github_iconTop Results From Across the Web

Share controllers, views, Razor Pages and more with ...
Using Application Parts, you can share an assembly (DLL) containing controllers, views, Razor Pages, razor compilation sources, Tag Helpers, and ...
Read more >
Adding asp.net controllers/views from a Class Library
var assembly = typeof(AnySharedController).Assembly; builder.Services.AddRazorPages() .AddJsonOptions(x => x.JsonSerializerOptions.
Read more >
AddController vs AddMvc vs AddControllersWithViews ...
In this article, I am going to discuss the AddController() vs AddMvc() vs AddControllersWithViews() vs AddRazorPages() methods.
Read more >
ASP.NET Core MVC 3.x – AddMvc(), AddMvcCore ...
In ASP.NET Core 3.x, aside from those two approaches, there are three additional ways: services.AddControllers(); services.
Read more >
Exploring .NET Core 3.0. What's New?
This command will create a single file package for your Windows 10 application. Alternatively, you can edit your project file by adding the...
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