views:

888

answers:

7

I have been trying to read up on how to do configuration properly in .net and have run into something that I find a bit odd and that is that configuration settings are compiled into assemblies through the Settings class. Removing the app.config and running the application does not result in configuration errors as I was expecting but rather now I have no way of replacing the configuration values.

In my application I have a configuration setting for a webservice url that I need to be able to pick up and set programmatically. Should I be creating custom sections in the configuration for my application that I will read through the ConfigurationManager.GetSection(..) to trigger reading of the config file or is there another way around this problem as I absolutely don't want the webservice url used to generate the ws proxy as an url that could potentially leak into production.

Please help.

A: 

If you click out the plus on the properties, and then on teh settings, you will find a file called settings.designer.cs

This file contains the default value you created when defining your application setting, and it is why your app works without a config file.

However you can always override this value.

Your only way to test this is to create a default value which is safe for public consumption, and change it during test.

Spence
In my case there is no value for the webservice url 'safe' for the public consumption. I have dev, stage, prod webservices and therefore if not set in the config file I want the application to throw a configurationerrorsexception instead of using the dev ws url which i used to create the proxy.
Fadeproof
A: 

When you add a reference to the webservice, you can set the property to make the webservice url dynamic (i.e. read from the configuration file).

Click on the "Web References". Click on your webservice and set the property "URL Behavior" from "Static" to "Dynamic".

Once you make the change, all you have to do to point the webservice to the right URL is to change the configuration file before your app runs.

Tundey
All my webservice references are set to URL Behavior=Dynamic which works fine. This still does not solve the problem initially stated. If I remove the config file after building the initial url is still picked up from the Settings which is compiled into the assembly.
Fadeproof
Restrict the IP address range for your dev web service so that only your dev machines can talk to it. Then if the config file gets removed after the app is installed at least the app will fail when it attempts to connect to it. The only other way I know is to generate the proxy by hand and modify.
tvanfosson
d Settings class reads from d app's config file. It's just a new way (in .Net 2.0) of strongly typing your app config values. However, to fix d problem u state, look in the Reference.cs file 4 your webservice and change d default URL. You'll have to keep doing this whenever you refresh the service.
Tundey
+1  A: 

Try adding an application configuration file to your project rather than a settings file. An application configuration file does not get compiled into the assembly.

Andrew Hare
When adding a webservice reference the settings file is automatically created. How can I counter that?
Fadeproof
What kind of web reference? Web service? WCF?
Andrew Hare
A: 

As far as i remember ( i stand to be corrected) , .Net creates a config file that is stored in your windows app data folder , this is especially true if you are deploying the app.

Search in the app data (C:\Documents and settings\yourusername\Local Setting\Application Data\your app name) and see if you can find the app config in there.If so you should be able to change it there.

Also i am not sure what type of web service you are creating , but should you not be using a web.config (its what i use) to store settings ?

RC1140
A: 

As far as I know the webservice reference logic states that if there is a certain key in the app.config (or web.config) it will pick that... if not... it will use the URL reference used to create the webservice reference. That in Framework 1.1 and may be 2.0.

The Settings.settings file... in framework 3.0 at least is used as the default values. If you don't provide a key in the web.config that overrides the ones in Settings.settings it uses the default values... and that is why you don't see a configuration error. In the case of webservice references, is in that file where is stored the value you provide to create the reference.

As I can see... the Settings.settings allows you to update the keys programatically in order to save preferences and some how get persisted.

More info:

Romias
A: 

I think I understand what you're saying, Jojoe. Say you add a webservice from www.productionurl.com, the proxy class generated keeps the production url in the source code. At runtime, it uses this default value if it can't find a value for the url in the configuration file (provided you've set the service to Dynamic).

If you don't like this behavior, you have 2 choices:

  • change the Reference.cs file (you'll have to keep doing this whenever you refresh the service proxy)
  • Create a partial class for the proxy and override the URL setting

Or you can check to see if the webservice has a development site. If it does, create the web reference from that site instead of the production site.

Tundey
A: 

look http://stackoverflow.com/questions/469742/where-are-user-mode-net-settings-stored

.NET settings are not in the assembly

Nicolas Dorier