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

Property with ActivateAttribute cannot be activated #2578

Closed
evil-shrike opened this issue May 19, 2015 · 1 comment
Closed

Property with ActivateAttribute cannot be activated #2578

evil-shrike opened this issue May 19, 2015 · 1 comment

Comments

@evil-shrike
Copy link

I created an empty aspnet5-mvc web application. It's a default app based on "WebApi" template with beta4 packages (dnx-beta4, "Microsoft.AspNet.Mvc": "6.0.0-beta4", "Microsoft.AspNet.Server.IIS": "1.0.0-beta4", "Microsoft.AspNet.Server.WebListener": "1.0.0-beta4").

Then I registered a service

    public interface IQueryService
    {
        Uri GetQueryString(HttpContext ctx);
    }
    public class QueryService : IQueryService
    {
        public Uri GetQueryString(HttpContext ctx)
        {
            return new Uri(ctx.Request.QueryString.ToUriComponent());
        }
    }

    public class Startup
    {
        public void ConfigureServices(IServiceCollection services)
        {
            services.AddTransient<IQueryService, QueryService>();
            services.AddMvc();
        }
        public void Configure(IApplicationBuilder app, IHostingEnvironment env)
        {
            app.UseMvc();
        }
    }

And added this service as dependency into a Controller:

    public class ValuesController : Controller
    {
        [Activate]
        public IQueryService QueryService { get; set; }
    }

On accessing the controller I'm getting such an error:
System.InvalidOperationException
The property 'QueryService' on controller 'WebApplication5.Controllers.ValuesController' cannot be activated.

If I add a constructor with the dependency (ValuesController(IQueryService queryService)) then controller is activated.

What is wrong with ActivateAttribute?

@pranavkm
Copy link
Contributor

You need to use [FromServices] - see #2476 for a previous discussion on this.

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

2 participants