jeudi 16 mars 2017

Asp.MVC custom handle error attribute does not work with Castle Windsor DI

I am working on an ASP.NET MVC application. I have used Castle Windsor for DI and have created a custom handler error attribute to handle errors in controllers.

I have created a custom windsor controller factory to use the DI. Now the issue is, with castle windsor, the custom error handler attribute code is never called, and I directly get the yellow screen of error.

When an error occurs in the controller, the error bubbles up to GetControllerInstance() method of WindsorControllerFactory, and then it never reaches the custom error handler of MVC.

Can anyone help me, how can I get the custom handle error attribute to work?

Please Note: Without the DI setup, the custom error handler works like charm

public override void OnException(ExceptionContext exceptionContext)
{
    var controllerName = (string)exceptionContext.RouteData.Values["controller"];
    var actionName = (string)exceptionContext.RouteData.Values["action"];
    var model = new HandleErrorInfo(exceptionContext.Exception, controllerName, actionName);

    exceptionContext.Result = new ViewResult
    {
        ViewName = View,
        MasterName = Master,
        ViewData = new ViewDataDictionary<HandleErrorInfo>(model),
        TempData = exceptionContext.Controller.TempData
    };

    exceptionContext.ExceptionHandled = true;
    exceptionContext.HttpContext.Response.Clear();
    exceptionContext.HttpContext.Response.StatusCode = (int)HttpStatusCode.InternalServerError;

    exceptionContext.HttpContext.Response.TrySkipIisCustomErrors = true;
}


public class WindsorControllerFactory : DefaultControllerFactory
{
    private readonly IKernel kernel;

    public WindsorControllerFactory(IKernel kernel)
    {
        this.kernel = kernel;
    }

    public override void ReleaseController(IController controller)
    {
        kernel.ReleaseComponent(controller);
    }

    protected override IController GetControllerInstance(RequestContext requestContext, Type controllerType)
    {
        if (controllerType == null)
        {
            throw new HttpException(404, string.Format("The controller for path '{0}' could not be found.", requestContext.HttpContext.Request.Path));
        }
        return (IController)kernel.Resolve(controllerType);
    }
}

public class MvcApplication : System.Web.HttpApplication
{
    private static IWindsorContainer container;

    protected void Application_Start()
    {
        AreaRegistration.RegisterAllAreas();
        RouteConfig.RegisterRoutes(RouteTable.Routes);
        FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
        InitializeContainer();
    }

    private static void InitializeContainer()
    {
        container = new WindsorContainer()
            .Install(FromAssembly.This());
        var controllerFactory = new WindsorControllerFactory(container.Kernel);
        ControllerBuilder.Current.SetControllerFactory(controllerFactory);
    }

    protected void Application_End()
    {
        container.Dispose();
    }
}

Aucun commentaire:

Enregistrer un commentaire