I am using Castle Windsor in my application and I would like to use inject some services example ILog in my ExceptionFilterAttribute :
public class GenericExceptionFilterAttribute : ExceptionFilterAttribute
{
private readonly ILog _logger;
public GenericExceptionFilterAttribute()
{
}
public GenericExceptionFilterAttribute(ILogManager logManager)
{
_logger = logManager.GetLogger(typeof(GenericExceptionFilterAttribute));
}
}
How can I inject services in this class ?
Regards
Carlos
NET supports the dependency injection (DI) software design pattern, which is a technique for achieving Inversion of Control (IoC) between classes and their dependencies. Dependency injection in . NET is a built-in part of the framework, along with configuration, logging, and the options pattern.
A view component class: Supports constructor dependency injection. Doesn't take part in the controller lifecycle, therefore filters can't be used in a view component.
Dependency Injection (DI) is a software design pattern that allows us to develop loosely coupled code. DI is a great way to reduce tight coupling between software components. DI also enables us to better manage future changes and other complexity in our software. The purpose of DI is to make code maintainable.
Once the container has been created, the IServiceCollection instance is composed into an IServiceProvider instance. You can use this instance to resolve services. You can inject an instance of type IServiceProvider into any method of a class.
Hi the dependencyResolver to solve this :
public override void OnException(HttpActionExecutedContext context)
{
var log= (ILog)context.ActionContext.ControllerContext.Configuration.DependencyResolver.GetService(typeof(ILog));
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With