How to load diffrent environmental configuration details programatically?

Topics: Building and extending application blocks, Data Access Application Block, Enterprise Library Core, General discussion, Logging Application Block
Nov 5, 2008 at 7:43 AM
Hi,

I have diffrent application environments: Development, Test, Staging, Production, etc
Currently, I am logging using Database Trace Listner programatically.
Now, I need to load environment specific details (e.g connection string for Development, Test, Staging, Production etc) programatically.
How/Where can i specify environment specific details in my web.config or anywhere else?

Anyone have sample example for above scanerio?

Thanks,
Kaushal
Nov 5, 2008 at 8:14 AM
Hi, I think this is what you need.  http://www.davidhayden.com/blog/dave/archive/2007/03/01/EnvironmentalOverridesEnterpriseLibrary3.aspx


Sarah Urmeneta
Global Technology & Solutions
Avanade, Inc.
sarah.b.urmeneta@avanade.com
Nov 5, 2008 at 11:25 AM

Hi Sarah,

I have already gone through this article.

That articles shows that how to configure Environmental Configuration using Enterprise Library Configuration Editor.

Suppose, i have diffrent environment e.g.
Development, Test, Staging, Production, etc

Now, I want to load diffrent connetion string as per environment programatically using coding as i did in Database Trace listener.

I dont want to make use of Enterprise Library Congiration Editor.

e.g. I have configuration element as below in my web.config file or app.config file.

 

<

DatabaseConnection>
<
DEV>
<
param name="connection" value="Server=MyTempDEVServer;Database=MyTempDEVDB;UID=devuser;PWD=devpwd;" />
</
DEV>

 

<

STAGE>
<param name="connection" value="Server=MyTempSTAGEServer;Database=MyTempSTAGEDB;UID=stageuser;PWD=stagepwd;" />

</
STAGE>

 

<

PROD>
<
param name="connection" value="Server=MyTempPRODServer;Database=MyTempPRODDB;UID=produser;PWD=prodpwd;" />
</
PROD>
</
DatabaseConnection>

 

 

Now, I want to load these connection string values depends on environment without make use of Enterprise Library Congiration Editor.

Thanks,
Kaushal.

Nov 6, 2008 at 5:48 AM
I'm not sure if this is really a question regarding ent lib since you don't want to use the EnvironmentalOverrides feature at all.  Anyway, I found this page, see if this helps.
Nov 6, 2008 at 7:35 AM
Edited Nov 7, 2008 at 7:18 AM
I've gone through article posted at 
http://www.davidhayden.com/blog/dave/archive/2007/03/01/EnvironmentalOverridesEnterpriseLibrary3.aspx

My main configuration file is trace.exe.config.
I made 3 configuration file STAGE.config, DEV.Config & PROD.config along with its' delta file.
For each, environment, i have different database configuration.

Now, I have certain question for this.
1. How can we set environment for our machine?
2. How my configuration file (here trace.exe.config) will load database configuration depends on environment?

I have created Database trace listener as below before make use of Environment:

 

TextFormatter formatter = new TextFormatter("Timestamp: {timestamp}{newline}" +
"Message: {message}{newline} " +
"Category: {category}{newline}" );  

IConfigurationSource source = new FileConfigurationSource(@"trace.exe.config");
SqlDatabase sqlDB = new SqlDatabase("Data Source=VSDBSERVER;Initial Catalog=MYDB;User ID=myusr;Password=***");
FormattedDatabaseTraceListener databaseTraceListener = new FormattedDatabaseTraceListener(sqlDB, "WriteLog", "AddCategory", formatter);

Here, i have explicitly defined connection string in above example.

Now, I want to load connection string based on environment setting. How will i load connection string defined in diffrent congif file(DEV/STAGE/PROD.config) for diffrent environment ?

It would be great if anyone guide me for this.

Thanks,
Kaushal

 

 

 

Nov 6, 2008 at 7:59 AM
Sorry, forgot to include the link. http://www.hanselman.com/blog/ManagingMultipleConfigurationFileEnvironmentsWithPreBuildEvents.aspx
Nov 7, 2008 at 6:50 AM
Edited Nov 7, 2008 at 6:51 AM
Hi,
I set my machine environment by adding below code in my machine.config.

<appSettings>
    <add key="Environment" value="PROD" />
</
appSettings>

As i mentioned that, i've main configuration file 'trace.exe.config' which is created using  Entriprise Library Configuration Tool.
Then i've created 2 diffrent environment that is STAGE & PROD.
Then, using 'Save Merged Configuration' option, I've created two configuration file. i.e PROD.congif & STAGE.config.

Now,  i am creating database trace listener as below: 

IConfigurationSource
configSource = new FileConfigurationSource(@"trace.exe.config");
DatabaseProviderFactory dbFactory = new DatabaseProviderFactory(configSource);
Database customDB = dbFactory.Create("Connection String");

If you look at the code, for configuration source, i am using 'trace.exe.config'.

I want to load configuration details depend on configurartion environment.
As of now, i am using 'trace.exe.config'. so when i run the application, it is taking connection string value defined in trace.exe.config eventhough i have set environment as 'PROD'.

Do i need to write any custom function which will load config file for configuration source by checking the environment value defined in machine.config?
OR
Will Application block internally take care of loading configuration file? If yes, then how?

http://www.davidhayden.com/blog/dave/archive/2007/03/01/EnvironmentalOverridesEnterpriseLibrary3.aspx - This article is only showing that how to make configuration file for diffrent environment. But its not showing how to make use of those diffrent environment specific configuration file.

Kindly, provide some guidance for this urgent issue.

Thanks,
Kaushal.

 

 

 

Nov 7, 2008 at 7:17 AM
Regarding your question number one, are you actually asking if there's any class in EntLib which can do this?  Well, there isn't any.  What you can do is add a registry entry that will indicate what environment your application is currently running on.  It will be your basis then what configuration file you will use to create an IConfigurationSource object. 



Sarah Urmeneta
Global Technology & Solutions
Avanade, Inc.
entlib.support@avanade.com

Nov 7, 2008 at 10:25 AM
Hi,
Thanks for your reply.

How to add envrionment into registry?
Do i need to create Environment variable with name-value = PROD:PROD, STAGE:STAGE?
And then i have to create one wrapper class which will check for environment from config/registry & then load configuration source according to environment.?

If this is the way then, i can sayt it is the limitation for enterprise library.
If they are providing facility to create diffrent configuration file (using Save Merged Configuration) for diffrent environment, then there mut be some way/logic to load that configuration file according to environment. There is no need to maintain 3 diffrent configuration file.

Do you have any point of contact for enterprise library?

Thanks,
Kaushal


Nov 7, 2008 at 2:22 PM
Edited Nov 7, 2008 at 3:10 PM
I think you're making this harder than it has to be.
The Environment Overrides in the configuration tool allows you to create an app/web.config for each environment. You're supposed to deploy the correct config file to the appropriate environment.
If you use an installer to deploy your application, you can make a dialog in this installer where one can select the correct environment. For instance, if the production environment is selected in the installer, the production config file should be put in the application directory. I did it this way at my client's and I think this is a common way of dealing with application settings on different environments.

Yes, you have 3 or 4 different config files you have to maintain, but it's still the simplest solution. I try to minimize writing settings to the registry. Writings settings to the registry makes your life a bit harder, since most of the time you won't have access to staging or production servers. When a problem comes up, you must contact a system administrator to look in the registry. If you keep all your application settings in your config file, you always know what the settings are (provided some system admin didn't change them obviously). If you keep the environment setting in a config file, you still have to maintain multiple config files. So the simplest solution is to deploy the right config file.

My humble 2 cents,
Nils Gruson