Brian Button - One Agile Coder

Blogging on all things .Net, C#, and Agile

My Links

Blog Stats

News

Archives

Post Categories

Agile Solutions Group

Blogs I read

Enterprise Library V1 Team

Configuration ExceptionHandling without using an external configuration file

A couple of weeks ago, someone posted a question on the Enterprise Library GDN workspace, asking about how they could configure the Exception Handling block without using an external configuration file. I wrote a bit of code to do that, and I wanted to share it with everyone else.

Here is my solution. This first part is a very small change I made to ExceptionPolicy to allow it to accept an ExceptionPolicyFactory through an additional HandledException method.

//===============================================================================
// Microsoft patterns & practices Enterprise Library
// Exception Handling Application Block
//===============================================================================
// Copyright © Microsoft Corporation. All rights reserved.
// Adapted from ACA.NET with permission from Avanade Inc.
// ACA.NET copyright © Avanade Inc. All rights reserved.
// THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY
// OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT
// LIMITED TO THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
// FITNESS FOR A PARTICULAR PURPOSE.
//===============================================================================

using System;
using System.Collections;
using System.Configuration;
using Microsoft.Practices.EnterpriseLibrary.Common.Configuration.ObjectBuilder;
using Microsoft.Practices.EnterpriseLibrary.Common.Instrumentation;
using Microsoft.Practices.EnterpriseLibrary.ExceptionHandling.Configuration;
using Microsoft.Practices.EnterpriseLibrary.ExceptionHandling.Properties;
using Microsoft.Practices.EnterpriseLibrary.ExceptionHandling.Instrumentation;

namespace Microsoft.Practices.EnterpriseLibrary.ExceptionHandling
{
    /// 
    /// Represents a policy with exception types and
    /// exception handlers. 
    /// 
    public static class ExceptionPolicy
    {
        private static readonly ExceptionPolicyFactory policyFactory = new ExceptionPolicyFactory();

        public static bool HandleException(Exception exceptionToHandle, string policyName)
        {
            return HandleException(exceptionToHandle, policyName, policyFactory);
        }

        public static bool HandleException(Exception exceptionToHandle, string policyName, ExceptionPolicyFactory factory)
        {
            if (exceptionToHandle == null) throw new ArgumentNullException("exceptionToHandle");
            if (factory == null) throw new ArgumentException("factory");
            if (string.IsNullOrEmpty(policyName)) throw new ArgumentException(Resources.ExceptionStringNullOrEmpty);

            ExceptionPolicyImpl policy = GetExceptionPolicy(exceptionToHandle, policyName, factory);

            return policy.HandleException(exceptionToHandle);
        }

        private static ExceptionPolicyImpl GetExceptionPolicy(Exception exception, string policyName, ExceptionPolicyFactory factory)
        {
            try
            {
                return factory.Create(policyName);
            }
            catch (ConfigurationErrorsException configurationException)
            {
                try
                {
                    DefaultExceptionHandlingEventLogger logger = EnterpriseLibraryFactory.BuildUp();
                    logger.LogConfigurationError(configurationException, policyName);
                }
                catch { }
                throw;
            }

            catch (Exception ex)
            {
                try
                {
                    string exceptionMessage = ExceptionUtility.FormatExceptionHandlingExceptionMessage(policyName, ex, null, exception);

                    DefaultExceptionHandlingEventLogger logger = EnterpriseLibraryFactory.BuildUp();
                    logger.LogInternalError(policyName, exceptionMessage);
                }
                catch { }

                throw new ExceptionHandlingException(ex.Message, ex);
            }
        }
    }
}

And here is an example I took from the ExceptionHandling basic quickstart. If you take this code and put it in the QuickStartForm.Main method and adjust the constructor for AppService to take an ExceptionPolicyFactory, then you should have a complete example. I’ve tried to indent the construction of the objects to make it more clear about what is being created and how it all gets hooked up.

[STAThread]
static void Main()
{
    DictionaryConfigurationSource internalConfigurationSource = new DictionaryConfigurationSource();
        ExceptionHandlingSettings settings = new ExceptionHandlingSettings();
            NamedElementCollection policyData = settings.ExceptionPolicies;
                ExceptionPolicyData globalPolicyData = new ExceptionPolicyData("Global Policy");
                    NamedElementCollection globalPolicyExceptionTypes = globalPolicyData.ExceptionTypes;
                        ExceptionTypeData exceptionType = new ExceptionTypeData("Exception", typeof(Exception), PostHandlingAction.None);
                            NameTypeConfigurationElementCollection exceptionTypeHandlers = exceptionType.ExceptionHandlers;
                            exceptionTypeHandlers.Add(new CustomHandlerData("Custom Handler", typeof(AppMessageExceptionHandler)));
                    globalPolicyExceptionTypes.Add(exceptionType);
                ExceptionPolicyData handleAndResumeData = new ExceptionPolicyData("Handle and Resume Policy");
                    NamedElementCollection handleAndResumeExceptionTypes = handleAndResumeData.ExceptionTypes;
                    handleAndResumeExceptionTypes.Add(exceptionType);
                ExceptionPolicyData propagatePolicyData = new ExceptionPolicyData("Propagate Policy");
                    NamedElementCollection propagatePolicyExceptionTypes = propagatePolicyData.ExceptionTypes;
                        ExceptionTypeData exceptionWithRethrowType = new ExceptionTypeData("Exception", typeof(Exception), PostHandlingAction.NotifyRethrow);
                    propagatePolicyExceptionTypes.Add(exceptionWithRethrowType);
                ExceptionPolicyData replacePolicyData = new ExceptionPolicyData("Replace Policy");
                    NamedElementCollection replacePolicyExceptionTypes = replacePolicyData.ExceptionTypes;
                        ExceptionTypeData securityExceptionType = new ExceptionTypeData("SecurityException", typeof(SecurityException), PostHandlingAction.ThrowNewException);
                            NameTypeConfigurationElementCollection securityExceptionTypeHandlers = securityExceptionType.ExceptionHandlers;
                            securityExceptionTypeHandlers.Add(new ReplaceHandlerData("Replace Handler", "Replaced Exception: User is not authorized to peform the requested action.", typeof(ApplicationException)));
                    replacePolicyExceptionTypes.Add(securityExceptionType);
                ExceptionPolicyData wrapPolicyData = new ExceptionPolicyData("Wrap Policy");
                    NamedElementCollection wrapPolicyExceptionTypes = wrapPolicyData.ExceptionTypes;
                        ExceptionTypeData dbConcurrencyExceptionType = new ExceptionTypeData("DBConcurrencyException", typeof(DBConcurrencyException), PostHandlingAction.ThrowNewException);
                            NameTypeConfigurationElementCollection dbConcurrencyExceptionTypeHandlers = dbConcurrencyExceptionType.ExceptionHandlers;
                            dbConcurrencyExceptionTypeHandlers.Add(new WrapHandlerData("Wrap Handler", "Wrapped Exception: A recoverable error occurred while attempting to access the database.", typeof(BusinessLayerException)));
                    wrapPolicyExceptionTypes.Add(dbConcurrencyExceptionType);
                  
                policyData.Add(globalPolicyData);
                policyData.Add(handleAndResumeData);
                policyData.Add(propagatePolicyData);
                policyData.Add(replacePolicyData);
                policyData.Add(wrapPolicyData);

        internalConfigurationSource.Add(ExceptionHandlingSettings.SectionName, settings);
        policyFactory = new ExceptionPolicyFactory(internalConfigurationSource);

    AppForm = new QuickStartForm();
    Application.Run(AppForm);
}

If this example isn’t clear to you, please let me know, and I can try to answer any questions that any of you may still have.
— bab
 

posted on Monday, February 20, 2006 8:34 AM

Feedback

# re: Configuration ExceptionHandling without using an external configuration file 2/21/2006 10:45 AM David Hayden

Here is another approach to the same problem.

http://davidhayden.com/blog/dave/archive/2006/02/18/2805.aspx

# re: Configuration ExceptionHandling without using an external configuration file 2/21/2006 3:38 PM Alois Kraus

Hi David,

your post does programatically configure the logging application block and not the Exception Handling block. You are very active at the moment. If we advertise Entlib in our companies even more they will need Entlib Consultants ;-)

Yours,

Alois Kraus

# re: Configuration ExceptionHandling without using an external configuration file 2/22/2006 7:02 AM David Hayden

Brian and Alois,

I didn't write that comment above. I guess someone read my post on the logging application block and confused that article with this post. Nice of them to mention my article, but I wish they would have used their own name :)

I will be checking out this post soon, however, as I am very interested in the progammatic areas of Ent Lib.

Regards,

Dave

# .NET Resources 5/6/2006 1:44 AM mattonsoftware.com

The following links to .NET resources have been collated over time with the assistance of colleagues. ...

# re: Configuration ExceptionHandling without using an external configuration file 9/21/2006 8:46 AM Java Developer

Thank you for your solution.

# re: Configuration ExceptionHandling without using an external configuration file 10/5/2006 1:50 AM apapd

http://hometown.aol.de/merranus/
http://hometown.aol.de/goamatrice/
http://hometown.aol.de/lotasian/
http://hometown.aol.de/tietyass/
http://hometown.aol.de/nestbaise/
http://hometown.aol.de/quetbeurette/
http://hometown.aol.de/pabbikini/
http://hometown.aol.de/garbbisexuel/
http://hometown.aol.de/esbblack/
http://hometown.aol.de/panbblonde/
http://hometown.aol.de/yaeboob/
http://hometown.aol.de/haebrune/
http://hometown.aol.de/cifcelebrite/
http://hometown.aol.de/aafchaleur/
http://hometown.aol.de/rrifcharme/
http://hometown.aol.de/baflitoris/
http://hometown.aol.de/iesfcochon/
http://hometown.aol.de/pafcouille/
http://hometown.aol.de/nybenculer/
http://hometown.aol.de/aaberotique/
http://hometown.aol.de/baiberotisme/
http://hometown.aol.de/lanbetudiante/
http://hometown.aol.de/dobexhibitionnis/
http://hometown.aol.de/rubellation/
http://hometown.aol.de/sexeuu/
http://hometown.aol.de/sexuuh/
http://hometown.aol.de/ornouu/
http://hometown.aol.de/ornuuu/
http://hometown.aol.de/movieuu/
http://hometown.aol.de/bifemmet/
http://hometown.aol.de/asfemme/
http://hometown.aol.de/omofesse/
http://hometown.aol.de/refetiche/
http://hometown.aol.de/nasfetichisme/
http://hometown.aol.de/elfilmm/
http://hometown.aol.de/vefilmm/
http://hometown.aol.de/rafilmsh/
http://hometown.aol.de/nofilms/
http://hometown.aol.de/hagirll/
http://hometown.aol.de/llegratuite/
http://hometown.aol.de/gaogratuite/
http://hometown.aol.de/lasgros/
http://hometown.aol.de/regrossee/
http://hometown.aol.de/jashardcoree/
http://hometown.aol.de/dehomosexuel/
http://hometown.aol.de/mihotth/
http://hometown.aol.de/venimage/
http://hometown.aol.de/talatinas/
http://hometown.aol.de/nalesbian/
http://hometown.aol.de/esmatureh/
http://hometown.aol.de/tanmodels/
http://hometown.aol.de/yemovie/
http://hometown.aol.de/nimureh/
http://hometown.aol.de/tasnude/
http://hometown.aol.de/denudiste/
http://hometown.aol.de/floorgasme/
http://hometown.aol.de/resorgie/
http://hometown.aol.de/keadultee/
http://hometown.aol.de/cuanamateur/
http://hometown.aol.de/doamateur/
http://hometown.aol.de/paanall/
http://hometown.aol.de/saasiatique/
http://hometown.aol.de/mibiteh/
http://hometown.aol.de/flacoquin/
http://hometown.aol.de/mencull/
http://hometown.aol.de/cagayy/
http://hometown.aol.de/kegayy/
http://hometown.aol.de/yohard/
http://hometown.aol.de/lehistoire/
http://hometown.aol.de/tijeune/
http://hometown.aol.de/rolesbienne/
http://hometown.aol.de/flomangaa/
http://hometown.aol.de/resnoire/
http://hometown.aol.de/kinuhh/
http://hometown.aol.de/eroenis/
http://hometown.aol.de/kephotoo/
http://hometown.aol.de/mephotoh/
http://hometown.aol.de/bapied/
http://hometown.aol.de/hilpipe/
http://hometown.aol.de/espoitrine/
http://hometown.aol.de/stornn88/
http://hometown.aol.de/aaorno/
http://hometown.aol.de/rumorno/
http://hometown.aol.de/fornographie/
http://hometown.aol.de/hkeussy/
http://hometown.aol.de/suerasee/
http://hometown.aol.de/hnarousse/
http://hometown.aol.de/tansadoo/
http://hometown.aol.de/flasalope/
http://hometown.aol.de/hmenex/
http://hometown.aol.de/casexuelle/
http://hometown.aol.de/itussexy/
http://hometown.aol.de/brasodomie/
http://hometown.aol.de/zossuce/
http://hometown.aol.de/sesuceasu/
http://hometown.aol.de/mueteen/
http://hometown.aol.de/vantithhh/
http://hometown.aol.de/altoonn/
http://hometown.aol.de/comtranssexuelle/
http://hometown.aol.de/pasvideooh/
http://hometown.aol.de/delvideoh/
http://hometown.aol.de/vivideosh/
http://hometown.aol.de/envideos/
http://hometown.aol.de/tovoyeur/
http://hometown.aol.de/echwebcam/
http://hometown.aol.de/aaxhi/
http://hometown.aol.de/lexhy/
http://hometown.aol.de/htoxxxi/
http://hometown.aol.de/disexee/
http://hometown.aol.de/tasex9/



http://hometown.aol.de/dosafari/
http://hometown.aol.de/minillusion/
http://hometown.aol.de/govoyager8/
http://hometown.aol.de/pueimagehumour/
http://hometown.aol.de/denblague/
http://hometown.aol.de/liblagues/
http://hometown.aol.de/garsportive/
http://hometown.aol.de/litourisme/
http://hometown.aol.de/guecheat/
http://hometown.aol.de/porwallpaper/
http://hometown.aol.de/akiyoga/
http://hometown.aol.de/ligcinema/
http://hometown.aol.de/ueppsppt/
http://hometown.aol.de/portenniss/
http://hometown.aol.de/ayatouristique/
http://hometown.aol.de/nofamille/
http://hometown.aol.de/hayecrandeveille/
http://hometown.aol.de/kienf1rallye/
http://hometown.aol.de/laecransveille/
http://hometown.aol.de/agsolutionjeu/
http://hometown.aol.de/uacodejeu/
http://hometown.aol.de/nteprogramme/
http://hometown.aol.de/ditelevision/
http://hometown.aol.de/bebemuye/
http://hometown.aol.de/midlingerie/
http://hometown.aol.de/oodicone/
http://hometown.aol.de/nodtarot/
http://hometown.aol.de/lodtatouage/
http://hometown.aol.de/dedtatouages/
http://hometown.aol.de/jetunning/
http://hometown.aol.de/ensjeuu/
http://hometown.aol.de/trasvideoshumour/
http://hometown.aol.de/pkssport/
http://hometown.aol.de/xksjeux/
http://hometown.aol.de/pkserecette/
http://hometown.aol.de/esvoiture/
http://hometown.aol.de/unzerotique/
http://hometown.aol.de/lazjeu/
http://hometown.aol.de/drzcouple/
http://hometown.aol.de/onzhistoire/
http://hometown.aol.de/oizvideohumour/
http://hometown.aol.de/gaferotiques/
http://hometown.aol.de/gufgolf/
http://hometown.aol.de/arfjeux/
http://hometown.aol.de/difjeu/
http://hometown.aol.de/aafjeux/
http://hometown.aol.de/yoffootball/
http://hometown.aol.de/syhtv/
http://hometown.aol.de/unhauto/
http://hometown.aol.de/gihsoluce/
http://hometown.aol.de/tahtele/
http://hometown.aol.de/nohhoroscope/
http://hometown.aol.de/fihmassage/
http://hometown.aol.de/nogspectacle/
http://hometown.aol.de/fithoroscopes/
http://hometown.aol.de/noyvoiture/
http://hometown.aol.de/iirrecette/
http://hometown.aol.de/fibrecettes/
http://hometown.aol.de/liwcarte/
http://hometown.aol.de/pifcartes/
http://hometown.aol.de/pabnofond/
http://hometown.aol.de/solhumour/
http://hometown.aol.de/poyrhumour/
http://hometown.aol.de/tutfonds/
http://hometown.aol.de/cawcarte/
http://hometown.aol.de/lleupartition/
http://hometown.aol.de/oyicartes/
http://hometown.aol.de/teediaporama/
http://hometown.aol.de/pueavoyage/
http://hometown.aol.de/doscartes/
http://hometown.aol.de/versastuce/
http://hometown.aol.de/consdiaporamas/
http://hometown.aol.de/losrtruc/
http://hometown.aol.de/otrefamiliale/
http://hometown.aol.de/oorsexy/
http://hometown.aol.de/ssrvacances/
http://hometown.aol.de/serenfant/
http://hometown.aol.de/losncartes/
http://hometown.aol.de/cuanvcarte/



http://hometown.aol.de/xxxgratuitf/
http://hometown.aol.de/videoxgratuitg/
http://hometown.aol.de/videosexegratui5/
http://hometown.aol.de/videosexgratuits/
http://hometown.aol.de/videotornograt88/
http://hometown.aol.de/videogratuitm/
http://hometown.aol.de/traducteurgratu/
http://hometown.aol.de/toutgratuiti/
http://hometown.aol.de/telechargerjeug7/
http://hometown.aol.de/telechargergragg/
http://hometown.aol.de/telechargementlh/
http://hometown.aol.de/telechargementg0/
http://hometown.aol.de/telechafilmgratu/
http://hometown.aol.de/tarotgratuitv/
http://hometown.aol.de/sudokugratuitr/
http://hometown.aol.de/smsgratuite/
http://hometown.aol.de/sexegratuitr/
http://hometown.aol.de/sexeamateurgrat9/
http://hometown.aol.de/sexgratuitbeure9/
http://hometown.aol.de/sexgratuitg/
http://hometown.aol.de/tornogratuit888/
http://hometown.aol.de/photosexegratuiy/
http://hometown.aol.de/photosexgrat888/
http://hometown.aol.de/phototornograty/
http://hometown.aol.de/photogaygrayyy/
http://hometown.aol.de/mp3gratuity/
http://hometown.aol.de/logicielgratuitu/
http://hometown.aol.de/jeuvoituregratyy/
http://hometown.aol.de/jeuvideogratuitd/
http://hometown.aol.de/jeupcgratuitf/
http://hometown.aol.de/jeugratuitcadeux/
http://hometown.aol.de/jeugratuitg/
http://hometown.aol.de/jeuenfantgrat888/
http://hometown.aol.de/jeuadultegratuiy/
http://hometown.aol.de/horoscopegratuuu/
http://hometown.aol.de/hentaigratuitlrr/
http://hometown.aol.de/grosseingratuitu/
http://hometown.aol.de/gaygratuitg/
http://hometown.aol.de/fondecrangratu9y/
http://hometown.aol.de/filmxgratuity66/
http://hometown.aol.de/filmtornogratuio/
http://hometown.aol.de/emoticonegratu88/
http://hometown.aol.de/ecranveillegrayy/
http://hometown.aol.de/culgratuite77/
http://hometown.aol.de/clipgratuitd/
http://hometown.aol.de/chatgratuitryy7/
http://hometown.aol.de/antivirusgratu7/
http://hometown.aol.de/chansongratuitv/
http://hometown.aol.de/chansonsgratuitd/
http://hometown.aol.de/divxgratuitd55/
http://hometown.aol.de/emulegratuiteg/
http://hometown.aol.de/kazaagratuitf/
http://hometown.aol.de/logicielgratuith/
http://hometown.aol.de/logicielsgratuit/
http://hometown.aol.de/messengergratuii/
http://hometown.aol.de/mp3gratuito/
http://hometown.aol.de/msngratuith/
http://hometown.aol.de/musicgratuiteu/
http://hometown.aol.de/musiquegratuiteh/
http://hometown.aol.de/musiquesgratuity/
http://hometown.aol.de/nerogratuitu/
http://hometown.aol.de/parolegratuitj/
http://hometown.aol.de/parolesgratuiti/
http://hometown.aol.de/telechagratuitop/
http://hometown.aol.de/telechargergrath/
http://hometown.aol.de/telechaantiviruy/
http://hometown.aol.de/telechachansonf/
http://hometown.aol.de/telechachansonsh/
http://hometown.aol.de/telechadivxg/
http://hometown.aol.de/telechaemuled/
http://hometown.aol.de/telechakazaae/
http://hometown.aol.de/telechalogicielh/
http://hometown.aol.de/telechalogiciely/
http://hometown.aol.de/telechamessengey/
http://hometown.aol.de/telechamp3f/
http://hometown.aol.de/telechamsnt/
http://hometown.aol.de/telechamusich/
http://hometown.aol.de/telechamusiquei/
http://hometown.aol.de/telechaneroy/
http://hometown.aol.de/telechaparolej/
http://hometown.aol.de/telechaparolesi/
http://hometown.aol.de/telechay/
http://hometown.aol.de/telechargerantiy/
http://hometown.aol.de/telechargerchany/
http://hometown.aol.de/telechargerchanh/
http://hometown.aol.de/telechargerdivxt/
http://hometown.aol.de/telechargeremulu/
http://hometown.aol.de/telechargerkazah/
http://hometown.aol.de/telechargerlogij/
http://hometown.aol.de/telechargerlogip/
http://hometown.aol.de/telechargermessy/
http://hometown.aol.de/telechargermp3f/
http://hometown.aol.de/telechargermsngt/
http://hometown.aol.de/telechargermusiy/
http://hometown.aol.de/telechargermusii/
http://hometown.aol.de/telechargernerot/
http://hometown.aol.de/telechargerparok/
http://hometown.aol.de/telechargerparoj/

# re: Configuration ExceptionHandling without using an external configuration file 11/15/2007 9:33 AM seo

Nicely done with the codes. Thanks for sharing your solution.

Title  
Name  
Url
Spam Protection:
Enter the code you see:
If you can't read it, click your refresh button to get a new image.
Comments