How to read redirected connection string

Topics: Building and extending application blocks, Enterprise Library Core
Mar 29, 2011 at 11:13 AM

I am trying to share a connection string among different services and different data access technology.

Since the configSource attribute on connection string doesn't support path to a shared location, I want to use EL5.0 's configSource to redirect connection string from a file like "..\CommonConfig\connectionString.common.config".

I have modified the EL5.0 source code to support this relative path and tested with the validation block to share a redirected ruleset.

The problem is how to read the redirected connection string programmatic in my services, and pass it to my EF4.0 and SqlDependency

I read the EL5.0 source code related to file configuration source, but it seems a bit too complicated for me without some description.

Mar 30, 2011 at 2:05 AM

As you might have noticed, you can't read it using ConfigurationManager.ConnectionStrings.  A way around it is to create an instance of IConfigurationSource out of the default config source and getting the ConnectionStringsSection from that configuration source.

IConfigurationSource configSource = ConfigurationSourceFactory.Create();
ConnectionStringsSection connSection = (ConnectionStringsSection)configSource.GetSection("connectionStrings");

Sarah Urmeneta
Global Technologies & Solutions
Avanade, Inc.

Mar 30, 2011 at 8:03 AM

Thanks! It worked like a magic!