-
Notifications
You must be signed in to change notification settings - Fork 2.1k
No equivalent of AddControllersAsServices() for VC's and TagHelpers #4087
Comments
|
We should also look at adding a more general mechanism for injecting everything from an assembly: #4089. |
We should also support constructor injection in view components by default like we do for controllers. |
And we should do the same for Tag Helpers. |
Not sure if this is related, have raised #4100 to do with ViewModels not being resolved when they also live in the external assembly alongiside the razor view. It seems whatever is responsible for compiling the razor view does not include the assembly that the view is in itself (embedded within assembly) when attempting to resolve Model's.. |
@dazinator - Razor uses your project's referenced assemblies as its inputs for compilation. We need to figure out a good all-up way to handle these scenarios. What's stopping you from referencing this assembly at compile time? Can you enumerate all assemblies you want to "include" at startup time? |
I'm building an application that is designed to be extensible, as in plugins / extensions can be installed at runtime, after the application is deployed. So far, the only blockers I have found are that
Yep. At startup time, my application registers an |
Then you need to hook more than just the assembly provider, in rc2 you can more easily add assemblies to the razor compilation. In rc1 it's a bit cumbersome but possible. |
These two aren't the same (at least not in my mind). Are you saying that you want to add assemblies after startup? Or is the set of assemblies fixed at startup time?
VC's use the assembly provider. If it's not finding your extra assemblies, this is something weird. https://github.com/aspnet/Mvc/blob/dev/src/Microsoft.AspNetCore.Mvc.ViewFeatures/ViewComponents/DefaultViewComponentDescriptorProvider.cs#L49
We should still make this easier if it's just adding more "MVC" assemblies. |
You are right! Implementing an
Yes please, so it's as easy as
When the application starts up, the set of assemblies is fixed, but it consists of the assemblies that were project referenced (that |
Are you able to point me in the right direction for how to do this with RC 1 as I'd love to get that working? |
#4089 - iterating on design in this area |
@dazinator - sorry, I missed your comment here. @pranavkm - how can you add more assemblies as references for Razor compilation at startup time? |
@dazinator, there's a callback on var previous = options.CompilationCallback;
options.CompilationCallback = (context) =>
{
if (previous != null)
{
previous(context);
}
var references = myAssemblies.Select(Assembly.Load).ToArray();
context.Compilation = context.Compilation.AddReferences(references);
}; |
@pranavkm - |
We added that as part of RC2.Looking at the code, there isn't a hook to configure compilation in RC1. Perhaps you could copy https://github.com/aspnet/Mvc/blob/6.0.0-rc1/src/Microsoft.AspNet.Mvc.Razor/Compilation/RoslynCompilationService.cs in to your application for the time being and remove it once you upgrade to RC2? |
…omponentActivator * Added ViewComponentFeture and ViewComponentFeatureProvider to perform view component discovery. * Changed view component discovery to use application parts. * Changed ViewComponentDescriptorProvider to make use of Application parts. * Added AddViewComponentsAsServices method on IMvcBuilder that performs view component discovery through the ApplicationPartManager and registers those view components as services in the service collection. Assemblies should be added to the ApplicationPartManager in order to discover view components in them in them.
…omponentActivator * Added ViewComponentFeture and ViewComponentFeatureProvider to perform view component discovery. * Changed view component discovery to use application parts. * Changed ViewComponentDescriptorProvider to make use of Application parts. * Added AddViewComponentsAsServices method on IMvcBuilder that performs view component discovery through the ApplicationPartManager and registers those view components as services in the service collection. Assemblies should be added to the ApplicationPartManager in order to discover view components in them in them.
…omponentActivator * Added ViewComponentFeture and ViewComponentFeatureProvider to perform view component discovery. * Changed view component discovery to use application parts. * Changed ViewComponentDescriptorProvider to make use of Application parts. * Added AddViewComponentsAsServices method on IMvcBuilder that performs view component discovery through the ApplicationPartManager and registers those view components as services in the service collection. Assemblies should be added to the ApplicationPartManager in order to discover view components in them in them.
…omponentActivator * Added ViewComponentFeture and ViewComponentFeatureProvider to perform view component discovery. * Changed view component discovery to use application parts. * Changed ViewComponentDescriptorProvider to make use of Application parts. * Added AddViewComponentsAsServices method on IMvcBuilder that performs view component discovery through the ApplicationPartManager and registers those view components as services in the service collection. Assemblies should be added to the ApplicationPartManager in order to discover view components in them in them.
Reopened until I check-in the tag helpers part |
If you have an external assembly, contianing a Controller, and a ViewComponent, you can register the controller with MVC by calling
AddControllersAsServices()
However there is no equivalent for the ViewComponent.
The default behaviour seems to be if you have a project reference to the assembly then the VC is picked up, but if you don't have a project reference to it (i.e
ILibraryManager
is not aware of it) - because for example, it's a standalone "plugin" assembly, then VC's are not detected.Could you add a method similar to
AddControllersAsServices()
but for VC's?Or perhaps consider adding some sort of "IncludeMvcServicesFromAssemblies()` method, that will make sure the assemblies are treated as candidates for all types of services that MVC looks for - i.e Controllers, VC's, and whatever else.
The text was updated successfully, but these errors were encountered: