locked
Using Enterprise Library for Web Api MVC4 project RRS feed

  • Question

  • Hi, We are using MVC4 Web Api controllers and also have Enterprise Library 5.0 for exception handling and logging. Since Web api will throw HTTP messages, I am not able to perform exception handling. Can you please suggest on how to handle and log exceptions using EL for MVC4 Web Api projects.

    Thanks,

    Pugal

    Friday, October 4, 2013 1:14 PM

Answers

  • Hi Pugal, how you doing?

       To address this issue you should create a Custom Error Handler Class. It´s often something like the example below:

    namespace MvcApplication2.Common
    {
        public class EntLibErrorHandler : System.Web.Mvc.HandleErrorAttribute
        {
            public override void OnException(System.Web.Mvc.ExceptionContext filterContext)
            {
                //Use Exception Handling App Block here
            }
        }   
    }

     After that you should register this Class as a filter in your application. In order to do that, go to AppStart folder and look for FilterConfig class.

    There your code shold like this:

    namespace MvcApplication2
    {
        public class FilterConfig
        {
            public static void RegisterGlobalFilters(GlobalFilterCollection filters)
            {
                filters.Add(new HandleErrorAttribute());
    
                //Your custom Error Handler
                filters.Add(new Common.EntLibErrorHandler());
            }
        }
    }

    And that´s all. Every unhandled exception in your application will be handled by your class.

    I guess you are not having issues dealing with ent. lib, if so let me know, ok?

    Regards

      
    Friday, October 4, 2013 6:36 PM

All replies

  • Hi Pugal, how you doing?

       To address this issue you should create a Custom Error Handler Class. It´s often something like the example below:

    namespace MvcApplication2.Common
    {
        public class EntLibErrorHandler : System.Web.Mvc.HandleErrorAttribute
        {
            public override void OnException(System.Web.Mvc.ExceptionContext filterContext)
            {
                //Use Exception Handling App Block here
            }
        }   
    }

     After that you should register this Class as a filter in your application. In order to do that, go to AppStart folder and look for FilterConfig class.

    There your code shold like this:

    namespace MvcApplication2
    {
        public class FilterConfig
        {
            public static void RegisterGlobalFilters(GlobalFilterCollection filters)
            {
                filters.Add(new HandleErrorAttribute());
    
                //Your custom Error Handler
                filters.Add(new Common.EntLibErrorHandler());
            }
        }
    }

    And that´s all. Every unhandled exception in your application will be handled by your class.

    I guess you are not having issues dealing with ent. lib, if so let me know, ok?

    Regards

      
    Friday, October 4, 2013 6:36 PM
  • Thanks Rafael.

    Will this work with Enterprise Library 5.0 or do I need a higher version?

    Sunday, October 6, 2013 2:20 PM
  • Hi Pugal,

        It will work with ent.lib 5.0. You don´t need to upgrade the version!

    Regards.

    Monday, October 7, 2013 12:56 PM
  • Thanks Rafael. My exception handling code is like below

    ExceptionPolicy.HandleException(ex, "Policy1");

    The first parameter is of type "Exception" in above line of code whereas the method specified by you provide only System.Web.Mvc.ExceptionContext. How do I use exception handling here?

    Thanks,

    Pugal


    Thanks, Pugazhendhi B

    Monday, October 14, 2013 8:30 AM
  • Hi Pugal, 

       The System.Web.Mvc.ExceptionContext class provides a property, called Exception,  with the type you are looking for. 

        So, your code will look like this: 

      public class EntLibErrorHandler : System.Web.Mvc.HandleErrorAttribute
        {
            public override void OnException(System.Web.Mvc.ExceptionContext filterContext)
            {
                ExceptionPolicy.HandleException(filterContext.Exception, "Policy1")
            }
        }

       Regards.

    Rafael - http://rafatolotti.wordpress.com/

    Monday, October 14, 2013 2:55 PM