aspnet / Mvc

[Archived] ASP.NET Core MVC is a model view controller framework for building dynamic web sites with clean separation of concerns, including the merged MVC, Web API, and Web Pages w/ Razor. Project moved to https://github.com/aspnet/AspNetCore
Apache License 2.0
5.62k stars 2.14k forks source link

[Discussion] FromServicesAttribute is no longer applicable on controller or model properties #3578

Closed pranavkm closed 7 years ago

pranavkm commented 8 years ago

FromServicesAttribute used to be applicable on properties and parameters. The former allowed services to be bound on controller and model properties. However there was a lot of confusion as to where and how this worked.

This change, limits FromServicesAttribute to only work with action arguments. FromServices on controller properties can be replaced by a DI framework that support property injection.

See https://github.com/aspnet/Mvc/issues/3507 for discussion for past discussions for this change.

tariqalardah commented 8 years ago

@Eilon, I am trying to implement my ExceptionFilterAttribute to be used on some API Controllers. public class MyExceptionHandlerAttribute : ExceptionFilterAttribute And planning to use IHostingEnvironment and my IRepository service in the implementation!

Then will use it on Controller, like,

[MyExceptionHandler]
public class MySampleController: Controller
{ }

Any chance this will work?

tuespetre commented 8 years ago

@tariqalardah you could get the services you want out of the ExceptionContext that gets passed into public override void OnException(ExceptionContext context) like this:

var services = context.HttpContext.RequestServices;
var environment = services.GetService<IHostingEnvironment>();
var repository = services.GetService<IRepository>();

Otherwise, if you want them to be constructor-injected, you would have to stick your IExceptionFilter in the service container and either use [ServiceFilter(typeof(MyExceptionFilter))] or make your own attribute that implements IFilterFactory and returns your filter from the service container.

tariqalardah commented 8 years ago

@tuespetre, Thanks, now I have two options to try IFilterFactory and context.HttpContext.RequestServices.GetService<>()

pranavkm commented 8 years ago

@tariqalardah why not use ServiceFilters - https://docs.asp.net/en/latest/mvc/controllers/filters.html?#configuring-filters?

tariqalardah commented 8 years ago

@pranavkm, I was going to use them after a time being trying and searching how to get the needed service in the custom attribute, as a last chance, I posted my question here. And fortunately, got the needed answer by using (IHostingEnvironment)context.HttpContext.RequestServices.GetService(typeof(IHostingEnvironment)); suggested by @tuespetre :+1: BTW, I couldn't find the generic GetService<T>() but no problem as GetService() is there.

Afshintm commented 8 years ago

Hi, Can somebody please give me a hand?

I am Autofac as DI in my .NET Core webApi following the docs here is my code in startup class:

       public IServiceProvider ConfigureServices(IServiceCollection services)
    {
        services.AddMvc();
        var builder = new ContainerBuilder();
        builder.RegisterModule<DefaultModule>(); 
       builder.Populate(services);
        this.ApplicationContainer = builder.Build();
        return this.ApplicationContainer.Resolve<IServiceProvider>(); 

}

DefaultModule.cs code

public class DefaultModule : Module
{
    protected override void Load(ContainerBuilder builder)
    {
        builder.RegisterType<BlogServices>().As<IBlogServices>();
    }
}

Controller Code:

private readonly IBlogServices _blogServices;
    public BlogsController(IBlogServices blogServices) {
        IBlogServices _blogServices = blogServices;
    }

But the resolved dependency passed to controller is null!!!!

Any Idea what am I missing please ?

Eilon commented 7 years ago

We are closing this issue because no further action is planned for this issue. If you still have any issues or questions, please log a new issue with any additional details that you have.