Updating configuration value in web.config after web services starts??

M

MisConFit8

My question is this - is it possible to set a standard value in the
web.config file and change it later based on conditions? Why, you might
ask? I am building a web based application for inter-office usage. It
is an application where software product keys and license keys are
stored and created. We have 1/2 of the office personnel working from
within the domain, and 1/2 that disconnect and work in the field. I am
setting up replication for those that disconnect, but would like to
have them connected to the server when they are here. (That is to allow
them real time connectivity w/o extra work on the server caused by
setting up transactional replication) I hoped to set the db string
(thats the configuration value in web.config) to the large SQL server
if the connection was successful, but to change the connection string
to the MSDE db on their local machine if the prev call failed.
Any insight as to if I can set that value after testing the
connections, whether I can test the connection via c# in the web.config
itself (which I don't think is possible), or a better way that I don't
see?
Any help would be greatly appreciated!!!
Sincerely,
MisC
 
N

Nicholas Paldino [.NET/C# MVP]

MisC,

I don't think that changing the value in the web.config file is a good
idea. Rather, you should have a list of connection strings, in an order (or
named in such a way which indicates preference) that is used.

When the application first starts, the system checks the first preferred
connection string. If the connection succeeds, it uses this. If it
doesn't, it cycles to the next one.

What I don't get is why this is necessary. In reality, you should only
need one connection string in the file, and have the file differ depending
on whether or not they are outside or inside the domain. When people rejoin
the domain, they won't be accessing the application on their local pc, they
will be accessing the application on the domain, which they will need a
different url for (and not access the replicated site on their pc).

In the end, you shouldn't be accessing the web.config file for something
like this. You should just deploy the correct web.config file, and have the
app use that.

Hope this helps.
 
M

MUS

Hello MisConFit8!

You need to remember onething that config file are
"read-once-cache-forever" story i.e. config file will be read as your
application starts and even if you write anything to it change wont get
reflected untill you restart your application.

As for your particular scenario, i suggest that you try a fail-over
approach by keeping as many of the connection string you want in your
config file (just a thought). If you are concerned about set of custom
elements in your config file, may be something like this can do (custom
sections):

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<configSections>
<section name="DatabaseRepository" type="<your_type>,
<your_type_assembly>" />
</configSections>
<DatabaseRepository>
<DataSources>
<DataSource name="sqlserver" connection-string="sqlserver-conn-str" />
<DataSource name="msde" connection-string="msde-conn-str" />
</DataSources>
</DatabaseRepository>
<configuration>

The sequence of the connection string will matter as if connection to
sql server cant be made then try out the msde connection string.

I hope this might be of some help.

Let me know in case of any inconsistancy.

Regards,

Moiz Uddin Shaikh
Software Engineer
Kalsoft (Pvt) Ltd
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top