Differences between App.Config and Web.Config? - asp.net

when building a desktop app in wpf can you read documentation of problems and safely subsititute 'app.config' when people's answer's refer to 'web.config'?
if so are there any glaring GOTCHAS you have to look out for?
tnx

Read the Documentation:
Web.config and App.config
The choice of the
configuration file name is determined by the hosting environment you
choose for the service. If you are using IIS to host your service, use
a Web.config file. If you are using any other hosting environment, use
an App.config file.
In Visual Studio, the file named App.config is used to create the
final configuration file. The final name actually used for the
configuration depends on the assembly name. For example, an assembly
named "Cohowinery.exe" has a final configuration file name of
"Cohowinery.exe.config". However, you only need to modify the
App.config file. Changes made to that file are automatically made to
the final application configuration file at compile time.
In using an App.config, file the configuration system merges the
App.config file with content of the Machine.config file when the
application starts and the configuration is applied. This mechanism
allows machine-wide settings to be defined in the Machine.config file.
The App.config file can be used to override the settings of the
Machine.config file; you can also lock in the settings in
Machine.config file so that they get used. In the Web.config case, the
configuration system merges the Web.config files in all directories
leading up to the application directory into the configuration that
gets applied.

Web.Config is used for asp.net web projects / web services.
App.Config is used for Windows Forms, Windows Services, Console Apps and WPF applications
Your question isn't providing all the information as to where the gotcha's may lie for you.
Can you give us more info on what you are trying to do in terms of these config files?

Here's a link...
Problems with Web.config and App.config

Related

Class Library project in ASP.NET reading from web.config and not app.config

I have an ASP.NET solution in which there are two separate projects. One is normal UI and the other one is a class library which is being referred in the former.
In my class library project I am trying to read from App.Config file but it is reading from Web.Config file. I am using:
string url = Convert.ToString(ConfigurationManager.AppSettings["UpdateURL"]);
Initially, this key was written in Web.Config but now I have removed it from there and added this into the App.Config file. However, the compiler is still trying to fetch it from Web.Config. There may be something wrong I am doing. Please tell me what changes I need to make to get this done.
A web application will use web.config.
Keep your app settings inside that. A dll specic config file is not required.
A windows application will use App.config while a web application will use web.config.
If you use your dll in a windows or console application put the setting in app.config.
Dlls will always use the config file of the application they are loaded into.
If you want to have a dll specific config file, you will have to load it yourself.
This is normal behavior. Any referenced project will run under the parent's rules.
So if your class library had his own appConfig file, this one is no longer "valid". The Web.Config file has preference.
You need to copy the appSettings section of the class library into the Web.Config file.
You also have the option to "chain" the reading of the settings, it's like, the appSettings section of the web.Config might point to another .config file, but that's just a matter of taste and it's up to you.

How to replace web.config with transformed for debug file when running WAP in Visual Studio?

I want to replace web.config with transformed for debug config file when running in Visual Studio( I am NOT interesting in publishing project to some output directory). I want to keep original web.config in the root of web application in TFS, but on runtime (on IIS or Cassini) I want to use transformed debug version.
The best approach that I found so far (in Use Visual Studio web.config transform for debugging and ASP.NET Web Projects: web.debug.config & web.release.config ) is to use web.template.config as a master file, and web.config as derived transformed file.
I am not fully happy with it, because developers use to consider web.config as a master file.
I have an idea to output transformed file to ASP.Net temporary files directory, that ASP.NET used for cached files.
Does ASP.NET use web.config file from temporary cache folfder?
Will ASP.NET allow to have web.config file in cache directory different to web.config in original folder?
I found a couple properties, related to ASP.Net temporary files directory.
MSBuild tempDirectory property of the CompilationSection can be used to replace root folder of ASP.Net temporary files.
ClientBuildManager.CodeGenDir Property gives access to specific folder where current cache is located.
However I could not find, how to access path of the folder from MSBuild and will it allow me to output transformed web.config to cached folder.
Any suggestions/considerations will be appreciated.
The debug and release templates of the config file are used when you deploy the application. Can you use the publish option to publish the application to another virtual directory on your machine for debugging purposes? Then, you would have the transformed config file.

web.config with app.config in .dll

I just inherited a very old ASP.NET 2.0 web application.
In the application it has SEVERAL support class library projects. In the DataAccess class library, is an app.config (and setting.settings file) with a connection string named ConnString1.
I always thought that a .DLL couldn't have a app.config/setting.settings file (or at least you can include them but they won't be used), so this is what is confusing to me.
The web.config also has a connection string named ConnString1 with the same server login credentials, but a different server name.
When I run the application from Visual Studio DEBUG, it uses the connection string that is located in the app.config/settings file, and not the one defined in the web.config/machine.config.
I thought .DLLs wouldn't do this, but use the web.config instead?
However, when I pushed this application in RELEASE mode to our production server (in test website), it seems to be using the correct connection string in the web.config.
Can anyone explain this?
There's got to be something that is confusing you to think that the config file that's a part of that DLL is being used - as opposed to the applications (entry point's) config file (yourapp.exe.config or web.config). Maybe that conn string is hard-coded somewhere for the use in debug mode, e.g. by using conditional compilation via "#if DEBUG" preprocessor directive (so, maybe search for "#if DEBUG" across your solution to see if this particular thing is happening).
MSDN article about app settings
See the yellow "Note" in the section "Creating Application Settings at Design Time": "Because there is no configuration file model for class libraries, application settings do not apply for Class Library projects."
I found the issue:
In the above example, I am using the web.config/machine.config to set the connection string for the application.
If the connection string isn't defined in the web.config, it defaults to the machine.config. If the connection string isn't defined in the machine.config, it will use the app.config setting found in the .dll.
It's important to note, that placing the connection string in the machine.config, it must be defined in the correct Framework/CONFIG.
On my development machine, the connection string wasn't defined in the web.config but in the environment.config, but in the Framework64/CONFIG -- however, the application is compiled in 32-bit, thus, the reason for using Framework/CONFIG that did not have the connection string defined in it and causing it to default to the app.config in the class library.
I hope that explains that?
All config settings must be specified in the executable config file. For windows and console apps it's app.config, for web projects it's web.config.
Libraries can specify config settings, but you have to copy the settings to the executable's config file in order for the application to be able to read them.
Difference between Web.config, AppSettings.json and App.config
Web.config:
Web.config is needed when you want to host your application on IIS. Web.config is a mandatory config file for IIS to configure how it will behave as a reverse proxy in front of Kestrel. You have to maintain a web.config if you want to host it on IIS.
AppSetting.json:
For everything else that does not concern IIS, you use AppSetting.json.
AppSetting.json is used for Asp.Net Core hosting. ASP.NET Core uses the "ASPNETCORE_ENVIRONMENT" environment variable to determine the current environment. By default, if you run your application without setting this value, it will automatically default to the Production environment and uses "AppSetting.production.json" file. When you debug via Visual Studio it sets the environment to Development so it uses "AppSetting.json". See this website to understand how to set the hosting environment variable on Windows.
App.config:
App.config is another configuration file used by .NET which is mainly used for Windows Forms, Windows Services, Console Apps and WPF applications. When you start your Asp.Net Core hosting via console application app.config is also used.
Summary
The choice of the configuration file is determined by the hosting environment you choose for the service. If you are using IIS to host your service, use a Web.config file. If you are using any other hosting environment, use an App.config file.
See Configuring Services Using Configuration Files documentation
and also check out Configuration in ASP.NET Core.

Alternate to copying app.config settings to web.config?

I am working on a class library that uses Microsoft enteprise library logging application block. I have some settings in my app.config file.
Now, when a developer uses this library in a web applications, they have to copy entire config sections and appsettings part from the app.config to the web.config.
Is there something which can be done to bypass this step and makes it easier for developers to use my library without manual copy of all those settings.
Could try putting the repeated sections in an external config file, adding that file as a reference to the web.config project, and then referencing it in both the app.config & the web.config.
Enterprise Library has a visual config editor that you can use to load your web.config.
You can choose the settings you want and have them persisted to this file. Personally, I would not use Enterprise Library unless I had no choice. Simply too much glop code and overhead.

What are the Web.Debug.config and Web.Release.Config files for?

I just upgraded to Visual Studio 2010 and MVC 2.0 and I noticed the Web.config has two additional files attached to it? Are these files used to specify debug and release specific settings, so you don't clutter up the main Web.config?
Does it even make sense to place a connection string in the root Web.config file if I have a local and remote one in the debug and release Web.configs respectively?
Thanks!
It's the new Web.config transformation feature of Visual Studio 2010. More information here.
Edit:
Are these files used to specify debug and release specific settings, so you don't clutter up the main web.config?
It isn't limited to three files, you could (in theory) have as many files as you have environments. The "top level" Web.config provides a template of your web config. The files under it provide replacement values specific to that environment (like if you have different connection strings for local/stage/test/whatever).
Does it even make sense to place a connection string in the root web.config file if I have have a local and remote one in the debug and release web.configs respectively.
It would only make sense if it wasn't going to change between environments. Sounds like in your case it does so, in your case no, it would not make sense to leave it in the Web.config.
These are Web.config transformations files. From ASP.NET Web Deployment using Visual Studio: Web.config File Transformations:
There are two ways to automate the process of changing Web.config file settings: Web.config transformations and Web Deploy parameters. A Web.config transformation file contains XML markup that specifies how to change the Web.config file when it is deployed.
You can specify
different changes for specific build configurations and for specific
publish profiles. The default build configurations are Debug and
Release, and you can create custom build configurations. A publish
profile typically corresponds to a destination environment.
In case anyone is interested, here is something I wrote up to have a dynamic connection string per environment. I wanted to deploy the code to any environment (Dev, Test, Pre-Prod, Prod...) without having to worry about changing connection strings. I couldn't really find a good way to do this with Asp.Net MVC 4, so I came up with my own way to rely on a properties file per environment.
There may be a better solution, I come from a Wicket/Java background and recently started developing with MVC 4 so, it's possible a better solution exists. But here is a link to my question and answer for a dynamic connection string:
Asp.net MVC 4 dynamic connection string
That was something long needed in VS. Unfortunately there seems to be a problem with the implementation. For example consider this scenario (VS.2010 Ultimate, all SP):
Web.Config
No connectionStrings section
Full Membership User/Role/etc. Provider configuration using connectionStringName="test"
Web.Release.Config
No membership configuration (already specified in main web.config)
connectionStrings section including the CS named "test"
Web.Debug.Config
No membership configuration (already specified in main web.config)
connectionStrings section including the CS named "test"
When executing the application gives the following error:
The connection name 'test' was not found in the applications configuration or the connection string is empty.
In other words, because the connection string elements are in the Release/Debug designer files and used by configuration elements in the main (Web.config) file, it is unable to resolve it.

Resources