在控制台和Web应用程序之间共享AppSettings和ConnectionStrings [英] Sharing AppSettings and ConnectionStrings between a console and a web app

查看:51
本文介绍了在控制台和Web应用程序之间共享AppSettings和ConnectionStrings的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个多层ASP.Net MVC 3应用程序,其中不同的层需要共享一些设置,我想知道如何最好地实现这一点.

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.

现在,我所有的AppSettings都在Web.config文件中,但是我只是在解决方案中添加了一个新项目,以创建一个控制台应用程序,该应用程序每晚都会被调用,购买Windows Task Scheduler来执行日常维护任务.该控制台应用程序将使用与Web应用程序相同的数据库,存储库层,服务层等,并且两个应用程序在大多数情况下应共享相同的设置.我想避免将设置复制到两个文件(app.config和web.config)中.

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.

在Web应用程序和控制台应用程序之间共享配置设置

我还阅读了该文章所链接的文章,但我认为解决方案并不十分优雅.

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

我正在使用VS 2010和EF 4.3.1.

I am using VS 2010 and EF 4.3.1.

推荐答案

这个想法是,您可以将设置放入所有项目之间共享的公共.config中作为链接,而不是将其放置在所有项目中. this answer 中概述了该解决方案.如果需要,还可以检出 configSource property on MSDN 尚不知道将配置元素外部化为单独文件的可能性.

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.

这篇关于在控制台和Web应用程序之间共享AppSettings和ConnectionStrings的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆