Activation error occured while trying to get instance of type ExceptionManager

Topics: Enterprise Library Core, Exception Handling Application Block, Logging Application Block
Feb 9, 2012 at 8:05 AM

I have created a C# solution which contains

- NetTiers projects (Class Libraries)

- Main Class Library project

- Test UI Project.

The Main Class Library is called by the UI Test Project. All the DLLs share a config file which I specify using the following code.

Module_ManageMenus.Data.DataRepository.SetConfigurationFileExplicitly(in_strDLLFilePath + ".config");

This works fine.

Now, one of the Nettiers projects uses the following code:

ExceptionManager exManager = EnterpriseLibraryContainer.Current.GetInstance<ExceptionManager>();
bool rethrow = exManager.HandleException(e, policyName);
return rethrow;

The bold line throws the following exception:

{"Activation error occured while trying to get instance of type ExceptionManager, key \"\""}

Inner Exception:

{"Resolution of the dependency failed, type = \"Microsoft.Practices.EnterpriseLibrary.ExceptionHandling.ExceptionManager\", name = \"(none)\".\r\nException occurred while: while resolving.\r\nException is: InvalidOperationException - The type ExceptionManager cannot be constructed. You must configure the container to supply this value.\r\n-----------------------------------------------\r\nAt the time of the exception, the container was:\r\n\r\n  Resolving Microsoft.Practices.EnterpriseLibrary.ExceptionHandling.ExceptionManager,(none)\r\n"}

The NetTiers DLL is referencing the following:

Microsoft.Practices.EnterpriseLIbrary.Caching
Microsoft.Practices.EnterpriseLIbrary.Caching.Cryptography
Microsoft.Practices.EnterpriseLIbrary.Common
Microsoft.Practices.EnterpriseLIbrary.ExceptionHandling
Microsoft.Practices.EnterpriseLIbrary.ExceptionHandling.Logging
Microsoft.Practices.EnterpriseLIbrary.Logging
Microsoft.Practices.EnterpriseLIbrary.Logging.Database
Microsoft.Practices.EnterpriseLIbrary.Security
Microsoft.Practices.EnterpriseLIbrary.Security.Cache.CachingStore.dll
Microsoft.Practices.EnterpriseLIbrary.Security.Cryptography

 

My App.config file looks like this:

<?xml version="1.0"?>
<!-- .NET application configuration file - this file must have the exact same name as your application with .config appended to it. -->
<configuration>
  <!-- Register a section handler for the log4net section -->
  <configSections>
    <section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler,log4net"/>

    <!--Entlib start-->
    <section name="securityConfiguration" type="Microsoft.Practices.EnterpriseLibrary.Security.Configuration.SecuritySettings, Microsoft.Practices.EnterpriseLibrary.Security, Version=5.0.414.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" />
    <section name="loggingConfiguration" type="Microsoft.Practices.EnterpriseLibrary.Logging.Configuration.LoggingSettings, Microsoft.Practices.EnterpriseLibrary.Logging, Version=5.0.414.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" />
    <section name="exceptionHandling" type="Microsoft.Practices.EnterpriseLibrary.ExceptionHandling.Configuration.ExceptionHandlingSettings, Microsoft.Practices.EnterpriseLibrary.ExceptionHandling, Version=5.0.414.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" />
    <section name="dataConfiguration" type="Microsoft.Practices.EnterpriseLibrary.Data.Configuration.DatabaseSettings, Microsoft.Practices.EnterpriseLibrary.Data, Version=5.0.414.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" />
    <section name="cachingConfiguration" type="Microsoft.Practices.EnterpriseLibrary.Caching.Configuration.CacheManagerSettings, Microsoft.Practices.EnterpriseLibrary.Caching, Version=5.0.414.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" />
    <!--Entlib End-->

    <!-- Nettiers start-->
    <!--<section name="exceptionHandling" type="Microsoft.Practices.EnterpriseLibrary.ExceptionHandling.Configuration.ExceptionHandlingSettings, Microsoft.Practices.EnterpriseLibrary.ExceptionHandling" />-->

    <section name="Module_ManageMenus.Data"
      type="Module_ManageMenus.Data.Bases.NetTiersServiceSection, Module_ManageMenus.Data"
      allowDefinition="MachineToApplication"
      restartOnExternalChanges="true" />

    <enterpriseLibrary.ConfigurationSource selectedSource="File Configuration Source">
      <sources>
        <add name="File Configuration Source" type="Microsoft.Practices.EnterpriseLibrary.Common.Configuration.FileConfigurationSource, Microsoft.Practices.EnterpriseLibrary.Common, Version=5.0.414.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" filePath="C:\SVN\OKS\Development\Code\C#\Module_ManageMenus\Module_ManageMenus.DLL\entlib.config"/>
      </sources>
    </enterpriseLibrary.ConfigurationSource>

    <!-- Nettiers end-->
  </configSections>

  <!-- Nettiers start-->
  <connectionStrings>
    <add name="netTiersConnectionString" connectionString="Data Source=OKSLPT-SUES;Initial Catalog=Workflow; Integrated Security=True" />
    <add name="Module_ManageMenus.DLL.Properties.Settings.WorkflowConnectionString"
      connectionString="Data Source=OKSLPT-SUES;Initial Catalog=Workflow;Integrated Security=True"
      providerName="System.Data.SqlClient" />
  </connectionStrings>

  <Module_ManageMenus.Data defaultProvider="SqlNetTiersProvider">
    <providers>

      <add
    name="SqlNetTiersProvider"
    type="Module_ManageMenus.Data.SqlClient.SqlNetTiersProvider, Module_ManageMenus.Data.SqlClient"
    connectionStringName="netTiersConnectionString"
    providerInvariantName="System.Data.SqlClient"
    entityFactoryType="Module_ManageMenus.Entities.EntityFactory"
    useEntityFactory="true"
    enableEntityTracking="false"
    enableMethodAuthorization="false"
    useStoredProcedure="false"
  />

    </providers>
  </Module_ManageMenus.Data>
  <!-- Nettiers end -->

  <!--Entlib start-->
  <securityConfiguration defaultAuthorizationInstance="NetTiers.RuleProvider"
      defaultSecurityCacheInstance="">
    <authorizationProviders>
      <add type="Microsoft.Practices.EnterpriseLibrary.Security.AuthorizationRuleProvider, Microsoft.Practices.EnterpriseLibrary.Security, Version=5.0.414.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
              name="NetTiers.RuleProvider" />
    </authorizationProviders>
    <securityCacheProviders>
      <add cacheManagerInstanceName="Module_ManageMenus.Entities.EntityCache"
              defaultSlidingSessionExpirationInMinutes="10" defaultAbsoluteSessionExpirationInMinutes="60"
              type="Microsoft.Practices.EnterpriseLibrary.Security.Cache.CachingStore.CachingStoreProvider, Microsoft.Practices.EnterpriseLibrary.Security.Cache.CachingStore, Version=5.0.414.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
              name="Caching Store Provider" />
    </securityCacheProviders>
  </securityConfiguration>
  <loggingConfiguration name="Logging Application Block" tracingEnabled="true"
      defaultCategory="General" logWarningsWhenNoCategoriesMatch="true">
    <listeners>
      <add fileName="Module_ManageMenus.log.exclude" header="----------------------------------------"
              footer="----------------------------------------" formatter=""
              listenerDataType="Microsoft.Practices.EnterpriseLibrary.Logging.Configuration.FlatFileTraceListenerData, Microsoft.Practices.EnterpriseLibrary.Logging, Version=5.0.414.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
              traceOutputOptions="None" type="Microsoft.Practices.EnterpriseLibrary.Logging.TraceListeners.FlatFileTraceListener, Microsoft.Practices.EnterpriseLibrary.Logging, Version=5.0.414.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
              name="FlatFile TraceListener" />
    </listeners>
    <formatters>
      <add template="Timestamp: {timestamp}&#xD;&#xA;Message: {message}&#xD;&#xA;Category: {category}&#xD;&#xA;Priority: {priority}&#xD;&#xA;EventId: {eventid}&#xD;&#xA;Severity: {severity}&#xD;&#xA;Title:{title}&#xD;&#xA;Machine: {machine}&#xD;&#xA;Application Domain: {appDomain}&#xD;&#xA;Process Id: {processId}&#xD;&#xA;Process Name: {processName}&#xD;&#xA;Win32 Thread Id: {win32ThreadId}&#xD;&#xA;Thread Name: {threadName}&#xD;&#xA;Extended Properties: {dictionary({key} - {value}&#xD;&#xA;)}"
              type="Microsoft.Practices.EnterpriseLibrary.Logging.Formatters.TextFormatter, Microsoft.Practices.EnterpriseLibrary.Logging, Version=5.0.414.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
              name="Text Formatter" />
    </formatters>
    <categorySources>
      <add switchValue="Error" name="Exceptions">
        <listeners>
          <add name="FlatFile TraceListener" />
        </listeners>
      </add>
      <add switchValue="All" name="General">
        <listeners>
          <add name="FlatFile TraceListener" />
        </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="FlatFile TraceListener" />
        </listeners>
      </errors>
    </specialSources>
  </loggingConfiguration>
  <exceptionHandling>
    <exceptionPolicies>
      <add name="ServiceLayerExceptionPolicy">
        <exceptionTypes>
          <add type="System.Exception, mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"
                      postHandlingAction="NotifyRethrow" name="Exception">
            <exceptionHandlers>
              <add logCategory="Exceptions" eventId="100" severity="Error"
                              title="Module_ManageMenus Exception Handling" formatterType="Microsoft.Practices.EnterpriseLibrary.ExceptionHandling.XmlExceptionFormatter, Microsoft.Practices.EnterpriseLibrary.ExceptionHandling, Version=5.0.414.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
                              priority="0" type="Microsoft.Practices.EnterpriseLibrary.ExceptionHandling.Logging.LoggingExceptionHandler, Microsoft.Practices.EnterpriseLibrary.ExceptionHandling.Logging, Version=5.0.414.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
                              name="Logging Handler" />
            </exceptionHandlers>
          </add>
        </exceptionTypes>
      </add>
    </exceptionPolicies>
  </exceptionHandling>
  <cachingConfiguration defaultCacheManager="Module_ManageMenus.Entities.EntityCache">
    <cacheManagers>
      <add expirationPollFrequencyInSeconds="60" maximumElementsInCacheBeforeScavenging="1000"
              numberToRemoveWhenScavenging="10" backingStoreName="Null Storage"
              name="Module_ManageMenus.Entities.EntityCache" type="Microsoft.Practices.EnterpriseLibrary.Caching.CacheManager, Microsoft.Practices.EnterpriseLibrary.Caching, Version=5.0.414.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" />
    </cacheManagers>
    <backingStores>
      <add encryptionProviderName="" type="Microsoft.Practices.EnterpriseLibrary.Caching.BackingStoreImplementations.NullBackingStore, Microsoft.Practices.EnterpriseLibrary.Caching, Version=5.0.414.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
              name="Null Storage" />
    </backingStores>
  </cachingConfiguration>
  <!--Entlib End-->

  <appSettings>
    <!-- To enable internal log4net logging specify the following appSettings key -->
    <add key="log4net.Internal.Debug" value="true"/>
    <add key="ClientSettingsProvider.ServiceUri" value=""/>
  </appSettings>
  <system.diagnostics>
    <trace autoflush="true">
      <listeners>
        <add name="textWriterTraceListener" type="System.Diagnostics.TextWriterTraceListener" initializeData="TraceListener.txt"/>
      </listeners>
    </trace>
  </system.diagnostics>
 
  <system.web>
    <membership defaultProvider="ClientAuthenticationMembershipProvider">
      <providers>
        <add name="ClientAuthenticationMembershipProvider" type="System.Web.ClientServices.Providers.ClientFormsAuthenticationMembershipProvider, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" serviceUri=""/>
      </providers>
    </membership>
    <roleManager defaultProvider="ClientRoleProvider" enabled="true">
      <providers>
        <add name="ClientRoleProvider" type="System.Web.ClientServices.Providers.ClientRoleProvider, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" serviceUri="" cacheTimeout="86400"/>
      </providers>
    </roleManager>
  </system.web>
  <startup>
    <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.0"/>
  </startup>
</configuration>

 

I have no idea why this is happening. It works fine in a UI App, but not in a DLL.

Please can you try to assist me. I've already spent 3 days trying to find a solution.

Many thanks,
Sue

Feb 10, 2012 at 6:02 AM

The error message you are seeing happens when there is a configuration problem.  Your configuration file looks good.  It sounds like the configuration file is being loaded when running the UI app but is not being loaded in the other scenario.  I would double check that configuration file is being loaded and that the exact file that is being loaded has the same configuration as was posted above.

I'm not 100% sure about what you mean when you say "works fine in a UI App, but not in a DLL"?  Can you be more specific about what .exe is running and in what situations ExceptionManager can be resolved and in what situations resolution fails?  Can ExceptionManager  be resolved in the UI App but not in the NetTiers assembly when running in the same AppDomain?  When the exception occurs "who" was calling the DLL?  What is the process that is loading the DLL (since it sounds like it's not the UI App)? etc.

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

Feb 10, 2012 at 6:19 AM

Hi Randy,

Thank you so much for responding.

Firstly, let me clarify the "works fine in a UI App" issue.

I have written 2 applications (solutions), that are identical in every way EXCEPT the one has a class library (Solution B) as it's Entry Point, the other a Windows Form (Solution A)

Solution A contains:

  • NetTiers generated projects that compile to DLLs
  • A Class Library project
  • A Windows Form project that tests the Class Library project.

Solution B contains

  • NetTiers generated projects that compile to DLLs
  • A Windows Forms project

The code below, sits in one of the cs files in one of the NetTiers DLL Projects

ExceptionManager exManager = EnterpriseLibraryContainer.Current.GetInstance<ExceptionManager>();
bool rethrow = exManager.HandleException(e, policyName);
return rethrow;

This code works fine in Solution A. However, it throws the error (as mentioned in my previous post) when executed in Solution B.

I have not answered your remaining questions as I'm not sure whether they still pertain after my explanation above. Please advise whether I still need to answer them.

Many thanks,

Sue

Feb 10, 2012 at 7:10 AM

In solution B the Windows Forms project should have an app.config file that contains the configuration you previously posted.  If not, then the configuration would have to be manually loaded.

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

Feb 10, 2012 at 7:50 AM

That worked perfectly.

Thank you so much for you assistance.
Sue