Skip to content
This repository has been archived by the owner on Dec 14, 2018. It is now read-only.

No equivalent of AddControllersAsServices() for VC's and TagHelpers #4087

Closed
dazinator opened this issue Feb 12, 2016 · 17 comments
Closed

No equivalent of AddControllersAsServices() for VC's and TagHelpers #4087

dazinator opened this issue Feb 12, 2016 · 17 comments
Assignees
Milestone

Comments

@dazinator
Copy link

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.

@dazinator
Copy link
Author

@danroth27
Copy link
Member

We should also look at adding a more general mechanism for injecting everything from an assembly: #4089.

@danroth27
Copy link
Member

We should also support constructor injection in view components by default like we do for controllers.

@danroth27 danroth27 changed the title No equivalent of AddControllersAsServices() for VC's No equivalent of AddControllersAsServices() for VC's and TagHelpers Feb 12, 2016
@danroth27
Copy link
Member

And we should do the same for Tag Helpers.

@dazinator
Copy link
Author

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..

@rynowak
Copy link
Member

rynowak commented Feb 16, 2016

@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?

@dazinator
Copy link
Author

@rynowak

What's stopping you from referencing this assembly at compile 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 VC's don't work without a project reference, and neither does Razor View compilation.

Can you enumerate all assemblies you want to "include" at startup time?

Yep. At startup time, my application registers an IAssemblyProvider that returns the union of the DefaultAssembyProvider plus my additional "plugin" assemblies. This doesn't seem to effect Razor Compilation or VC Discovery though.

@davidfowl
Copy link
Member

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.

@rynowak
Copy link
Member

rynowak commented Feb 16, 2016

after the application is deployed

Yep. At startup time ...

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?

This doesn't seem to effect Razor Compilation or VC Discovery though.

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

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.

We should still make this easier if it's just adding more "MVC" assemblies.

@dazinator
Copy link
Author

@rynowak

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

You are right! Implementing an IAssemblyProvider that includes my VC assembly has actually fixed this issue, I just wasn't testing correctly! :)

We should still make this easier if it's just adding more "MVC" assemblies.

Yes please, so it's as easy as AddControllersAsServices() !

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?

When the application starts up, the set of assemblies is fixed, but it consists of the assemblies that were project referenced (that ILibraryManager knows about) and the set that have been placed in some location through the concept of installing an extension into the application which happens at runtime, after the application has been deployed somewhere. Installing an extension basically dumps additional assemblies somewhere, and restarts the application, so that on startup the new assemblies are detected, and the MVC components can be correctly registered with the framework. It's this second part - correctly registering these assemblies with the MVC framework that has been challenging, as some things have API's like AddControllersAsServices and some things require that you implement IAssemblyProvider or some other hook :)

@dazinator
Copy link
Author

@davidfowl

In rc1 it's a bit cumbersome but possible.

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?

@rynowak
Copy link
Member

rynowak commented Mar 3, 2016

#4089 - iterating on design in this area

@rynowak
Copy link
Member

rynowak commented Mar 3, 2016

@dazinator - sorry, I missed your comment here.

@pranavkm - how can you add more assemblies as references for Razor compilation at startup time?

@pranavkm
Copy link
Contributor

pranavkm commented Mar 3, 2016

@dazinator, there's a callback on RazorViewEngineOptions that lets you configure the Roslyn compilation context:

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);
};

@rynowak rynowak assigned javiercn and unassigned rynowak Mar 4, 2016
@dazinator
Copy link
Author

@pranavkm - options.CompilationCallback doesn't appear to be a thing in 6.0.0-rc1-final. Is there an alternative for RC1 or would we have to upgrade to perhaps RC2? or Core?

@pranavkm
Copy link
Contributor

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?

javiercn added a commit that referenced this issue Mar 28, 2016
javiercn added a commit that referenced this issue Mar 29, 2016
javiercn added a commit that referenced this issue Mar 30, 2016
…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.
javiercn added a commit that referenced this issue Mar 31, 2016
…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.
javiercn added a commit that referenced this issue Mar 31, 2016
…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.
javiercn added a commit that referenced this issue Apr 1, 2016
…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.
@javiercn javiercn reopened this Apr 1, 2016
@javiercn
Copy link
Member

javiercn commented Apr 1, 2016

Reopened until I check-in the tag helpers part

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

6 participants