Server specific Web.config replacement - visual-studio-2010

I am currently using Visual Studio 2010. I want to set up a different connection string for my "Testing" Configuration. I have tried doing it by using config transformations, but it does not seem to work.
In my Web.config my connection string is the following:
<add name="MyDb" connectionString="Data Source=.\SQLEXPRESS;Initial Catalog=DevelopmentDb;Integrated Security=SSPI; MultipleActiveResultSets=True" providerName="System.Data.SqlClient" />
I found out that you can replace the connection string defined in Web.config by adding config transformation. I right clicked on Web.config and clicked on "Add config transformations". This created a Web.TEST.config file. I then added a connection string replacement, but it doesnt seem to work. It still uses the connection string defined in Web.config.
The file contents of Web.TEST.config is:
<?xml version="1.0"?>
<!-- For more information on using web.config transformation visit http://go.microsoft.com/fwlink/?LinkId=125889 -->
<configuration xmlns:xdt="http://schemas.microsoft.com/XML-Document-Transform">
<!--
In the example below, the "SetAttributes" transform will change the value of
"connectionString" to use "ReleaseSQLServer" only when the "Match" locator
finds an atrribute "name" that has a value of "MyDB".
-->
<connectionStrings>
<add name="MyDb"
connectionString="Data Source=.\SQLEXPRESS;Initial Catalog=TestDb;Integrated Security=SSPI; MultipleActiveResultSets=True"
xdt:Transform="SetAttributes" xdt:Locator="Match(name)"/>
</connectionStrings>
<system.web>
<!--
In the example below, the "Replace" transform will replace the entire
<customErrors> section of your web.config file.
Note that because there is only one customErrors section under the
<system.web> node, there is no need to use the "xdt:Locator" attribute.
<customErrors defaultRedirect="GenericError.htm"
mode="RemoteOnly" xdt:Transform="Replace">
<error statusCode="500" redirect="InternalError.htm"/>
</customErrors>
-->
</system.web>
</configuration>
Any ideas to what I could be missing?

Transformations get applied when you deploy the project, not when you compile it.
If you are using IIS, you can deploy to the configured web application directory, which will cause the transform to apply.

Related

Web.config Remove ConnectionStrings node from Web.Release.Config

I have this in my Web.config so that my service will run locally:
<connectionStrings>
<add name="DefaultConnection" providerName="System.Data.SqlClient" connectionString="Data Source=IPADD;Initial Catalog=DB;Persist Security Info=True;User ID=UN;Password=PW" />
</connectionStrings>
I need this here because the Web.Debug.Config doesn't work. So when I publish I want to remove this string because it is actually inherited from a parent Web.config
<connectionStrings>
<remove name="DefaultConnection"/>
</connectionStrings>
This doesn't seem to work... (it throws a The entry 'DefaultConnection' has already been added. error)
How can I remove it? XSLT?
P.s. I can't use clear/ because it removes the Machine.Config DB Connection string to the Membership provider also throwing a different error
Put below code:
<connectionStrings>
<add name="LocalizationDB"
xdt:Transform="Remove" xdt:Locator="Match(name)"/>
</connectionStrings>
But be careful when you do publish from visual studio. When you look on Output view. It should write you something like:
Transformed Web.config using Web.Release.config into obj\Debug\TransformWebConfig\transformed\Web.config.
Copying all files to temporary location below for package/publish:
But if you have active "Debug" you will have Web.Debug.config instead of Web.Release.config

Configuring Lightspeed for use in MVC3 Web project

I have a question regarding using LightSpeed in a MVC3 Web application. I am creating a REST Api project where I want to use LightSpeed, but the official configuration guidelines are quite vague. The official documentation says that the following lines need to be added to the file web.config:
<configSections>
<section name="lightSpeedContexts"
type="Mindscape.LightSpeed.Configuration.LightSpeedConfigurationSection, Mindscape.LightSpeed" />
</configSections>
<lightSpeedContexts>
<add name="Test" />
</lightSpeedContexts>
<lightSpeedContexts>
<add name="Test" dataProvider="SQLite3" />
</lightSpeedContexts>
I tried adding following lines to the web.config in the root:
<configSections>
<section name="lightSpeedContexts"
type="Mindscape.LightSpeed.Configuration.LightSpeedConfigurationSection, Mindscape.LightSpeed" />
</configSections>
<lightSpeedContexts>
<add name="Default" connectionStringName="Prod" dataProvider="MySQL5" />
</lightSpeedContexts>
<connectionStrings>
<add name="Prod" connectionString="server=localhost;User Id=production;password=xxx;Persist Security Info=True;database=CBS"/>
</connectionStrings>
This throws an exception when I start the Web application which tells me that configSections may not be specified more than once in the application. The root web.config file did not have any of these specified per default.
I am not sure where to put this configuration.
OK, I found out how to solve the problem.
I moved out the configSections that were in the two sub web.config files (in each Views folder) and put the contents in the main web.config file, then I added the lightSpeedContexts and connectionStrings in the main web.config file.

Why am I getting a "No attribute 'name' exists for the Match Locator" error when publishing?

I am so confused on this. I want to use the SQL Server on my desktop when I am developing and using the live SQL Server when I publish my project. I am playing with the transformation stuff in Visual Studio 2010.
I get the "No attribute 'name' exists for the Match Locator" when I try to publish my project.
My Web.config file contains:
<connectionStrings>
<add name="EFDbContext" connectionString="Data Source=.\SQLEXPRESS;Integrated Security=SSPI;Initial Catalog=db" providerName="System.Data.SqlClient"/>
</connectionStrings>
<system.web>
<sessionState mode="SQLServer" sqlConnectionString="Server=.\SQLEXPRESS;Integrated Security=SSPI;Initial Catalog=ASPState;Application Name=eGov" timeout="20" allowCustomSqlDatabase="true" />
</system.web>
I am still testing it, so for now, my Web.Release.config file contains:
<connectionStrings>
<add name="EFDbContext"
connectionString="Data Source=.\SQLEXPRESS;Integrated Security=SSPI;Initial Catalog=db"
providerName="System.Data.SqlClient"
xdt:Transform="SetAttributes" xdt:Locator="Match(name)" />
</connectionStrings>
<system.web>
<compilation xdt:Transform="RemoveAttributes(debug)" />
<sessionState mode="SQLServer"
sqlConnectionString="Server=.\SQLEXPRESS;Integrated Security=SSPI;Initial Catalog=ASPState;Application Name=app"
timeout="20" allowCustomSqlDatabase="true"
xdt:Transform="SetAttributes" xdt:Locator="Match(name)" />
</system.web>
Anything I am seeing online is only confusing me more. Any help to get me up and running?
xdt:Locator="Match(name) mean that system will match nodes to replace using name tag. If you don't have name attribute then it fails. You must have some unique attribute to use this type of transformation.
Doh! The problem was in the sessionState section. It should be:
<system.web>
<sessionState mode="SQLServer"
sqlConnectionString="Server=.\SQLEXPRESS;Integrated Security=SSPI;Initial Catalog=ASPState;Application Name=app"
timeout="20" allowCustomSqlDatabase="true"
xdt:Transform="SetAttributes" xdt:Locator="XPath(configuration/system.web/sessionState)" />
</system.web>
Using "name" in Match(name) is for a typical config setting like the following. The key in this case is "name".
<add name="errorAddress" email="me#google.com" xdt:Transform="SetAttributes" xdt:Locator="Match(name)" />
If the key in your setting is something else, that is what you need to use:
<add token="UserToken" value="23jkl2klk2j3kja9d8f" xdt:Transform="SetAttributes" xdt:Locator="Match(token)"/>

Config Transform does not work?

I have this config in my Web.config file:
<connectionStrings>
<add name="LocalConnString" connectionString="metadata=res://*/;provider=System.Data.SqlClient;provider connection string="Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\NCU.Joberwocky.Db.mdf;Integrated Security=True;User Instance=True;MultipleActiveResultSets=True"" providerName="System.Data.EntityClient" />
</connectionStrings>
This in my Web.Release.config:
<?xml version="1.0"?>
<configuration xmlns:xdt="http://schemas.microsoft.com/XML-Document-Transform">
<connectionStrings>
<add name="LocalConnString" connectionString="metadata=res://*/;provider=System.Data.SqlClient;provider connection string="Data Source=192.168.10.3;Initial Catalog=AeorionJobs;User Id=aeorionjobs;Password=aejobs3695;MultipleActiveResultSets=True""
xdt:Transform="SetAttributes" xdt:Locator="Match(name)"/>
</connectionStrings>
</configuration>
When I switch the build to release, it still uses the config from Web.config. Any idea what the problem is?
I am also struggling with configuration transforms. Actually I am trying to have a different configuration on my environment than the common one uploaded in code control.
I have been also trying the same thing as you are doing and I would say those transforms are only applied on the deployment packages that you can build.
Hope it helps.

Nesting appSettings with the file attribute

I'm trying to pull off an inheritance chain of appSetting sections (VS2010 C#)
Given this,
Base.config
<appSettings>
<add key="basekey" value="basevalue"/>
</appSettings>
Derived.config
<appSettings file="Base.config">
<add key="derivedkey" value="derivedvalue" />
</appSettings>
App.config
<configuration>
<appSettings file="Derived.config">
<add key="mykey" value="myvalue" />
</appSettings>
</configuration>
This line:
ConfigurationManager.AppSettings["derivedkey"]
Throws exception:
Unrecognized attribute 'file'. Note that attribute names are case-sensitive. (...\Derived.config line 1)
It appears that App.config can successfully "file" over to Derived.config, but Derived.config is unable to "file" over to Base.config because "file" suddenly becomes an unknown attribute.
It's a little circular/confusing to me since the "file" attribute in App.config must be recognized successfully in order to reach Derived.config, where the same "file" attribute is suddenly unknown.
This is just not alllowed. You can not specify another external AppSetting file from within the first external AppSetting file.

Resources