Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8305597
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T18:08:52+00:00 2026-06-08T18:08:52+00:00

I am using the EntLib in an environment where database connection strings are retrieved

  • 0

I am using the EntLib in an environment where database connection strings are retrieved from a separate library call that decrypts a proprietary config file. I have no say over this practice or the format of the config file.

I want to do EntLib exception logging to the database in this setting. I therefore need to set up a EntLib database configuration instance with the name of the database, with the connection string. Since I can’t get the connection string until run time, but EntLib does allow run-time configuration, I use the following code, as described in this:

builder.ConfigureData()
               .ForDatabaseNamed("Ann")
                 .ThatIs.ASqlDatabase()
                 .WithConnectionString(connectionString)
                 .AsDefault();

The parameter connectionString is the one I’ve retrieved from the separate library.

The sample code goes on to merge the created configuration info with an empty DictionaryConfigurationSource. I, however, need to merge it with the rest of the configuration code from the app.config. So I do this:

        var configSource = new SystemConfigurationSource();
        builder.UpdateConfigurationWithReplace(configSource);
        EnterpriseLibraryContainer.Current
          = EnterpriseLibraryContainer.CreateDefaultContainer(configSource);

… which is based very closely on the sample code.

But: I get an internal error in Microsoft.Practices.EnterpriseLibrary.Common.Configuration.SystemConfigurationSource.Save. The failing code is this:

        var fileMap = new ExeConfigurationFileMap { ExeConfigFilename = ConfigurationFilePath };
        var config = ConfigurationManager.OpenMappedExeConfiguration(fileMap, ConfigurationUserLevel.None);

        config.Sections.Remove(section);
        config.Sections.Add(section, configurationSection);

        config.Save();

… where ‘section’ is “connectionStrings”. The code fails on the Add method call, saying that you can’t add a duplicate section. Inspection shows that the connectionStrings section is still there even after the Remove.

I know from experience that there’s always a default entry under connectionStrings when the configuration files are actually read and interpreted, inherited from the machine.config. So perhaps you can never really remove the connectionStrings section.

That would appear to leave me out of luck, though, unless I want to modify the EntLib source, which I do not.

I could perhaps build all the configuration information for the EntLib at run time, using the fluent API. But I’d rather not. The users want their Operations staff to be able to make small changes to the logging without having to involve a developer.

So my question, in several parts: is there a nice simple workaround for this? Does it require a change to the EntLib source? Or have I missed something really simple that would do away with the problem?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-08T18:08:53+00:00Added an answer on June 8, 2026 at 6:08 pm

    I found a workaround, thanks to this post. Rather than taking the system configuration source and attempting to update it from the builder, I copy the sections I set up in app.config into the builder, and then do an UpdateConfigurationWithReplace on an empty dummy configuration source object in order to create a ConfigurationSource that can be used to create the default container.

            var builder = new ConfigurationSourceBuilder();
            var configSource = new SystemConfigurationSource();
    
            CopyConfigSettings("loggingConfiguration", builder, configSource);
            CopyConfigSettings("exceptionHandling", builder, configSource);
    
            // Manually configure the database settings
            builder.ConfigureData()
                   .ForDatabaseNamed("Ann")
                     .ThatIs.ASqlDatabase()
                     .WithConnectionString(connectionString)
                     .AsDefault();
    
            // Update a dummy, empty ConfigSource object with the settings we have built up.
            // Remember, this is a config settings object for the EntLib, not for the entire program.  
            // So it doesn't need all 24 sections or however many you can set in the app.config.  
            DictionaryConfigurationSource dummySource = new DictionaryConfigurationSource();
            builder.UpdateConfigurationWithReplace(dummySource);
    
            // Create the default container using our new ConfigurationSource object.  
            EnterpriseLibraryContainer.Current
              = EnterpriseLibraryContainer.CreateDefaultContainer(dummySource);
    

    The key is this subroutine:

        /// <summary>
        /// Copies a configuration section from the SystemConfigurationSource to the ConfigurationSourceBuilder.
        /// </summary>
        /// <param name="sectionName"></param>
        /// <param name="builder"></param>
        /// <param name="configSource"></param>
        private static void CopyConfigSettings(string sectionName, ConfigurationSourceBuilder builder, SystemConfigurationSource configSource)
        {
            ConfigurationSection section = configSource.GetSection(sectionName);
            builder.AddSection(sectionName, section);
        }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm using the Microsoft.Practices.EnterpriseLibrary.Data library to establish a database connection. I suspect that that
I'm trying to call a stored procedure from my MVC app using EntLib 5.0.
I am using the patterns & practices enterprise library (p&p EntLib) logging block to
I'm trying to call a sproc from EntLib (MVC3, EntLib5, SQL Server 2008). I'm
I am using EntLib 4.1. _db = DatabaseFactory.CreateDatabase(DbName); DbCommand dbCommand = _db.GetStoredProcCommand(someProcedure); _db.AddInParameter(dbCommand, Id,
There were 2 solutions suggested so far Environment variables using %username% in the filename
I'm using EntLib 5.0 for logging and exception handling, but not for data access.
Typical scenario, I am using EntLib 5.0-April 2010 Logging on my development machine and
I am building a logging DLL that will simplify EntLib 5 Logging Application Block.
My site is using enterprise library v 5.0. Mainly the DAAB. Some functions such

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.