Closed pranavkm closed 7 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?
@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.
@tuespetre, Thanks, now I have two options to try
IFilterFactory
and context.HttpContext.RequestServices.GetService<>()
@tariqalardah why not use ServiceFilters - https://docs.asp.net/en/latest/mvc/controllers/filters.html?#configuring-filters?
@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.
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 ?
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.
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.