Sharing AppSettings and ConnectionStrings between a console and a web app

ぃ、小莉子 提交于 2019-12-11 17:33:02

问题


I have a multi-tiered ASP.Net MVC 3 application in which different layers need to share some settings, and I am wondering how to best achieve this.

For now, all my AppSettings are in the Web.config file, but I just added a new project to my solution to create a console application that will be called every night buy the Windows Task Scheduler to perform routine maintenance tasks. This console application will be using the same database, repository layer, service layer, etc. as the web application, and both applications should share the same settings for the most part. I would like to avoid duplicating the settings in two files (app.config and web.config).

Also, I want these settings to be available to the service layer, which is in a separate project in the same solution.

I have read the following post, but I don't fully understand the solution, and I am not sure it is the best way to do this.

Sharing Config settings between Web App and Console App

I also read the article linked to by that post, and I don't find the solution very elegant.

I am using VS 2010 and EF 4.3.1.


回答1:


If you're simply interested in accessing the AppSettings of your web.config file from a console app, that's simple enough. The only real annoyance is that since your console app won't be running in a web context, you will have to know in advance what your site is called in IIS. This being your own application shouldn't be a challenge.

Just use System.Web.Configuration.WebConfigurationManager to open and access the web.config of the specified site.




回答2:


The idea is that you could put the settings into a common .config shared between all your projects as a link instead of putting it in all projects. The solution is outlined in this answer. Also checkout the configSource property on MSDN if you are not aware about the possibility of externalizing configuration elements into a separate file.



来源:https://stackoverflow.com/questions/13260006/sharing-appsettings-and-connectionstrings-between-a-console-and-a-web-app

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!