Ticket

Get instance of IOrchardServices in HandleErrorAttribute?

I have a module with a controller.

I wrote a HandleErrorAttribute for use on certain actions of this controller, so Orchard's default UnhandledExceptionFilter wouldn't touch the exception.

I can't figure out how to access the instance of IOrchardServices in my attribute's OnException method.

If I add a construction to the attribute like so:

    public MyControllerExceptionHandler(IOrchardServices orchardServices) {
        services = orchardServices;
    }

I get errors wherever I've used the attribute, since the classe no longer has a constructor that doesn't take any arguments.

The answer might very well be quite simple, but I'm not super familiar with Autofac...

Here's my code:

namespace MyProject.Controllers
{
    [Themed]
    public class MyController : Controller
    {
        protected IOrchardServices services { get; set; }

        public MyController(IOrchardServices iServices)
            {
                services = iServices;
        }

        [HttpGet]
        [MyControllerExceptionHandler]
        public ActionResult Index()
        {
                // Exception thrown here
        }
    }
}




namespace MyProject.Filters
{
    [AttributeUsage(AttributeTargets.Method)]
    public class MyControllerExceptionHandler : HandleErrorAttribute
    {
        public IOrchardServices services { get; set; }
        public Localizer T { get; set; }   // <-- This gets populated properly

        public AccountControllerExceptionHandler(IOrchardServices orchardServices) {
            services = orchardServices;
        }

        public override void OnException(ExceptionContext filterContext)
        {
            services.Notifier.Add(Orchard.UI.Notify.NotifyType.Information, T("Oops."));
        }
    }
}

Post a reply

You need to be signed in to post a reply.

Sign In