when does web.debug.config apply? - c#

I have a project which has a normal web.config with the following appsettings:
<add key="CachingEnabled" value="true"/>
<add key="IsDeployed" value="true"/>
In my web.debug.config I have tried to overwrite these settings with
<add key="CachingEnabled" value="false" xdt:Transform="SetAttributes" xdt:Locator="Match(key)"/>
<add key="IsDeployed" value="false" xdt:Transform="SetAttributes" xdt:Locator="Match(key)"/>
And I also put the corresponding values in the web.release.config
However, when I step through my code, these values are still the same as the ones in the main web.config
Do these values only apply if you deploy the site in debug mode or should they work if you are just running the site locally? I am running the site locally through iis rather than by pressing the play button in visual studio if that makes any difference.

These values don't apply to anything, what happens is as part of the publish command VS transforms your default web.config using the rules you have defined in your build-specific web.config (in your case, your debug build config).
A lot of people get confused thinking that these values just automagically work when you build your project, that's not the case. However, if you want this behaviour there is a nice little plugin called SlowCheetah that will do this for you.
It's worth reading up on Web.config File Transformations and How to: Transform Web.config When Deploying a Web Application Project

The web.debug.config or web.release.config are only consumed by web deployment to transform web.config, say "Build deployment package" or "Publish". Regular build/debug doesn't invoke transform.
SlowCheetah may be able to help you: http://visualstudiogallery.msdn.microsoft.com/69023d00-a4f9-4a34-a6cd-7e854ba318b5

Related

Configuring the Seq URL from a single place

I have a solution with a bunch of .net framework applications that are all configured to use Seq for logging. Currently I have the same settings duplicated in every app.config file:
<appSettings>
<add key="serilog:using:Seq" value="Serilog.Sinks.Seq" />
<add key="serilog:write-to:Seq.serverUrl" value="http://localhost:5341" />
</appSettings>
If I would like to be able to configure the Seq URL for all the applications from a single file, not only when running in Visual Studio but also when the system has been deployed.
How can this be done?
I have looked into creating a common settings file and include it in all applications using <appSettings file="CommonSettings.config">, but from what I understand this actually creates a copy of the file for each application.

Web.config Transform Issue with Azure Project Deployment

I have a .net MVC project that i'm attempting to deploy directly to Azure using the publish settings file. I can publish locally without an issue, however when attempting to publish to Azure, it appears to be trying to transform the web.config files within subfolders (which don't have transforms, and don't need them). I receive the following error:
Transformed Web.config using C:\Projects\Git\Web.Project\Web.Debug.config into obj\Debug\TransformWebConfig\transformed\Web.config.
C:\Program Files (x86)\Microsoft Visual Studio\2017\Enterprise\MSBuild\Microsoft\VisualStudio\v15.0\Web\Microsoft.Web.Publishing.targets(2311,5): Error : Could not open Source file: Could not find a part of the path 'C:\Projects\Git\Web.Project\App_Plugins\PluginName\Web.config;\App_Plugins\PluginName\Web.config'.
The web.config referenced does exist (I've verified) and this error, as mentioned, is not thrown when publishing to a local folder.
Any suggestions as to why this is occurring and how to fix it?
Edited: Added contents of web.debug file below
<?xml version="1.0" encoding="utf-8"?>
<!-- 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 attribute "name" that has a value of "MyDB".
<connectionStrings>
<add name="MyDB"
connectionString="Data Source=ReleaseSQLServer;Initial Catalog=MyReleaseDB;Integrated Security=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>
According to your description, you could go to .net project in visual studio and right click Property on the web.config.
Set your Build Action to Content. This means that there will be no transform on this config, usual configuration is compile.
i'm attempting to deploy directly to Azure using the publish settings file.
To achieve you requirement, you could refer to import publish settings and deploy to Azure.
Step 1: Create the publish settings file in azure app service.
Click Get publish profile and save the profile locally.
Step 2: Import the publish settings in Visual Studio and deploy
After deploying, you could visit it successfully through browser.

Set different appSettings key in production server

How can I set a different value when I publish my MVC5 do production server?
Example:
In dev I have
<appSettings>
<add key="XLSFile" value="C:\\temp\\file.xls" />
</appSettings>
And when I publish the project I want to set a different path:
<appSettings>
<add key="XLSFile" value="C:\\projectname\\file.xls" />
</appSettings>
You'll want to use a config file transformation. Essentially you will override the config file for your release build.
For example:
<add key="XLSFile" value="C:\\projectname\\file.xls" xdt:Transform="Replace" xdt:Locator="Match(key)"/>
You can use two config files one with name web.Debug.config and second web.Release.config. First one for development and second for production server when published and now specify different value of key in both files as required

ConnectionString for assemblies

I have a website with a connection string listed in its web.config. The connection string is altered by the Publish feature so that it can reference a development database until it is released, when it references a separate release database.
The website accesses the databases through some assemblies, though. They are Class Libraries so they can't be Published, at least as far as I can tell. I read that the web.config would override the app.config connectionstrings, but that doesn't seem to be happening.
Whenever I Publish the release site references the development database, unless I alter the assemblies app.config files to reference the release database.
I don't want to have to remember to do that every time. How do I handle this?
You've got two issues here:
1. How to remember to publish the correct settings each time you publish:
One way to deploy such settings is by using web.config transformations in Visual Studio. This is pretty easy to set up and means that you do not have to remember to update the settings each time you publish.
As well as debug and release environments, you can also create transforms for "UAT", "Staging", "Beta" or whatever other configs you might need.
You might find these articles useful: here, here and here.
For example, here is a transform for a Release environment:
<?xml version="1.0"?>
<configuration xmlns:xdt="http://schemas.microsoft.com/XML-Document-Transform">
<connectionStrings>
<add name="MyDB"
connectionString="Data Source=ReleaseSQLServer;Initial Catalog=MyReleaseDB;Integrated Security=True"
xdt:Transform="SetAttributes" xdt:Locator="Match(name)"/>
</connectionStrings>
<system.web>
<compilation xdt:Transform="RemoveAttributes(debug)" />
<customErrors defaultRedirect="GenericError.htm"
mode="RemoteOnly" xdt:Transform="Replace">
<error statusCode="500" redirect="InternalError.htm"/>
</customErrors>
</system.web>
</configuration>
Above, you will see that the transform for Release mode sets the attributes of the MyDB connection string (xdt:Transform="SetAttributes"), removes the debug attribute from the compilation section and replaces the customErrors section with a new version.
It's a simple, yet very powerful technique.
2. How to get your assemblies to pick up the settings in the web.config
If your libraries have been written in the usual way, they should be retrieving their connection strings by simply accessing the [Web]ConfigurationManager.ConnectionStrings property. Like #Bob Horn says, they should then pick up the settings from the host process's config file (in this case the web.config of your web app).
However, sometimes you might find that a library is getting its settings from a .Settings file in the project, in which case things get a little more complicated. You will need to copy the settings section of the app.config in to the web.config. (You can also do this using the transforms technology described above.)
If you have access to the source code of the other assemblies, find the part of the code that retrieves connection strings. If it's not accessing the ConfigurationManager class, then that might explain why it's not picking up the web.config file.

Web.Config Debug/Release : Doesn't work

I have this in my web.config
<add name="AS400" connectionString="Provider=IBMDA400;Data Source=AAAAA;User Id=aaaaaa;Password=aaaaaa;Default Collection=*SYS;" providerName="System.Data.OleDb" lockItem="true"/>
In my Web.Debug.config (The same of Webconfig)
<add name="AS400" connectionString="Provider=IBMDA400;Data Source=AAAAA;User Id=aaaaaa;Password=aaaaaa;Default Collection=*SYS;" providerName="System.Data.OleDb" lockItem="true" xdt:Transform="SetAttributes" xdt:Locator="Match(name)"/>
In my Web.Release.config
<add name="AS400" connectionString="Provider=IBMDA400;Data Source=ZZZZZ;User Id=aaaaaa;Password=aaaaaa;Default Collection=*SYS;" providerName="System.Data.OleDb" lockItem="true" xdt:Transform="SetAttributes" xdt:Locator="Match(name)"/>
But nothing working. If i make F5, in Release mode, i have always the connection string in web.config.
An idea?
Configuration transformation is only done when publishing. Your base configuration file should have your development settings. If you choose to use the default build configurations, normally the release transform file should contain your production environment settings and the debug transform file will contain your test environment settings.
Personally, I usually create a new build configuration for testing and for production and leave the debug and release transforms empty.
Edit:
If you use the latest version of the SlowCheetah extension for Visual Studio, it will transform your configuration files during the build.

Categories

Resources