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

[Activate] does not work on Controllers in beta4 #2476

Closed
Tragetaschen opened this issue Apr 30, 2015 · 5 comments
Closed

[Activate] does not work on Controllers in beta4 #2476

Tragetaschen opened this issue Apr 30, 2015 · 5 comments
Labels

Comments

@Tragetaschen
Copy link
Contributor

The following two lines work in a tag helper, but give a stack trace when in a controller:

[Activate]
private IHostingEnvironment hostingEnvironment{ get; set; }

That stacktrace is

InvalidOperationException: The property 'hostingEnvironment' on controller '*.Controllers.somecontroller' cannot be activated.

    Microsoft.AspNet.Mvc.DefaultControllerFactory.CreateActivateInfo(PropertyInfo property)
    System.Linq.Enumerable.WhereSelectArrayIterator`2.MoveNext()
    System.Linq.Buffer`1..ctor(IEnumerable`1 source)
    System.Linq.Enumerable.ToArray[TSource](IEnumerable`1 source)
    Microsoft.Framework.Internal.PropertyActivator`1.GetPropertiesToActivate(Type type, Type activateAttributeType, Func`2 createActivateInfo)
    Microsoft.AspNet.Mvc.DefaultControllerFactory.GetPropertiesToActivate(Type type)
    System.Collections.Concurrent.ConcurrentDictionary`2.GetOrAdd(TKey key, Func`2 valueFactory)
    Microsoft.AspNet.Mvc.DefaultControllerFactory.ActivateProperties(Object controller, ActionContext context)
    Microsoft.AspNet.Mvc.DefaultControllerFactory.CreateController(ActionContext actionContext)
    Microsoft.AspNet.Mvc.Core.ControllerActionInvoker.CreateInstance()
    […]
@rynowak
Copy link
Member

rynowak commented Apr 30, 2015

This is a design change - [Activate] is for context items like ActionContext. Use [FromServices] in a controller to access a service (or take it as a constructor parameter).

We have some changes planned for the current milestone to make this consistent across taghelpers/controllers

@Tragetaschen
Copy link
Contributor Author

Makes sense. It looks like view components are working like tag helpers right now, right?

It might be a different issue, but I'm trying to get an IApiDescriptionGroupCollectionProvider into my Controller. In my ViewComponent, I can still use [Activate] and it works like before. In the controller, though, the situation is:

  • Constructor parameter: works
  • [Activate]: Cannot activate exception with stack trace
  • [FromServices]: property remains null

@rynowak
Copy link
Member

rynowak commented May 1, 2015

[FromServices] on a controller property is expected to work in beta4 - is it a public get/set property?

@Tragetaschen
Copy link
Contributor Author

Ah, that's it. I still had this specified as a private property. That worked with [Activate] and still works ViewComponents and TagHelpers.

@danroth27
Copy link
Member

Note that we plan to clean up the naming of Inject vs Activate vs FromService. See #2151.

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

No branches or pull requests

3 participants