简体   繁体   中英

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, eg 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM