Where is my app.config file? - asp.net

I have built a server api that consists aith 3 projects:
Api(an api project) that calles BL(class library) that calls Dal(class library).
Very standard.
It works perfectly locally.
Now when I publish it, the app.config of the BL disappears.
Why is it? Where can I store my configurable parameters?
Thank you very much. Tal

On your main project ASP.NET, use the Web.Config instead of App.Config to make it work.

app.config is only the name during development, once the project is built the app.config is copied to .config and this is what is used by the application when running
An asp.net application reads config from web.config so if you have an app.config it implies that your project is not a web project but a standard library or executable.
if it is a library (.dll) then you should place your config in the web.config (if the library is used by asp.net) or in the .config if its a standard executable.
Ideally your libraries should not read settings as this creates a hidden coupling between the library and config files, and it would be better to provide those configuration parameters to the classes that need them in the library externally, this then leaves the application using the library free to store them where ever is most appropriate.
How is the consumer of your library going to know that they need to add SettingX to the appsettings of their configuration file? Better for the library to require the setting value directly. So if your DAL needs a connection string then the class which wants a connection string should ask for it in their constructor. Then the application using it can get it from settings (or whereever) and pass it to the library., and the consumers have visibility of the dependency on the connection string. If the library reads it from config the consumer has no way to know that this is something they need add to the settings
So in a web app the startup would read the settings from web.config and pass them to your library but some other app using the same library could store them in a database if they wanted.

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.

Enterprise Library Class Library App.Config

I have a class library whcih is a Wrapper class for Enterprise Library Logging Application Block. There I kept an App.Config configuration file. Where all the Enterprise Library related configurations are present.
Now I refered this Class Library in a ASP.NET Web Application. Problem I am getting is: The ASP.NET Web Application is unable to read the App.Config file to fetch Configuration Information. And I am getting below Exception:
Config files are per appdomain, not per binary. If you've got an .exe, it'll looking .config, in a web app, it looks in web.config. There are some tricks you can use, but they all involved putting something in the main config file - you can't avoid it for the most part.
The easiest thing to do would be to put the information in the web.config file. There are some other options, but they get increasingly more complicated. What version of Entlib are you using? If using 5.0, there's the programmatic configuration support (the configuration builder stuff) that makes is reasonably easy to set up entlib settings through code - you could use those in your library if the configuration is actually fixed.
Although you mention you're wrapping the logging block, but the screenshot shows the exception block. Is your main app going to be using Entlib as well, or just through your wrappers?

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.

ASP.NET solution with class library project

I have an solution in VS 2008 which contains two class library projects and an ASP.NET web site. The ASP.NET site references the class libraries and one of the libraries contains a LINQ To SQL item.
My question is with regards to the app.config in the class library which contains the connection string for the database. When I build the project, this app.config isn't within the build directory and this means I can't dynamically change the connection string for the deployed project.
What am I doing wrong here, how can I have these settings deployed too so I can make changes to the connection string?
Thanks in advance,
Martin.
This caused me a bit of confusion at first as well.
You might think that the class library uses the app.config file that's contained in it's own project but it doesn't. It uses the config file of the project that is referencing it.
So what you need to do is look for the <appSettings/> tag inside the web.config file of your ASP.Net project and change it to <appSettings></appSettings> And add the <add ... /> tags that are contained in the app.config file of the library project. You don't need to change anything in your code for the ConfigurationManager class to figure this out. It knows where to look automagically.
Hope that makes sense.
You can edit the Web.config file in the final product. Configuration APIs normally will get configuration data from the primary configuration file of the application which, in case of ASP.NET apps is the Web.config and for client applications is Myfile.exe.config. It's important to know that class libraries in the project usually will not have their separate configuration file like MyClassLib.dll.config (unless you manually refer to the specific file).
To overcome the problem of connection string, here is the trick
Inside ur class library declare module that has got two properties, one is a setter and the other is a getter, and make them public.
Inside ur website project, go to the global file, and under both session start and application start call the setter property that u declared previously, and assign it the connection string that is located in ur web.config, now the connection string will be available in the website general scope and the value exists as long as ur session credential not expired.
Copy the connectionString section from your library's app.config file to your web.config file. Change the actual connection string from your development to your production server as necessary. The ConfigurationManager class that LINQ2SQL uses to obtain the connection string will look in the web.config file for the appropriately named connection string and use it if it exists.
If you want to have different settings for development vs production, use the Web Deployment Project. Download here. From Microsoft's description:
Visual Studio 2008 Web Deployment
Projects provide additional
functionality to build and deploy Web
sites and Web applications in Visual
Studio 2008. This add-in provides a
comprehensive UI to manage build
configurations, merging, and using
pre-build and post-build tasks with
MSBuild.

Resources