EntLib 2.0 "item with the same key" Error

Topics: Enterprise Library Core, Logging Application Block
Jul 11, 2007 at 2:45 PM
Hi all:

We're using EntLib v2.0 in our BizTalk environment for logging purposes. Lately, we have been getting a lot of XLANG/s errors due to EntLib exceptions. Would you give us some pointers to resolve this issue please? Here is the error in detail:

Event Type: Error
Event Source: XLANG/s
Event Category: None
Event ID: 10034
Date: 7/11/2007
Time: 8:39:40 AM
User: N/A
Computer: BTServer01
Uncaught exception (see the 'inner exception' below) has suspended an instance of service 'Integration.orchDB(949982fa-9d1f-3c7a-1137-613d61c06eb9)'.
The service instance will remain suspended until administratively resumed or terminated.
If resumed the instance will continue from its last persisted state and may re-throw the same unexpected exception.
InstanceId: ed2706e5-2338-459a-b4c1-e0df62539096
Shape name: Login Exception
ShapeId: 21b53925-8e77-4158-a10a-c2db8f8a943e
Exception thrown from: segment 5, progress 5
Inner exception: The type initializer for 'Microsoft.Practices.EnterpriseLibrary.Logging.Logger' threw an exception.

Exception type: TypeInitializationException
Source: Microsoft.Practices.EnterpriseLibrary.Logging
Target Site: Void Write(Microsoft.Practices.EnterpriseLibrary.Logging.LogEntry)
The following is a stack trace that identifies the location where the exception occured

at Microsoft.Practices.EnterpriseLibrary.Logging.Logger.Write(LogEntry log)
at Microsoft.Practices.EnterpriseLibrary.Utilities.EntLibHelper.LogErrorMessageInternal(String message, String title, Guid handlingInstanceId, IDictionary properties)
at Microsoft.Practices.EnterpriseLibrary.Utilities.EntLibHelper.LogErrorMessage(String message, String appName)
at Integration.orchDB.segment5(StopConditions stopOn)
at Microsoft.XLANGs.Core.SegmentScheduler.RunASegment(Segment s, StopConditions stopCond, Exception& exp)
Additional error information:

An item with the same key has already been added.

Exception type: ArgumentException
Source: mscorlib
Target Site: Void ThrowArgumentException(System.ExceptionResource)
The following is a stack trace that identifies the location where the exception occured

at System.ThrowHelper.ThrowArgumentException(ExceptionResource resource)
at System.Collections.Generic.Dictionary`2.Insert(TKey key, TValue value, Boolean add)
at Microsoft.Practices.EnterpriseLibrary.SqlConfigurationSource.SqlConfigurationSource.EnsureImplementation(SqlConfigurationData data)
at Microsoft.Practices.EnterpriseLibrary.SqlConfigurationSource.SqlConfigurationSource..ctor(String connectString, String getStoredProc, String setStoredProc, String refreshStoredProc, String removeStoredProc)
at Microsoft.Practices.EnterpriseLibrary.SqlConfigurationSource.SqlConfigurationSourceElement.CreateSource()
at Microsoft.Practices.EnterpriseLibrary.Common.Configuration.ConfigurationSourceFactory.Create(String name)
at Microsoft.Practices.EnterpriseLibrary.Common.Configuration.ConfigurationSourceFactory.Create()
at Microsoft.Practices.EnterpriseLibrary.Logging.Logger..cctor()

For more information, see Help and Support Center at http://go.microsoft.com/fwlink/events.asp.
Jul 11, 2007 at 4:33 PM

There seems to be a thread safety issue on the implementation of the SqlCconfigurationSource. You can fix it by adding a second check inside the lock statement like this:

private static void EnsureImplementation(SqlConfigurationData data)
if (!implementationByConnectionString.ContainsKey(data.ConnectionString))
lock (lockObject)
if (!implementationByConnectionString.ContainsKey(data.ConnectionString))
SqlConfigurationSourceImplementation implementation = new SqlConfigurationSourceImplementation(data);
implementationByConnectionString.Add(data.ConnectionString, implementation);

Keep in mind this configuration source is packaged as a QuickStart, so using it in a production environment might not be appropriate.