Configuring Unity From FileConfigurationSource

Topics: Enterprise Library Core
Feb 27, 2009 at 12:04 AM
Using Enterprise library 4.1.

I have an app.config defined in AProject.

I have a test project, AProjectTest, which deploys AProject.app.config to a folder 'MYFOLDER' during the test:
So it is deployed to:
C:\Dev\Solution\~\username_E29388 2009-02-27 10_49_56\Out\MYFOLDER\AppConfig

I then create a file configuration source:
IConfigurationSource configSource = new FileConfigurationSource( "path" );
UnityConfigurationSection unityConfigurationSection = (UnityConfigurationSection)configurationSource.GetSection("unity");

var unityContainer = new UnityContainer();
unityConfigurationSection.Containers.Default.Configure(unityContainer);

// Then when I try and resolve a type configured in the config, it throws an exception: var logWriter = unityContainer.Resolve<LogWriter>();

This works if I add app.config to the root in my AProjectTest, and read in that file using the same technique above. Of course, it gets renamed to MyTestProject.dll.config.

Here is the stack trace:
Stack trace: Failed    CanConnectToFullAuthAndExecuteQuery1Test    TarongEnergy.Trading.Services.DataBroker.Tests    Class Initialization method TarongEnergy.Trading.Services.DataBroker.Data.Tests.MarketMessagesDataAdapterTest.ClassInitialize threw exception. Microsoft.Practices.Unity.ResolutionFailedException:  Microsoft.Practices.Unity.ResolutionFailedException: Resolution of the dependency failed, type = "Microsoft.Practices.EnterpriseLibrary.Logging.LogWriter", name = "". Exception message is: The current build operation (build key Build Key[Microsoft.Practices.EnterpriseLibrary.Logging.LogWriter, null]) failed: The parameter filters could not be resolved when attempting to call constructor Microsoft.Practices.EnterpriseLibrary.Logging.LogWriter(System.Collections.Generic.ICollection`1[[Microsoft.Practices.EnterpriseLibrary.Logging.Filters.ILogFilter, Microsoft.Practices.EnterpriseLibrary.Logging, Version=4.1.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35]] filters, System.Collections.Generic.IDictionary`2[[System.String, mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089],[Microsoft.Practices.EnterpriseLibrary.Logging.LogSource, Microsoft.Practices.EnterpriseLibrary.Logging, Version=4.1.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35]] traceSources, Microsoft.Practices.EnterpriseLibrary.Logging.LogSource allEventsTraceSource, Microsoft.Practices.EnterpriseLibrary.Logging.LogSource notProcessedTraceSource, Microsoft.Practices.EnterpriseLibrary.Logging.LogSource errorsTraceSource, System.String defaultCategory, System.Boolean tracingEnabled, System.Boolean logWarningsWhenNoCategoriesMatch, System.Boolean revertImpersonation). (Strategy type BuildPlanStrategy, index 3) --->  Microsoft.Practices.ObjectBuilder2.BuildFailedException: The current build operation (build key Build Key[Microsoft.Practices.EnterpriseLibrary.Logging.LogWriter, null]) failed: The parameter filters could not be resolved when attempting to call constructor Microsoft.Practices.EnterpriseLibrary.Logging.LogWriter(System.Collections.Generic.ICollection`1[[Microsoft.Practices.EnterpriseLibrary.Logging.Filters.ILogFilter, Microsoft.Practices.EnterpriseLibrary.Logging, Version=4.1.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35]] filters, System.Collections.Generic.IDictionary`2[[System.String, mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089],[Microsoft.Practices.EnterpriseLibrary.Logging.LogSource, Microsoft.Practices.EnterpriseLibrary.Logging, Version=4.1.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35]] traceSources, Microsoft.Practices.EnterpriseLibrary.Logging.LogSource allEventsTraceSource, Microsoft.Practices.EnterpriseLibrary.Logging.LogSource notProcessedTraceSource, Microsoft.Practices.EnterpriseLibrary.Logging.LogSource errorsTraceSource, System.String defaultCategory, System.Boolean tracingEnabled, System.Boolean logWarningsWhenNoCategoriesMatch, System.Boolean revertImpersonation). (Strategy type BuildPlanStrategy, index 3) --->  System.InvalidOperationException: The parameter filters could not be resolved when attempting to call constructor Microsoft.Practices.EnterpriseLibrary.Logging.LogWriter(System.Collections.Generic.ICollection`1[[Microsoft.Practices.EnterpriseLibrary.Logging.Filters.ILogFilter, Microsoft.Practices.EnterpriseLibrary.Logging, Version=4.1.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35]] filters, System.Collections.Generic.IDictionary`2[[System.String, mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089],[Microsoft.Practices.EnterpriseLibrary.Logging.LogSource, Microsoft.Practices.EnterpriseLibrary.Logging, Version=4.1.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35]] traceSources, Microsoft.Practices.EnterpriseLibrary.Logging.LogSource allEventsTraceSource, Microsoft.Practices.EnterpriseLibrary.Logging.LogSource notProcessedTraceSource, Microsoft.Practices.EnterpriseLibrary.Logging.LogSource errorsTraceSource, System.String defaultCategory, System.Boolean tracingEnabled, System.Boolean logWarningsWhenNoCategoriesMatch, System.Boolean revertImpersonation). --->  Microsoft.Practices.ObjectBuilder2.BuildFailedException: The current build operation (build key Build Key[System.Collections.Generic.ICollection`1[Microsoft.Practices.EnterpriseLibrary.Logging.Filters.ILogFilter], null]) failed: The current type, System.Collections.Generic.ICollection`1[Microsoft.Practices.EnterpriseLibrary.Logging.Filters.ILogFilter], is an interface and cannot be constructed. Are you missing a type mapping? (Strategy type BuildPlanStrategy, index 3) --->  System.InvalidOperationException: The current type, System.Collections.Generic.ICollection`1[Microsoft.Practices.EnterpriseLibrary.Logging.Filters.ILogFilter], is an interface and cannot be constructed. Are you missing a type mapping?.       



Feb 27, 2009 at 9:05 AM
Hi,

The unity was unable to resolve the LogWriter object because the LogWriter requires parameters that must also be resolved. Looking at the source code of the Logging Application Block, there are some parameters that must be resolved.

public

LogWriter(ICollection<ILogFilter> filters, IDictionary<string, LogSource> traceSources, LogSource errorsTraceSource, string defaultCategory)

You can post this question to the Unity Forum for the best answers. http://www.codeplex.com/unity/Thread/List.aspx

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

 

Feb 28, 2009 at 1:57 PM
Edited Feb 28, 2009 at 2:07 PM
Hi Valiant,

I picked this up from the Unity forum.  Not knowing anything about EnterpriseLibrary 4.1 (havn't been involved with it since SCSF) is loggingConfiguration not a plug'n play?   I reviewed the documentation and it suggest that the logFilters element is optional however, as you suggested, all of the LogWriter constructors have an ICollection<ILogFilter>().

Seems like there shouldn't be such a heavy dependency on the Logging Application Block for other EntLib 4.1 features, i.e, the FileConfigurationSource() method.  Unless we're barking up the wrong tree and are dealing with misconfigured logging?  Perhaps someone in this forum has experience with configuring the Logging Application Block for EnterpriseLibrary 4.1 that could assist?

IConfigurationSource configSource = new FileConfigurationSource( "path" );

---- excerpt from documentation follows:

Source Schema for the Logging Application Block

<configSections>
<section name="loggingConfiguration"
         type="Microsoft.Practices.EnterpriseLibrary.Logging.Configuration.LoggingSettings,
               Microsoft.Practices.EnterpriseLibrary.Logging, Version=4.1.0.0,
               Culture=neutral, PublicKeyToken=31bf3856ad364e35" />
</configSections>

 
The section-handler declaration contains the name of the configuration settings section and the name of the section-handler class that processes configuration data in that section. The name of the configuration settings section is loggingConfiguration. The name of the section-handler class is Microsoft.Practices.EnterpriseLibrary.Logging.Configuration.LoggingSettings.

...
...
...

logFilters Child Element
The logFilters element is a child element of the loggingConfiguration element. It lists the filters that process the log entry before it is routed to the trace sources. This element is optional.

Feb 28, 2009 at 3:38 PM
Hi KierenH,

EntLib 4.1 is pretty impressive (w/Unity very practical to use).  I see a number of blocks that will be of great value, saving me tons of programming time, so I'm starting to study it.  

If you'll go to the "Quick Starts" folder you'll find a LoggingQuickStart; it's App.Config should provide you all the information you'll require to properly setup the loggingConfiguration for EnterpriseLibrary; I trust after you setup the logFilter element you'll get past your error.

Bill