How can I instruct app.config to use different reference per environment?
For example: I have a configuration folder that contains 3 folders - Dev, QA and Prod
- Configuration
- Dev
* AppSettings.config
- QA
* AppSettings.config
- Prod
* AppSettings.config
My app.config have this reference
<appSettings configSource="Configuration\Dev\AppSettings.config" />
I would like to have something like
<appSettings configSource="Configuration\[$ENV]\AppSettings.config" />
$ENV config should be defined in appSettings or if not possible other external source.
Also what is the best practice to manage that? (without using fancy Chef server or configuration server).
**I'm also trying to avoid post script manipulation.**
You need to use XML configuration file trasforms (see this MSDN article).
Since default configuration transforms are implemented for Web.config, you need to use a Visual Studio extension to get this feature to any configuration file. Slow Cheetah is a good one.
Another solution can be using environment variables:
Using environment variables for .config file in .NET
Then bootstrap the application to change the app.config at runtime
http://www.codeproject.com/Articles/12589/Modifying-Configuration-Settings-at-Runtime
Related
I am currently working on .net core web application deployment in multiple environment - Testing, UAT, PROD, etc.
As per documentations says, we should have environment specific appsettings.{environment}.json files so when we deploy application onto specific environment, respective file will be used.
I believe we use IIS -> configuration Editor to set ASPNETCORE_ENVIRONMENT variable.
However, I am unable to find, how can we do this thru deployment plan without going onto server.
Currently I am using Octopus deployment tool.
Any idea?
I think you need to store environment specific settings in Octopus Deploy (in scoped variables) rather than in your .json files which will
- or should even - be under source control.
Say your appsettings.json looks like this
{
"weatherApiUrl": "dev.weather.com",
"weatherApiKey": "DEV1234567",
"tempImageFolder": "C:\temp\img"
}
In your Project Variables you specify the weatherApiUrl variable and set it to dev.weather.com scoped to Development, test.weather.com scoped to Test, etc.
In your Process, in the Deploy step, goto Configure Features and enable JSON configuration variables. Now the Deploy step will have an extra section named JSON configuration variables, specify the name of your config file there, appsettings.json and off you go.
More information here: JSON Configuration Variables Feature
Is there a way to set Project Settings with Build Configuration specific values in VS2013?
I need to set different values for a WebServiceURL setting from one Build Configuration to another (say MSSQL configuration vs Oracle configuration).
I saw both a preprocessor assembly configuration using #if DEBUG and an afterbuild config file overwrite approach, but the former does not allow for custom configuration names or more than 2 configurations while the later involves copying files after build rather than the quick and easy edit in the Project Properties > Settings page.
Is there something similar to Web.config Transformation available to App.config maybe?
Try this plugin, it works for me. And you can transform any xml-file.
https://visualstudiogallery.msdn.microsoft.com/579d3a78-3bdd-497c-bc21-aa6e6abbc859
I have an asp.net/c# website that runs on a dev server and a prod server. We keep the code identical between both servers. Is it possible, based on the URL to assign different settings in the web.config (e.g. for error pages etc)
Ideally I want an IF statement in the web.config e.g.
if (url.contains "http://dev") {
web.config += #'<customErrors mode="Off" />'
} else {
web.config += #'<customErrors mode="On" defaultRedirect="~/web/error.aspx" />'
}
Application configuration files have no direct templating nor scripting support so the approach outlined in the question is not possible. Building configuration files per environment is better placed as part of a build process.
If you are using Visual Studio 2012+ (or 2010 with the Visual Studio Web Publish Update) then you can use web.config transformations to manage the difference configurations across your environments.
This can be made to work for Web Site projects as per the question using some creative thinking. E.g. http://andrewtwest.com/2010/02/25/using-web-config-transformations-in-web-site-projects/. Although this involves creating a separate project for configuration management, it is a considerably less complex approach to implementing a full build process outside of the IDE.
Using this approach, you have a master web.config which contains your base config that is common across environments. In addition, you have a web.config transform file per build configuration that applies transformations specific to that environment. In your case, this would be to turn off customErrors in dev.
More information available here: http://msdn.microsoft.com/en-us/library/vstudio/dd465318(v=vs.100).aspx
But the basic idea is make sure you have all the build configurations you need, then right click on your web.config and select add transforms. These are then applied when the site is deployed using the respective build configuration.
I am using the Nuget Packager extension to create a Nuget Package to include my Logger project and Nlog. I have this working correctly but had a question about how do i use the correct config file for multiple environments (Dev/QA/Prod)?
Do I have multiple config files, one for each environment in the package? but how does the correct one get applied in QA for example, since different environments might have different listeners or targets? Whats the best way to accomplish this, an example to show how to do this would be great since i am new to Nuget and Nlog.Thanks for your help!
Nlog supports different locations of its config file. One of the location is the main configuration file of application. For more information please see Nlog documentation
Then we can use config transformations option. Visual studio supports this by default for web firendly projects (ASP.NET MVC, WCF etc.). If you application is of different type you can use an extension called SlowCheetah(NuGet Link). More information can be found at this page.
Config transformation deped on build configurations in visual studio. In web project you can see that web.config has two transformations: web.Debug.config and web.Release.config.
Hope it helps
I'm using Visual Studio 2008, developing a winforms app.
I have my database connection string stored in settings.settings.
My development and production environments require different database login credentials and right now I'm manually changing the connectionstring by hand before building for the 2 different environments.
Is there a better solution?
An eternal problem! :-)
Basically, right now, Microsoft doesn't really have a good answer for this.
What I would do is have both connection strings in my settings file, under two separate names, and then have a config setting in app.config which tells me which one to use:
MyDatabaseDEV = server=(local);database=mydatabase;-........
MyDatabasePROD = server=ProdServer;database=MyDatabase;........
and in app.config
<appSettings>
<add key="UseDatabase" value="MyDatabaseDEV" />
</appSettings>
This setting in app.config can be tweaked in your build process, e.g. by a "after build" batch file, or an MSBuild task or something, to be switched to "MyDAtabasePROD", when you do a production (release) build.
Microsoft promises us more and more flexible tools for .NET 4.0 / Visual Studio 2010, which should be out by the end of the year 2009. You should be able to create "config transformations" - check out these blog posts:
Web Deployment: Web.Config Transformation
ASP.NET 4.0 and Visual Studio 2010 Web Development Beta 1 Overview
Visual Studio 2010: Web.config transforms
Web.config Transformations with Visual Studio 2010
Marc
Where I work this is done by creating a folder called config that holds the various configurations.
source
config
MyProject
environment1
environment2
MyProject2
environment1
environment2
When the build script is run, it grabs the correct config based on which environment you're doing the build for...
you can make form asking the user to enter the connection parameters such as server name and database..., and store those parameters in Registry, files etc...
You should add an app.config file to your winform application. In this file store the connection string under the ConnectionStrings section.
In your code, when create a connection use that connection string using ConfigurationManager class to access config file.
So when you deploy your application, you have to chance the connection string once in config file and everything runs as expected!
You can defines your connections in settings, use dev connection in debug mode else production connection with #if directive.
//affect your prod connection here
#if DEBUG
//re affect your dev connection here;
#endif
A link to explain : http://msdn.microsoft.com/fr-fr/library/4y6tbswk.aspx
Right-click on the web site in the Solution Explorer and select Add Web Deployment Project.
Whenever this new WDP project is built, it will replace all the configuration elements you specify. You can also have different versions depending on Debug or Release builds. It works for almost all of the configuration options, including the connection string. There's lots of documentation on how to configure it correctly on the net, just search for "Web Deployment Project".
This is definitely the new "default" way to do this until MS decides to make it more formal in some future version of .Net/Visual Studio, if they ever do.
I've started using a setting called TestMode="dev|prod|uat".
The problem with compilation targets is that you can't change anything after build.
With this approach you can include all the connection strings and settings you need for all the environments you want. In your code configuration provider you can switch based on this setting.
With this approach all you have to do is change the flag once deployed or use deployment software like Octopus to change it for you.
I have given up with merging .config files which is difficult and error prone.