Web.config and different configurations in different projects missmatch - c#

I'm using this code (in my DAL project):
ConfigurationManager.AppSettings["server"]
to access appsettings section in web.config file (from web project):
<appSettings>
<add key="server" value="server.name.com"/>
<add key="database" value="databasename"/>
</appSettings>
and in a Web.Debug.config I'm using a following transformation
<add key="server" value="MY-LAPTOP"
xdt:Locator="Match(key)" xdt:Transform="Replace"/>
after that when I start application the config file isn't transformed. First line of code returns the nontransformed infromation. What's wrong with the code? What am i missing?
I have tried to publish it and when I check config file everything is ok like it is ment to be.

The web.config transformation is only perform during the publish process. You can still enable it on every build, when you it F5, see
ASP.NET Web Projects: web.debug.config & web.release.config
SlowCheetah - XML Transforms
Making Visual Studio 2010 Web.config Transformations Apply on Every Build
It's an MSBuild task to add.

Related

Why do my IIS site-specific application settings disappear?

I'm hosting an ASP.NET C# website using IIS on Windows Server. I publish to the IIS server using Web Deploy. I have a few application settings that I configure via IIS' site-specific application settings - mostly passwords that I don't want in plain-text in my app.config within the project itself. Every time that I publish, the settings that I've created via IIS disappear. I think the publish is over writing them. I can't figure out how to get them to persist through publishes. Does anybody know how to get the IIS application settings to remain even through Web Deploy publishes?
Not sure if that solves your problem, but you could try something like this:
Every config section has an optional attribute named "configSource" to
point to an external file. Therefore you could break down your web.config
into several files and update them accoringly:
web.config:
...
<configuration>
<appSettings/>
<connectionStrings configSource="connectionStrings.config"/>
<system.web>
...
connectionStrings.config:
<connectionStrings>
<add name="defaultConnectionString" connectionString="..." providerName="System.Data.SqlClient" />
</connectionStrings>
Then you could set the connectionString.config to not be included in the output of the project and just keep it unchanged on the server.
You could also:
-create a new build configuration for deployment
-add a web.config transformation for this build configuration
-deploy using this build configuration
That's how I usually do it

Can I store settings in a settings.config file in ASP.NET MVC?

I'm using .NET MVC
I have about 10 properties I want to store in a configuration file (.config etc.), related to environment/deployment stuff, + other things for quick changes without doing dLL deploys.
I'm using Team foundation service for CI builds etc, and my web.config is obviously under version-contrl.
What I'd like to do is have a settings.config (that's not in version control) file to store these, am I able to do this?
Or does it need to be in web.config?
To answer the title question, yes you can store settings in a separate config file, to do so you need to define the configSource property of appSettings element
E.g.
<appSettings configSource="settings.config" />
and in the settings.config file
<?xml version="1.0" encoding="UTF-8"?>
<appSettings>
<add key="settingKey" value="environmentValue" />
</appSettings>
However, for the sake of environment specific settings, you may want to look at config transforms. Setting up a transform config for each environment then deploying to that environment with the specified build configuration.
E.g. Web.Dev.config (provided you have setup a 'Dev' build configuration)
<configuration xmlns:xdt="http://schemas.microsoft.com/XML-Document-Transform">
<appSettings>
<add key="settingKey"
value="devEnvironmentValue"
xdt:Transform="SetAttributes" xdt:Locator="Match(key)"/>
</appSettings>
</configuration>
More details of build configuration and config transforms here: http://msdn.microsoft.com/en-us/library/dd465318(v=vs.100).aspx
Or you could take advantage of TFS features and parameterize the environment variables, I don't have a lot of experience with this, but the following should help: http://ig.obsglobal.com/2013/02/tfs-and-continuous-deployment-part-4-parameterized-deployments/

Hiding private details from open source projects

I have a .net github project that is basically a wrapper around a web API. In the test project, I am calling to the API using an API key. I need to keep this key private, how do I accomplish this in a visual studio project?
In some other projects, like python, I can have git ignore the file (config.py) and use something like config.example.py. But in visual studio's case, the project will not compile because of the missing file Config.cs. What is the proper way to solve this? I'm thinking of using this same method of ignoring the file and have them execute a build script that should rename Config.example.cs to Config.cs?
This is the perfect for .config files. Depending on whether its a web or console application, you will have a web.config or app.config file in your project.
You can use the appSettings section to store your API key.
To make things even easier, you can actually have this section read from another file, ie: specialappsettings.config and then just ignore that single file from your repository.
Modify your web.config (or app.config):
<configuration>
<appSettings file="specialappsettings.config">
</appSettings>
<system.web>
<!-- standard web settings go here -->
</system.web>
</configuration>
Create a new specialappsettings.config file:
<appSettings>
<add key="APIKey" value="YourApiKeyValue" />
<add key="AnotherKey" value="AnotherValue" />
</appSettings>
This can be accessed in your code via:
var apiKey = ConfigurationManager.AppSettings["APIKey"];
Notes:
You can keep your settings within the original web.config file as
well but this lets you ignore just the specific settings file from
your git repository without affecting the rest of the project's
necessary configuration details.
The same "key" can be saved in
either file however the external file will override the original
web.config file value.
You are probably looking for the App.config file for a project. It will be copied to <application>.exe.config when you compile it. Users can edit that config file as needed.
In that config file, you can add your API keys:
<configuration>
<appSettings>
<add key="APIKey" value="12345"/>
</appSettings>
</configuration>
Then you can access it from your code using ConfigurationManager.AppSettings:
string apiKey = ConfigurationManager.AppSettings["APIKey"];
One option is to use .config files instead of having secret keys hardcoded in sources.
More info Using Settings in C# and step-by-step guide
<configuration>
<appSettings>
<add key="SecretKey" value="0" />
</appSettings>
</configuration>
var secretKey = ConfigurationManager.AppSettings.Get("SecretKey");
Perhaps you can store the key outside of the Config.cs file and load it at run time.
Bonus, other people using your code won't have to recompile the project to change to their API key.

web.config transform elements not available through System.Configuration.ConfigurationManager

I have a web.config file that's transformed using a web.debug.config file. Trying to get access to these values through System.Configuration.ConfigurationManager.AppSettings is yielding nothing.
My web.config appSettings are empty.
<configuration>
<appSettings>
</appSettings>
</configuration>
My web.debug.config transform that's applied. Here's a sample.
<configuration>
<appSettings>
<add key="CommonURL" value="localhost/mysite/" xdt:Transform="Insert" />
</appSettings>
</configuration>
Here's the code to try and get this value, which returns null.
var cu = System.Configuration.ConfigurationManager.AppSettings["CommonURL"];
Any idea on why this could be?
Assuming that System.Configuration.ConfigurationManager.AppSettings is yielding nothing when you run from Visual Studio, then that is the expected behavior. You should add the CommonURL appSetting to your web.config file and change the entry in the web.debug.config to:
<add key="CommonURL" value="localhost/mysite/"
xdt:Transform="Replace" xdt:Locator="Match(key)" />
These changes will allow you to get the value specified in the web.config when you run from Visual Studio, and will allow that value to be replaced with what is defined in the web.debug.config when you execute the transform (for example, through the Publish function or through a custom MSBuild script). Keep in mind that the transforms are applied when you publish, not when you start running a debug or release build. The values that are in the web.config file are always the ones that are honored.

C# - Use configuration file in Debug mode

I discovered few days ago that we can use Configuration files in .NET and trying to use it in my applications.
First of all, configure correctly the use of configuration file is really borring :
Configuration file should have the same name as the application (understandable)
Then think to add the System.Configuration reference (understandable too)
When file is added, go in it's properties and change it to copy the file in output directory (less understandable).
Configuration file isn't taken into account in debug mode (because of *.vhosts.exe)
It takes me time to understand why this file wasn't taken into account...
So question is pretty simple, how can I fix this and use configuration files in Debug Mode ?
I would use it in order to configure my trace switches.
Here is my App.config file :
<configuration>
<appSettings>
<add key="A" value="B"/>
</appSettings>
<system.diagnostics>
<switches>
<add name="myFirstSwitch" value="1" />
<add name="MySecondSwitch" value="Error" />
</switches>
</system.diagnostics>
</configuration>
Thanks.
I think you created your config file the wrong way, the right way is:
On the Project menu, click Add New Item. The Add New Item dialog box
appears.
Select the Application Configuration
File template and then click Add. A
file named App.config is added
to your project.
This config file is automatically copied to the build folder when you build the project and works in both Debug and Release mode.

Categories