当前位置: 动力学知识库 > 问答 > 编程问答 >

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

问题描述:

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.

分享给朋友:
您可能感兴趣的文章:
随机阅读: