Activation error occured while trying to get instance of ExceptionPolicyImpl

Topics: Exception Handling Application Block, Logging Application Block
Jan 26, 2011 at 3:30 PM
Edited Jan 26, 2011 at 3:32 PM

I am trying to config Exception Handling with Logging block in Enterprise Library 5.0

  • I have created a sample WPF application
  • Added the necessary reference for Enterprise Library: Common, ExceptionHandling,Logging,Database
  • I tried to log the exception to a plain text file.
  • As soon as as I try to catch the exception: ExceptionPolicy.HandleException(ex, "Policy") I get the error: ActivationException was unhandled: Activation error occured while trying to get instance of ExceptionPolicyImpl

 

 

Jan 26, 2011 at 4:22 PM

What's in your app.config file? And what is the complete exception message?

 

Jan 26, 2011 at 5:02 PM
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
    <configSections>
        <section name="loggingConfiguration" type="Microsoft.Practices.EnterpriseLibrary.Logging.Configuration.LoggingSettings, Microsoft.Practices.EnterpriseLibrary.Logging, Version=5.0.414.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" requirePermission="true" />
        <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" />
    </configSections>
    <loggingConfiguration name="" tracingEnabled="true" defaultCategory="MyGeneral">
        <listeners>
            <add name="Flat File Trace Listener" type="Microsoft.Practices.EnterpriseLibrary.Logging.TraceListeners.FlatFileTraceListener, Microsoft.Practices.EnterpriseLibrary.Logging, Version=5.0.414.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
                listenerDataType="Microsoft.Practices.EnterpriseLibrary.Logging.Configuration.FlatFileTraceListenerData, Microsoft.Practices.EnterpriseLibrary.Logging, Version=5.0.414.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
                fileName="C:\Download\trace.log" formatter="Text Formatter"
                traceOutputOptions="LogicalOperationStack, DateTime, Timestamp, ProcessId, ThreadId, Callstack" />
        </listeners>
        <formatters>
            <add type="Microsoft.Practices.EnterpriseLibrary.Logging.Formatters.TextFormatter, Microsoft.Practices.EnterpriseLibrary.Logging, Version=5.0.414.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
                template="Timestamp: {timestamp}{newline}&#xA;Message: {message}{newline}&#xA;Category: {category}{newline}&#xA;Priority: {priority}{newline}&#xA;EventId: {eventid}{newline}&#xA;Severity: {severity}{newline}&#xA;Title:{title}{newline}&#xA;Machine: {localMachine}{newline}&#xA;App Domain: {localAppDomain}{newline}&#xA;ProcessId: {localProcessId}{newline}&#xA;Process Name: {localProcessName}{newline}&#xA;Thread Name: {threadName}{newline}&#xA;Win32 ThreadId:{win32ThreadId}{newline}&#xA;Extended Properties: {dictionary({key} - {value}{newline})}"
                name="Text Formatter" />
        </formatters>
        <categorySources>
            <add switchValue="All" name="MyGeneral">
                <listeners>
                    <add name="Flat File Trace Listener" />
                </listeners>
            </add>
        </categorySources>
        <specialSources>
            <allEvents switchValue="All" name="All Events">
                <listeners>
                    <add name="Flat File Trace Listener" />
                </listeners>
            </allEvents>
            <notProcessed switchValue="All" name="Unprocessed Category">
                <listeners>
                    <add name="Flat File Trace Listener" />
                </listeners>
            </notProcessed>
            <errors switchValue="All" name="Logging Errors &amp; Warnings">
                <listeners>
                    <add name="Flat File Trace Listener" />
                </listeners>
            </errors>
        </specialSources>
    </loggingConfiguration>
    <exceptionHandling>
        <exceptionPolicies>
            <add name="MyPolicy">
                <exceptionTypes>
                    <add name="All Exceptions" type="System.Exception, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"
                        postHandlingAction="NotifyRethrow">
                        <exceptionHandlers>
                            <add name="Logging Exception Handler" type="Microsoft.Practices.EnterpriseLibrary.ExceptionHandling.Logging.LoggingExceptionHandler, Microsoft.Practices.EnterpriseLibrary.ExceptionHandling.Logging, Version=5.0.414.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
                                logCategory="MyGeneral" eventId="100" severity="Error" title="Enterprise Library Exception Handling"
                                formatterType="Microsoft.Practices.EnterpriseLibrary.ExceptionHandling.TextExceptionFormatter, Microsoft.Practices.EnterpriseLibrary.ExceptionHandling"
                                priority="0" />
                        </exceptionHandlers>
                    </add>
                </exceptionTypes>
            </add>
        </exceptionPolicies>
    </exceptionHandling>
</configuration>
Jan 26, 2011 at 5:02 PM

This is how I am raising it

 

try
            {   
                throw new Exception("Another Newly Created Excpetion by Ent Library");
            }

            catch (System.Exception ex)
            {

                ExceptionPolicy.HandleException(ex, "Policy");

            }
Jan 26, 2011 at 5:04 PM

and I have reference added to Microsoft.Practices.EnterpriseLibrary.Common,Microsoft.Practices.EnterpriseLibrary.ExceptionHandling,Microsoft.Practices.EnterpriseLibrary.ExceptionHandling.Logging

Jan 26, 2011 at 5:04 PM

really appreciated your response and help

Jan 26, 2011 at 9:55 PM

Do you have the actual exception message, not just the stack trace?

 

Jan 26, 2011 at 9:57 PM

Activation error occured while trying to get instance of type ExceptionPolicyImpl, key "Policy"

Jan 26, 2011 at 10:02 PM

There should be a lot more there. Is there something like "at the time of the exception the container was..." and a list of where it was in the object graph?

The full exception message should be several lines long, and run from "Activation error occurred" to the start of the stack trace.

Jan 27, 2011 at 2:51 PM

THis is the whole message

Activation error occured while trying to get instance of type ExceptionPolicyImpl, key \"Policy\""}

Jan 27, 2011 at 2:52 PM

There is this inner exception:

Resolution of the dependency failed, type = \"Microsoft.Practices.EnterpriseLibrary.ExceptionHandling.ExceptionPolicyImpl\", name = \"Policy\".\r\nException occurred while: while resolving.\r\nException is: InvalidOperationException - The type ExceptionPolicyImpl has multiple constructors of length 2. Unable to disambiguate.\r\n-----------------------------------------------\r\n

At the time of the exception, the container was:\r\n\r\n  Resolving Microsoft.Practices.EnterpriseLibrary.ExceptionHandling.ExceptionPolicyImpl,Policy\r\n"}

Jan 28, 2011 at 8:32 AM

One thing I've noticed is that from your code behind the exception policy you're using is named as "Policy" but in your configuration what you have is "MyPolicy". Hope this helps.

Gino Terrado
Global Technologies and Solutions
Avanade, Inc.
entlib.support@avanade.com

Jan 28, 2011 at 3:30 PM

Good catch. I changed it,  but still the same error

Jan 29, 2011 at 8:26 PM

The policy name mismatch is a part of it, another issue may be that Entlib isn't configured using the configuration file you think it is.

 

Jan 30, 2011 at 11:58 PM

You should also set your PostHandlingAction to ThrowNewException.

 

Sarah Urmeneta
Global Technologies and Solutions
Avanade, Inc.
entlib.support@avanade.com

Jan 31, 2011 at 3:07 PM

PostHandlingAction to ThrowNewException did not help !

How do I check to see if it is configured properly to use the proper configuration file ?

 

Feb 1, 2011 at 1:29 AM

If its a WPF App then by default the correct configuration file to put your configuration setting should be in the file named - "App.config". This config file should also reside under your WPF App project. 

If you used other config filename then exception would really occur. See if this would help resolve your problem. If not, send us a repro project for this for us to further investigate.

Gino Terrado
Global Technologies and Solutions
Avanade, Inc.
entlib.support@avanade.com

Feb 1, 2011 at 2:24 PM

It is is resolved :-)

The WPF repro application I have been using, by default somehow it named the config file App1.config. Changing the name and making sure I am using the correct policy solved the problem.

Thank you very much for all you help.

 

Mar 12, 2012 at 7:34 AM

Hai,

     I am in Asp.net development, I have my config file as We.Config.

 Still the error comes. Please Halp me.

Mar 12, 2012 at 3:52 PM

@arungalant, can you please start a new thread.  It would be helpful to post your web.config, a code snippet as well as the error you are seeing.

Thanks,

--
Randy Levy
Enterprise Library support engineer
entlib.support@live.com