What does <clear /> signify when specifying a connectionstring? - asp.net

This answer to another question states:
Do not forget to clear the connectionStrings first:
<connectionStrings>
<clear />
<add name="LocalSqlServer" connectionString="Data Source=(local);Initial Catalog=aspnetdb;Integrated Security=True" providerName="System.Data.SqlClient"/>
</connectionStrings>
... interesting. What does that do?

In .Net config files are inherited, so your applications config will inherit settings from your machines config.
The <clear/> tag will remove any inherited connection strings and thereby avoids confusion and potential problems.
In ASP.Net you may have several inherited connection strings, so this is very common there.

The element removes all sections and section groups from your application that were defined earlier in the current configuration file or at a higher level in the configuration file hierarchy.
http://msdn.microsoft.com/en-us/library/aa903345(v=vs.71).aspx
so for example, if this was a child config file and the parent config file had some settings... you may not want them being inherited so you specify the clear flag to clear it and then use your settings.

Related

Web.Config Transform Individual AppSettings to ConfigSource

I'd like to transform the appsettings section of my local web.config file from a bunch of individual settings for my local dev work, to a configSource file path attribute for publishing to client servers.
Basically from this:
<appSettings>
<add key="setting1" value="devVal1"></add>
<add key="setting2" value="devVal2"></add>
<add key="setting3" value="devVal3"></add>
</appSettings>
to this:
<appSettings configSource="clientSettings.config" />
This partial solution adds the configSource attribute.
<appSettings xdt:Transform="SetAttributes(configSource)" configSource="clientSettings.config"/>
But I'm not sure how to remove all the individual settings as well (I think the configSource will override them anyway, but I'd prefer to not have them there at all, to avoid having irrelevant dev settings in each of my client's web.config files)
A bit more perseverance lead me to the answer. Apply the "RemoveAll" transform to an add element within the appSetting.
<appSettings xdt:Transform="SetAttributes(configSource)" configSource="clientSettings.config">
<add xdt:Transform="RemoveAll"/>
</appSetting>
Hope this is of use to someone in the future (if only as an example of why you should do 30 minutes of extra work before reaching for the 'Ask Question' button)...

Use machine.config setting and ignore web.config setting

I have some important settings in the machine.config file and I need to prevent that websites can modify them in thier web.config files. In other words, I need those setting in the web.config files to be ignored.
I know I can lock elements but I don't want to have an exception, I just want to be ignored.
Example:
In machine.config
<appSettings>
<add key="a" value="AAAA"/>
</appSettings>
In web.config
<appSettings>
<add key="a" value="BBBB"/>
</appSettings>
In a website or webapplication
ConfigurationManager.AppSettings["a"] // returns "AAAA" from machine.config
Is it possible to do this?
Clarification:
These settings that I need to protect are the wcf serviceModel client elements and not appSettings as my example.

Connection Strings

Could you help me understand what this piece of code does in simple English? This is a beginner. Thank you in advance.
<connectionStrings>
<add name="BalloonShopConnection" connectionString="Server=(local)\Sql➥
Express; Database=BalloonShop; User=balloonshop; Password=ecommerce" ➥
providerName="System.Data.SqlClient" />
<remove name="LocalSqlServer"/>
<add name="LocalSqlServer" connectionString="Server=(local)\SqlExpress;➥
Database=BalloonShop; User=balloonshop; Password=ecommerce" providerName=➥
"System.Data.SqlClient" />
</connectionStrings>
This is from a tutorial. I covered everything up to first half of the book but this seems strange.
The web.config for your app is only part of the configuration settings your app gets. The total configuration is a combination of your web.config as well as the machine.config file, and settings defined in IIS. This line:
<remove name="LocalSqlServer"/>
implies that the there is a connection string named LocalSqlServer defined elsewhere that you may be getting from somewhere other than your web.config. So in your web.config they are explicitly removing that other LocalSqlServer connection string you would otherwise get, and replacing it with the one defined below that line. That change only affects your application. This is explained here: http://weblogs.asp.net/jgalloway/archive/2012/01/17/10-things-asp-net-developers-should-know-about-web-config-inheritance-and-overrides.aspx
If the remove tag wasn't there, and that connection string was also defined higher up the chain, your app would use the one defined higher up, and ignore the one defined in your web.config (which can be quite confusing!). That's why the remove tag is needed.
See also:
Avoid web.config inheritance in child web application using inheritInChildApplications
You add connection string which names BalloonShopConnection. Your sql server names (local)\SqlExpress; Your databese is BalloonShop user is balloonshop so you add second one which is very similar :)

asp.net change value of a web.config item?

im trying to get role provider working in multiple environments and hitting a wall
(link text)
i should be able to dynamically set the connectionString property of the web.congig item on app_onstart to the correct DB conection String to get it to work...
can anyone show me how to dynamically alter items in the web.config?
im guessing reflection...
<roleManager enabled="true" defaultProvider="SqlRoleManager">
<providers>
<clear/>
<add name="SqlRoleManager" type="System.Web.Security.SqlRoleProvider" connectionStringName="ISConnectionString_prod" applicationName="IS"/>
</providers>
</roleManager>
i want to adjust the connectionStringName value in the above snipet
thanks
If you're using VS2010, you can get it to automatically apply transforms to your config files depending on which environment you're publishing to.
We use this to set connection strings, payment provider config settings (sandbox mode, username, etc.) and a couple of other things like how exceptions are handled.
If you're not publishing, you can actually hook these transforms straight into the build engine by editing the project file.
This makes it incredibly simple to maintain (You have a web.config and a web.Live.config which contains the transforms). It also makes the whole process far less error-prone
eg:
web.config
<connectionStrings>
<clear />
<add name="OurConnectionString" connectionString="Data Source=DevDbHostname;Initial Catalog=DevDb;user id=Username;password=Password;MultipleActiveResultSets=True" />
</connectionStrings>
web.Release.config
<configuration xmlns:xdt="http://schemas.microsoft.com/XML-Document-Transform">
<connectionStrings>
<add name="OurConnectionString"
connectionString="Data Source=LiveDbHostname;Initial Catalog=LiveDb;user id=Username;password=Password;MultipleActiveResultSets=True"
xdt:Transform="SetAttributes" xdt:Locator="Match(name)"/>
</connectionStrings>
</configuration>
As long as the permissions allow it (you will have to change them), you can treat the web.config just as any other XML file. Knowing that, you can simply use an XDocument and pop in a new XElement where you want it. But be very careful and be sure to save some backups!

What i s best practice for accessing settings from config?

I want to know what best practice is for accessing settings
in config file when you have dev/test/production types.
If you have different config for each type when you
publish a ASP.NET website doesn't the config get copied as well??
Malcolm
We usually manually inject the settings file on each site. I think that it's uncommon, though not unheard of, to actually rely on VS to publish to your production site. Source control has dev/test/prod/ etc. web.config files.
ConfigurationManager.AppSettings ?
http://msdn.microsoft.com/en-us/library/system.configuration.configurationmanager.appsettings.aspx
In Visual Studio 2010 you can maintain Multiple Web.Config and use a transformation to generate the correct Configuration for an environment.
http://blogs.msdn.com/webdevtools/archive/2009/05/04/web-deployment-web-config-transformation.aspx
Basically we can make have one default Web.Config and different Transformation files for each environment e.g.
Web.Debug.Config
Web.Staging.Config
Web.Production.Config
The Transformation file can override the value of a particular config item for the environment e.g.
<?xml version="1.0"?>
<configuration xmlns:xdt="http://schemas.microsoft.com/XML-Document-Transform">
<connectionStrings>
<add name="personalDB"
connectionString="Server=StagingBox; Database=personal; User Id=admin; password=StagingPersonalPassword"
providerName="System.Data.SqlClient" xdt:Transform="Replace" xdt:Locator="Match(name)" />
<add name="professionalDB"
connectionString="Server=StagingBox; Database=professional; User Id=professional; password=StagingProfessionalPassword"
providerName="System.Data.SqlClient" xdt:Transform="Replace" xdt:Locator="Match(name)"/>
</connectionStrings>
</configuration>
Whenever we target build for that environment the Transformation are applied to the default Web.Config file.

Resources