Exception while creating ExceptionManager Class Object

Mar 17, 2011 at 11:36 AM

I have encountered the following error while creating a very simple application using the Enterprise Library for Exception Handling. Any help is appreciated. Following is the code and the error.

 

 public void DivideByZero()
        {
            int a = 10, b = 0;

            try
            {
                int c = a / b;
            }
            catch (Exception ex)
            {
                Exception throwme;            
              
                ExceptionManager exmngr = EnterpriseLibraryContainer.Current.GetInstance<ExceptionManager>();
              
                bool rethrow = exmngr.HandleException(ex, "TestPolicy",out throwme);

                if (rethrow)   
                    throw ;
               
            }

The XML config File. 

ERROR:

Unhandled Exception: Microsoft.Practices.ServiceLocation.
ActivationException: Activation error occured while trying to get instance of type ExceptionManager,
key "" ---> Microsoft.Practices.Unity.
ResolutionFailedException: Resolution of the dependency failed, type = "Microsoft.Practices.EnterpriseLibrary.ExceptionHandl
ing.ExceptionManager", name = "(none)".
Exception occurred while: while resolving.
Exception is: InvalidOperationException - The type ExceptionManager cannot be constructed. You must configure the container to supply this value.
-----------------------------------------------
At the time of the exception, the container was:

  Resolving Microsoft.Practices.EnterpriseLibrary.ExceptionHandling.ExceptionMan
ager,(none)
 ---> System.InvalidOperationException: The type ExceptionManager cannot be cons
tructed. You must configure the container to supply this value.
   at Microsoft.Practices.ObjectBuilder2.DynamicMethodConstructorStrategy.GuardT
ypeIsNonPrimitive(IBuilderContext context, SelectedConstructor selectedConstruct
or) in e:\Builds\Unity\UnityTemp\Compile\Unity\Unity\Src\ObjectBuilder\Strategie
s\BuildPlan\DynamicMethod\Creation\DynamicMethodConstructorStrategy.cs:line 289
   at Microsoft.Practices.ObjectBuilder2.DynamicMethodConstructorStrategy.PreBui
ldUp(IBuilderContext context) in e:\Builds\Unity\UnityTemp\Compile\Unity\Unity\S
rc\ObjectBuilder\Strategies\BuildPlan\DynamicMethod\Creation\DynamicMethodConstr
uctorStrategy.cs:line 71
   at Microsoft.Practices.ObjectBuilder2.StrategyChain.ExecuteBuildUp(IBuilderCo
ntext context) in e:\Builds\Unity\UnityTemp\Compile\Unity\Unity\Src\ObjectBuilde
r\Strategies\StrategyChain.cs:line 110
   at Microsoft.Practices.ObjectBuilder2.DynamicMethodBuildPlanCreatorPolicy.Cre
atePlan(IBuilderContext context, NamedTypeBuildKey buildKey) in e:\Builds\Unity\
UnityTemp\Compile\Unity\Unity\Src\ObjectBuilder\Strategies\BuildPlan\DynamicMeth
od\DynamicMethodBuildPlanCreatorPolicy.cs:line 48
   at Microsoft.Practices.ObjectBuilder2.BuildPlanStrategy.PreBuildUp(IBuilderCo
ntext context) in e:\Builds\Unity\UnityTemp\Compile\Unity\Unity\Src\ObjectBuilde
r\Strategies\BuildPlan\BuildPlanStrategy.cs:line 37
   at Microsoft.Practices.ObjectBuilder2.StrategyChain.ExecuteBuildUp(IBuilderCo
ntext context) in e:\Builds\Unity\UnityTemp\Compile\Unity\Unity\Src\ObjectBuilde
r\Strategies\StrategyChain.cs:line 110
   at Microsoft.Practices.Unity.UnityContainer.DoBuildUp(Type t, Object existing
, String name, IEnumerable`1 resolverOverrides) in e:\Builds\Unity\UnityTemp\Com
pile\Unity\Unity\Src\UnityContainer.cs:line 511
   --- End of inner exception stack trace ---
   at Microsoft.Practices.Unity.UnityContainer.DoBuildUp(Type t, Object existing
, String name, IEnumerable`1 resolverOverrides) in e:\Builds\Unity\UnityTemp\Com
pile\Unity\Unity\Src\UnityContainer.cs:line 515
   at Microsoft.Practices.Unity.UnityContainer.Resolve(Type t, String name, Reso
lverOverride[] resolverOverrides) in e:\Builds\Unity\UnityTemp\Compile\Unity\Uni
ty\Src\UnityContainer.cs:line 173
   at Microsoft.Practices.Unity.UnityServiceLocator.DoGetInstance(Type serviceTy
pe, String key) in e:\Builds\Unity\UnityTemp\Compile\Unity\Unity\Src\UnityServic
eLocator.cs:line 64
   at Microsoft.Practices.ServiceLocation.ServiceLocatorImplBase.GetInstance(Typ
e serviceType, String key) in c:\Home\Chris\Projects\CommonServiceLocator\main\M
icrosoft.Practices.ServiceLocation\ServiceLocatorImplBase.cs:line 49
   --- End of inner exception stack trace ---
   at Microsoft.Practices.ServiceLocation.ServiceLocatorImplBase.GetInstance(Typ
e serviceType, String key) in c:\Home\Chris\Projects\CommonServiceLocator\main\M
icrosoft.Practices.ServiceLocation\ServiceLocatorImplBase.cs:line 53
   at Microsoft.Practices.ServiceLocation.ServiceLocatorImplBase.GetInstance[TSe
rvice]() in c:\Home\Chris\Projects\CommonServiceLocator\main\Microsoft.Practices
.ServiceLocation\ServiceLocatorImplBase.cs:line 90
   at Exception_Handling_Test_C.Program.DivideByZero() in C:\Dev\Exception_Handl
ing_Testing\Exception_Handling_Test_C\Program.cs:line 38
   at Exception_Handling_Test_C.Program.Main(String[] args) in C:\Dev\Exception_
Handling_Testing\Exception_Handling_Test_C\Program.cs:line 23

 

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
    <configSections>
        <section name="exceptionHandling" type="Microsoft.Practices.EnterpriseLibrary.ExceptionHandling.Configuration.ExceptionHandlingSettings, Microsoft.Practices.EnterpriseLibrary.ExceptionHandling, Version=5.0.414.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" requirePermission="true" />
        <section name="dataConfiguration" type="Microsoft.Practices.EnterpriseLibrary.Data.Configuration.DatabaseSettings, Microsoft.Practices.EnterpriseLibrary.Data, Version=5.0.414.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" requirePermission="true" />
    </configSections>
    <exceptionHandling>
        <exceptionPolicies>
            <add name="TestPolicy">
                <exceptionTypes>
                    <add name="All Exceptions" type="System.Exception, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"
                        postHandlingAction="ThrowNewException">
                        <exceptionHandlers>
                            <add name="Wrap Handler" type="Microsoft.Practices.EnterpriseLibrary.ExceptionHandling.WrapHandler, Microsoft.Practices.EnterpriseLibrary.ExceptionHandling, Version=5.0.414.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
                                exceptionMessage="There seems to be an error in you application. Plese check"
                                wrapExceptionType="System.ApplicationException, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" />
                        </exceptionHandlers>
                    </add>
                </exceptionTypes>
            </add>
        </exceptionPolicies>
    </exceptionHandling>
    <dataConfiguration defaultDatabase="LocalSqlServer" />
</configuration>

 

Mar 17, 2011 at 4:11 PM

ANy help on the above error is greatly appreciated. Since I am very new to the Enterprise library hence i might not be implementing it correctly. Please Help.

Mar 18, 2011 at 4:29 AM

This kind of error is inevitably because the configuration you think you're using is not the one the framework is using. Is this configuration in your app.config file, or in a file for a DLL? Is this in an application or in a unit test harness? Is the app.config file being copied to the bin directory?

Mar 18, 2011 at 10:54 AM

I had changed the name of my configuration file to something other than App.config, hence it was not working. I changed it to App.config and got rid of the error. Can't i rename my config file and get my aplication to use that file?

Also I get the exception now, however the Wrap Exception does not work.  The following is how i try and throw the new wrapped exception.

catch (Exception ex)
            {
                Exception throwme;            
              
                var exmngr = EnterpriseLibraryContainer.Current.GetInstance<ExceptionManager>();
              
                bool rethrow = exmngr.HandleException(ex, "TestPolicy",out throwme);

                if (rethrow)   
                    throw throwme;
               
            }

Mar 18, 2011 at 11:48 AM
Edited Mar 18, 2011 at 11:53 AM

Hi,

Unfortunately, Enterprise Library will refer to the configuration file named App.config from your solution explorer. You can however use an external configuration file by following the steps here. Regarding your Wrap Exception Handler can you check if your PostHandlingAction is set to ThrowNewException?  Hope this helps :)

Mar 18, 2011 at 5:37 PM

Actually, it's not Enterprise Library that's loading App.config, it's the .NET Framework itself. That's how the framework works.

Technically, it's not App.config, it's <exename>.exe.config in the same directory as the .exe file. Visual Studio will copy and rename a file named App.config in your project directory.