Exception Handling - Ent Lib 5.0

Topics: Exception Handling Application Block
Mar 21, 2014 at 9:50 PM
I am new to the enterprise library, trying to add it to my visual studio 2008, windows form application. I went through the 5.0 lab and everything seemed to work as it should, according to the documentation.

I now just tried to take the same steps from the hands on lab and implement it on my project and I am not seeing the same results.
<?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.505.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" requirePermission="true" />
        <section name="exceptionHandling" type="Microsoft.Practices.EnterpriseLibrary.ExceptionHandling.Configuration.ExceptionHandlingSettings, Microsoft.Practices.EnterpriseLibrary.ExceptionHandling, Version=5.0.505.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" requirePermission="true" />
        <sectionGroup name="applicationSettings" type="System.Configuration.ApplicationSettingsGroup, System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" >
            <section name="Stewarts.TenDayRec.Windows.My.MySettings" type="System.Configuration.ClientSettingsSection, System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />
        </sectionGroup>
    </configSections>
    <loggingConfiguration name="" tracingEnabled="true" defaultCategory="General">
        <listeners>
            <add name="Event Log Listener" type="Microsoft.Practices.EnterpriseLibrary.Logging.TraceListeners.FormattedEventLogTraceListener, Microsoft.Practices.EnterpriseLibrary.Logging, Version=5.0.505.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
                listenerDataType="Microsoft.Practices.EnterpriseLibrary.Logging.Configuration.FormattedEventLogTraceListenerData, Microsoft.Practices.EnterpriseLibrary.Logging, Version=5.0.505.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
                source="Enterprise Library Logging" formatter="Text Formatter"
                log="" machineName="." traceOutputOptions="None" />
        </listeners>
        <formatters>
            <add type="Microsoft.Practices.EnterpriseLibrary.Logging.Formatters.TextFormatter, Microsoft.Practices.EnterpriseLibrary.Logging, Version=5.0.505.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="General">
                <listeners>
                    <add name="Event Log Listener" />
                </listeners>
            </add>
        </categorySources>
        <specialSources>
            <allEvents switchValue="All" name="All Events" />
            <notProcessed switchValue="All" name="Unprocessed Category" />
            <errors switchValue="All" name="Logging Errors &amp; Warnings">
                <listeners>
                    <add name="Event Log Listener" />
                </listeners>
            </errors>
        </specialSources>
    </loggingConfiguration>
    <exceptionHandling>
        <exceptionPolicies>
            <add name="UI Policy">
                <exceptionTypes>
                    <add name="All Exceptions" type="System.Exception, mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"
                        postHandlingAction="None">
                        <exceptionHandlers>
                            <add name="Logging Exception Handler" type="Microsoft.Practices.EnterpriseLibrary.ExceptionHandling.Logging.LoggingExceptionHandler, Microsoft.Practices.EnterpriseLibrary.ExceptionHandling.Logging, Version=5.0.505.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
                                logCategory="General" 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>
    <system.diagnostics>
        <sources>
            <source name="DefaultSource" switchName="DefaultSwitch">
                <listeners>
                    <add name="FileLog"/>
                    <!--<add name="EventLog"/>-->
                </listeners>
            </source>
        </sources>
        <switches>
            <add name="DefaultSwitch" value="Information" />
        </switches>
        <sharedListeners>
            <add name="FileLog"
                 type="Microsoft.VisualBasic.Logging.FileLogTraceListener, Microsoft.VisualBasic, Version=8.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL"
                 initializeData="FileLogWriter"/>
            <!--<add name="EventLog" type="System.Diagnostics.EventLogTraceListener" initializeData="APPLICATION_NAME"/> -->
        </sharedListeners>
    </system.diagnostics>
    <applicationSettings>
        <Stewarts.TenDayRec.Windows.My.MySettings>
            <setting name="DataPath" serializeAs="String">
                <value />
            </setting>
            <setting name="DebugDataPath" serializeAs="String">
                <value>c:\xml\101.xml</value>
            </setting>
            <setting name="ExportPath" serializeAs="String">
                <value>tendayexport.xml</value>
            </setting>
            <setting name="ReportPath" serializeAs="String">
                <value>Reports\TenDayReport.rpt</value>
            </setting>
        </Stewarts.TenDayRec.Windows.My.MySettings>
    </applicationSettings>
</configuration>
Here is where my error is being thrown:
        If Not File.Exists(sFileName) Then
            Throw New ApplicationException("Data file cannot be found")
        End If
Here is where I handle the error:
Imports System
Imports System.Windows.Forms
Imports System.Threading
Imports Microsoft.Practices.EnterpriseLibrary.ExceptionHandling
Imports Microsoft.Practices.EnterpriseLibrary.Common.Configuration

Shared Sub Main()
        AddHandler Application.ThreadException, AddressOf Application_ThreadException
        AddHandler AppDomain.CurrentDomain.UnhandledException, AddressOf CurrentDomain_UnhandledException

        Application.Run(New MainForm())
End Sub



Private Shared Sub HandleException(ByVal ex As Exception, ByVal policy As String)

        Dim rethrow As Boolean = False

        Dim exManager As ExceptionManager = EnterpriseLibraryContainer.Current.GetInstance(Of ExceptionManager)()

        Try
            rethrow = exManager.HandleException(ex, policy)

        Catch innerEx As Exception
            Dim errorMessage As String

            errorMessage = String.Format("An unexpected exception occured " & _
                                         "while calling HandleException with policy '{0}. {1}{2}'", _
                                         policy, Environment.NewLine, innerEx.ToString())

            MessageBox.Show(errorMessage, "Application Error", _
                            MessageBoxButtons.OK, MessageBoxIcon.Stop)

            Throw ex

        End Try

        If rethrow Then
            Throw ex
        Else
            MessageBox.Show("An unhandled exception occured and has been logged.  If this issue continues, please contact Tech Center")
        End If

    End Sub

    Private Shared Sub Application_ThreadException(ByVal sender As Object, ByVal e As ThreadExceptionEventArgs)
        HandleException(e.Exception, "UI Policy")
End Sub
What I don't understand is - I see the error being thrown, I go into the HandleException procedure, I get to this line where I get an exceptionManager, I don't go any further - my application just loads. When I step through this with the debugger, I look at the EnterpriseLibraryContainer.Current object and I see the following error:
{"An error occurred creating the configuration section handler for exceptionHandling:
Could not load file or assembly 'Microsoft.Practices.EnterpriseLibrary.ExceptionHandling,
Version=5.0.414.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35' or one of its dependencies.
The located assembly's manifest definition does not match the assembly reference.
(Exception from HRESULT: 0x80131040) (C:\Users\stephen.bell\Documents\Visual Studio 2008\Projects\
GasApplications\tendaywindows\Stewarts.TenDayRec\Stewarts.TenDayRec.Windows\bin\Debug\Stewarts.TenDayRec.Windows.exe.config line 5)"}
Microsoft.Practices.ServiceLocation.IServiceLocator
Any guidance would be greatly apprecaited.
Thanks
sb
Mar 23, 2014 at 6:41 AM
The message indicates a mismatch (probably version) between what the application is expecting and what was found.
It looks like the application is trying to load Enterprise Library 5.0.414.0 but it was not found. I would guess that version 5.0.505.0 was found. Check the references and make sure that correct version is being referenced. The configuration file is referencing version 5.0.505.0.

~~
Randy Levy
entlib.support@live.com
Enterprise Library support engineer
Support How-to
Mar 23, 2014 at 1:27 PM
Thank you for the reply - that is what I thought, but when I look at my references, I see this:

Image

The error I am receiving is specific to the exceptionhandling.dll, correct?

Thanks again!
Mar 23, 2014 at 3:36 PM
Yes, the references look good. The message occurs because at runtime the configuration section fails deserialization. Could you have an old configuration file that references 5.0.414.0 deployed?

I can recreate the error message if I reference version 5.0.505.0 (as you've done) but change my configuration file to reference version 5.0.414.0.

~~
Randy Levy
entlib.support@live.com
Enterprise Library support engineer
Support How-to
Sep 3, 2014 at 6:44 PM
I get an exception saying

" An error occurred creating the configuration section handler for exceptionHandling: Could not load file or assembly 'Microsoft.Practices.EnterpriseLibrary.ExceptionHandling, Version=5.0.414.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a' or one of its dependencies. The located assembly's manifest definition does not match the assembly reference."

I checked the version of added references but they remain to be 5.0.414.0 same as that of in the app.config file. Being new to enterprise library am I missing anything?Timely help would be appreciated. Also I tried referencing the ExceptionHandling.dll from the bin folder and from library too.Nothing helped me.Currently I am referencing from Bin of my solution.
This is my content of my App.config file.

<configuration>
<configSections>
<section name="exceptionHandling" type="Microsoft.Practices.EnterpriseLibrary.ExceptionHandling.Configuration.ExceptionHandlingSettings, Microsoft.Practices.EnterpriseLibrary.ExceptionHandling, Version=5.0.414.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" />
</configSections>
<exceptionHandling>
<exceptionPolicies>
  <add name="UI is empty Policy">
    <exceptionTypes>
      <add type="System.Exception, mscorlib, Version=5.0.414.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"
        postHandlingAction="NotifyRethrow" name="Exception" />
    </exceptionTypes>
  </add>
</exceptionPolicies>
</exceptionHandling>
</configuration>
This is code is in button handler.
try
        {
          throw new Exception("test exception");
        }
        catch (Exception ex)
        {
          bool rethrow = ExceptionPolicy.HandleException(ex, "UI is empty Policy");
          if(rethrow)
        {
        throw;
        }
        }
Sep 4, 2014 at 7:58 AM
The message indicates that the .NET runtime is finding another "version" (not just version number, could be other assembly manifest information) of the assembly. It may be in the GAC or perhaps you have some unsigned versions of the assemblies.

Fuslogvw.exe (Assembly Binding Log Viewer) should give you the exact details about the assembly causing the issue -- assembly information and where it is located.

~~
Randy Levy
entlib.support@live.com
Enterprise Library support engineer
Support How-to