How can i used Attribute property in Custom handler in Exception Handling Application block

Topics: Exception Handling Application Block
Apr 20, 2010 at 8:01 AM


Can you please explain how can i use the attribute propery in custom handler. sample will be great helpful.



Apr 20, 2010 at 8:19 AM

You can use it to  avoid hard-coding values in your custom handler and allow users to specify them in the config.  For example, if your custom exception handler utilizes other exception policies defined in your config, without the usage of that property, you would need to hard-code the name of the exception policy. 

Its values are populated from the list of items you added in the Attributes property in the config tool.

Sarah Urmeneta
Global Technology and Solutions
Avanade, Inc.

Apr 20, 2010 at 8:31 AM

thanks for the quick response. but how can i use it in the Custom handler class?


Apr 20, 2010 at 8:53 AM
Edited Apr 20, 2010 at 8:57 AM

Ok, let's consider the sample I mentioned above. Your custom handler handles exceptions based on certain conditions but if none of the conditions you apply, you want to use the "Global Exception Policy" which is defined in your config. To avoid hard-coding the string "Global Exception Policy" in your custom handler because overtime, this might change, you would add an item in the Attributes collection property of your custom handler. You would use "exceptionPolicy" as the key and the "Global Exception Policy" as the value. In your custom handler, you will access this value by :


Let me know if this is not clear.

Sarah Urmeneta
Global Technology and Solutions
Avanade, Inc.

Apr 20, 2010 at 9:01 AM


You have really helpful. I really appreciate it. Thanks a lot.

this how i created a custom handler. 

namespace CustomMsmqHandler



 public class CustomMsmqHandler : IExceptionHandler  


 public CustomMsmqHandler(NameValueCollection ignore) : base() { }
        MessageQueue queue;

        public Exception HandleException(Exception exception, Guid handlingInstanceId)      


     // Open the queue.            using (queue = new MessageQueue(".\\Private$\\MyQueue"))      


 // Enable the AppSpecific field in the messages.          

     queue.MessageReadPropertyFilter.AppSpecific = true;
                // Set the formatter to binary.            

   queue.Formatter = new BinaryMessageFormatter();
                // Since we're using a transactional queue, make a transaction.          

     using (MessageQueueTransaction mqt = new MessageQueueTransaction())        


                    // Create a simple text message.          

         Message msg = CreateMessage(exception);                   
                    // Send the message.          

         queue.Send(msg, mqt);        



 return exception;    


 /// <summary>    


 /// </summary>  

     /// <param name="exception"></param>      

 /// <returns></returns>    

   private Message CreateMessage(Exception exception)    


   Message queueMessage = new Message();        

   LogEntry entry = new LogEntry();          

 exception.Data["severity"] = System.Diagnostics.TraceEventType.Error;      

     exception.Data["timestamp"] = DateTime.Now;
            queueMessage.Body = exception;    

       queueMessage.Label = "Exception";
            return queueMessage;    



i get Following error

{"Cannot serialize member System.Exception.Data of type System.Collections.IDictionary, because it implements IDictionary."}

Apr 20, 2010 at 9:11 AM

The exception occurs because the Data property is not serializable; the BinaryFormatter requires the object to be serializable.  You need to find another way to pass the exception object, probably create your own custom exception and converting the contents of the Data property in a serializable form.


Sarah Urmeneta
Global Technology and Solutions
Avanade, Inc.

Apr 20, 2010 at 9:53 AM

thank you so much. I will try it and let you know.