FLuent API configuration AND file based configuration

Topics: Data Access Application Block, Enterprise Library Core, Exception Handling Application Block, General discussion, Logging Application Block
Oct 1, 2012 at 2:05 AM

I have read through a few posts on this forum regarding the fluent api configuration and it's compatibility with file based ent lib configuration.  Apparently, if you utilize the fluent api to setup during runtime logging, exception handling, database connections the 'update' operation completely replaces any file based ent lib configuration.

While there appear to be a couple attempts to make these compatible, they do come with some limitations or complexities themselves.

That being said, is it possible to perform both fluent api configuration for ent lib AND file based configuration in an additive manner? (anyway?)

Our scenario.. we need to decrypt the connectionstring and pass it into any database operations with ent lib (i.e logging to the db).  I have been able to do this entirely with fluent api, but I do not want to limit my developers from using file based configuration in any manner for say exception management options.

Can you provide insight on the the limitations of using fluent api?  What are developers doing to work around the fact that any fluent api operation completely wipes away and file based configuration?

Oct 1, 2012 at 5:51 AM

> That being said, is it possible to perform both fluent api configuration for ent lib AND file based configuration in an additive manner? (anyway?)

The simple answer to your question is: YES.  I may have also been responsible for perpetuating the opposite viewpoint.  

It's a little bit of hoop-jumping but here is the code to merge the file-based configuration with the fluent interface:

    var builder = new ConfigurationSourceBuilder();

                .WithConnectionString(@"data source=.\SQLEXPRESS;Database=LoggingDefault;Integrated Security=SSPI")

    var configSource = new DictionaryConfigurationSource();

    IUnityContainer container = new UnityContainer();

    // Load the default configuration information from the config file
    // i.e. ConfigurationSourceFactory.Create()
    // and add it to the container
    container.AddExtension(new EnterpriseLibraryCoreExtension());

    // Create a configurator to use to configure our fluent configuration
    var configurator = new UnityContainerConfigurator(container);
    EnterpriseLibraryContainer.ConfigureContainer(configurator, configSource);
    // Use the configured container with file and fluent config as the Enterprise Library service locator
    EnterpriseLibraryContainer.Current = new UnityServiceLocator(container);

Then I can invoke logging (to a database trace listener) without having specified any database information in my configuration file as long as the Enterprise Library configured name of the database (in this case "LoggingDB") used in the fluent configuration matches what is specified in the logging configuration in the configuration file:

    var logger = EnterpriseLibraryContainer.Current.GetInstance<LogWriter>();
    logger.Write("Test", "General");

Randy Levy
Enterprise Library support engineer

Oct 1, 2012 at 4:29 PM

Thank you Randy, the above code worked nicely.  I was able to pass in the connection string to be used by the various handlers.

What about the scenario where I wanted to pass in a variable for the email to address in the email trace listener? (or say the writeLogStoredProcName for the database trace listener?).  

1.) Is it possible to send these in 1 at a time?

2.) If I used the Fluent API to do this, would it completely replace any logging configuration in my web or machine.config?




Oct 1, 2012 at 5:59 PM

Yes, configuration occurs at a configuration section level so it's not simple to specify specific values.  What you could do is to read in the configuration section from configuration (into a LoggingSettings object) and then set the appropriate values.  Then you could convert the ConfigurationSection to an IConfigurationSource and add it to the container.

For an example of this see this post: http://entlib.codeplex.com/discussions/394890#PostContent_912030.  You can use the SerializableConfigurationSource posted there to do the conversion.

Randy Levy
Enterprise Library support engineer

Oct 4, 2012 at 7:55 AM

FYI, I used this thread and the one linked above as a basis for a blog post

Randy Levy
Enterprise Library support engineer