Intermittent NullReferenceException in LoggingBlockExtension.Initialize()

Topics: Enterprise Library Core, Logging Application Block
Sep 25, 2009 at 9:27 AM

Intermittenly getting this exception, details below.   Any ideas on what may be causing it?  Also, in my web.config file I have a database trace listener, email trace listener, and eventlog trace listener.  the email trace listener is configured to use a custom class I have written that derives from FormattedTraceListenerBase.  the custom email trace listener is in a strongly typed assembly that lives in the GAC.  It is working 99% of the time, but once in a while this occurs...

The UnityContainer only initializes when the Web Application starts up.

Any help will be appreciated.

I configured my application (web application), to use unity and through unity configured the logging applicaiton block. I am using Asp.Net 3.5, Enterprise Library 4.1

 

- Jon


 

Type : System.NullReferenceException, mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089
Message : Object reference not set to an instance of an object.
Source : mscorlib
Help link :
Data : System.Collections.ListDictionaryInternal
TargetSite : Void Insert(TKey, TValue, Boolean)
Stack Trace :    at System.Collections.Generic.Dictionary`2.Insert(TKey key, TValue value, Boolean add)
   at System.Collections.Generic.Dictionary`2.set_Item(TKey key, TValue value)
   at Microsoft.Practices.Unity.NamedTypesRegistry.RegisterType(Type t, String name)
   at Microsoft.Practices.Unity.UnityDefaultBehaviorExtension.OnRegister(Object sender, RegisterEventArgs e)
   at System.EventHandler`1.Invoke(Object sender, TEventArgs e)
   at Microsoft.Practices.Unity.UnityContainer.RegisterType(Type from, Type to, String name, LifetimeManager lifetimeManager, InjectionMember[] injectionMembers)
   at Microsoft.Practices.Unity.UnityContainerBase.RegisterType(Type t, String name, LifetimeManager lifetimeManager, InjectionMember[] injectionMembers)
   at Microsoft.Practices.EnterpriseLibrary.Logging.Configuration.Unity.LoggingBlockExtension.CreateTraceListenersAdditionalPolicies(IPolicyList policyList, IUnityContainer container, TraceListenerDataCollection traceListenerDataCollection)
   at Microsoft.Practices.EnterpriseLibrary.Logging.Configuration.Unity.LoggingBlockExtension.Initialize()
   at Microsoft.Practices.Unity.UnityContainer.AddExtension(UnityContainerExtension extension)
   at MyFactory.Initialize(IConfigurationSource configurationSource)
   at MyFactory..Initialize()
   at MyFactory.EnsureInitialized()
   at MyFactory.CreateDatabase(String databaseName)


    public static class MyFactory
    {

        public static UnityContainer UnityContainer { get; private set; }
        private static void Clear()
        {
            if (UnityContainer != null)
                UnityContainer = null;
        }
        public static bool Initialized { get; private set; }
        private static void EnsureInitialized()
        {
            if (!Initialized)
                Initialize();
        }
        public static void Initialize()
        {

            Initialize(null);
        }
        public static void Initialize(IConfigurationSource configurationSource)
        {

            if (Initialized)
                Clear();

            if (UnityContainer == null)
                UnityContainer = new UnityContainer();

            UnityContainer.AddExtension(configurationSource == null ? new EnterpriseLibraryCoreExtension() : new EnterpriseLibraryCoreExtension(configurationSource));
            UnityContainer.AddExtension(new DataAccessBlockExtension());
            UnityContainer.AddExtension(new LoggingBlockExtension());
            UnityContainer.AddExtension(new ExceptionHandlingBlockExtension());
            UnityContainer.AddExtension(new CachingBlockExtension());

            Initialized = true;
        }

        public static Database CreateDatabase()
        {
            EnsureInitialized();
            Database database = UnityContainer.Resolve<Database>();
            if (database == null)
                throw new NullReferenceException("EnterpriseLibraryFactory could not create the Database object");
            return database;
       
  }
 } 

 

Sep 28, 2009 at 7:23 AM

Hi,

I tried using using the code snippet you provided, im not hitting your error. can you provide a quick repro project?

Valiant Dudan
Global Technology and Solutions
Avanade, Inc.
entlib.support@avanade.com