从单独的类库访问 web.config?
我正在寻找一种实现以下目标的好方法:
我有一个 Web 应用程序 (MVC 3),带有一个单独的类库,其中包含我正在制作的 CMS 的后端逻辑。此 CMS 使用 NHibernate 连接到数据库。我希望用户能够在其 web.config 文件中配置连接字符串(最终甚至是数据库的风格)。
我正在寻找一种从 web.config 文件获取连接字符串的好方法,即使 DLL 是完全独立的。这可能吗?我是否必须以某种方式将连接字符串传递到我的类库?或者当应用程序运行时我可以访问它吗?
如果我必须在我的 Web 应用程序中创建一些代码来将连接字符串传递到我的类库,我怎样才能使该代码尽可能可移植,这样我就不必为我的下一个 Web 应用程序再次编写它?
非常感谢您的任何想法。
I'm looking for a good way to achieve the following:
I have a web application (MVC 3), with a separate Class Library that contains the back-end logic of a CMS that I'm making. This CMS uses NHibernate to connect to a database. I want the user to be able to configure the connectionstring (and eventually even the flavour of the database) in their web.config file.
What I'm looking for is a good way to get the connection string from the web.config file, even though the DLL is completely separate. Is this possible? Will I have to pass my connection string to my class library somehow? Or will I be able to access it when the application runs?
If I have to create some code in my web application to pass the connection string to my Class Library, how can I make this code as portable as possible, so I won't have to write it again for my next webapp?
Thanks a lot for any ideas you have.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
您可以将连接字符串从网站传递到类库中的类。
与尝试直接从配置文件获取信息相比,这是一个更好的选择,否则您将依赖于具有确切正确密钥的现有配置文件(使测试类变得更加困难)。
请参阅这篇博文 对于反对直接访问配置的参数(这是很常见的做法,但不是最佳实践)。
You can pass in the connection string to the classes in the class library from the web site.
This is a better choice than trying to get the information directly from the configuration file, as otherwise you will have a dependency on the configuration file existing with the exact right key (making testing the class somewhat harder).
See this blog post for arguments against accessing configuration directly (which is very commonly done, but is not best practice).
您可以访问
System.Configuration.ConfigurationManager
来自您的类库。这将使您能够访问 AppSettings 和 ConnectionStrings。You can access
System.Configuration.ConfigurationManager
from your class library. That'll give you access to the AppSettings and ConnectionStrings.我的设置与我参与的自由开源项目完全相同。它包含“Core”类库中的所有内容(甚至是控制器和 Global.asax.cs)。
有很多有效的解决方案,我选择的一个是创建一个 Settings 类,它本质上是一组静态属性,其中包含:
注意:确保您的类库已添加 System.Configuration 作为引用。
在您的应用程序(从 HttpApplication 派生的类)内部传递设置,尽管没有什么可以阻止您将 NH 设置与设置类紧密耦合:
如果这对您有任何用处,源代码在这里。
I have exactly the same setup with a FOSS project I'm involved with. It contains everything (even the Controllers and Global.asax.cs) in the 'Core' class library.
There's plenty of valid solutions, the one I opted for was to create a Settings class which is essentially a set of static properties, inside which you have:
Note: make sure your class library has System.Configuration added as a reference.
Inside your Application (the class derived from HttpApplication) you pass the settings across, although there is nothing to stop you tighly coupling the NH setup with the settings class:
If this is any use to you, the source is here.
您可以使用
ConfigurationManager
类来访问 web.config 或 app.config 文件中的项目。但是,在您的类库中,请确保从使用者获取任何 appSettings 和/或 connectionString 设置的键名称(最好在构造函数中)。这避免了您选择消费者已经在其他地方使用的密钥名称的问题。You can use the
ConfigurationManager
class to access items in your web.config or app.config file. However, in your class library, be sure to take in the key name of any appSettings and/or connectionString settings from the consumer (preferably in the constructor). This avoids the problem of you choosing a key name that the consumer is already using elsewhere.由于您正在 MVC Web 应用程序中使用类库,因此类库也可以访问它。不需要额外的设置。尽管类库在构建时给出了一个单独的 dll,但它在当前项目中被引用。因此连接字符串也可供类库使用。
Since you are using the class library to the MVC web application, it is accessible to the class library also. No additional settings are needed. Even though the class library when built giving a separate dll, it is referenced in the current project. So the connection string will be available to the class library also.
我会使用像 Autofac 这样的东西来为您提供一些 IoC 实现,它可以为您的连接字符串存储设置界面。这将允许您在应用程序启动时从 web.config 设置值,或者在测试中将其设置为不同的值,而无需将类库耦合到 web.config。
I'd go with something like Autofac to give you some IoC implementation which can store a settings interface for your connection strings. This would allow you to setup the value from the web.config on application start, or to set it within tests to a different value without your Class Library ever having to be coupled to a web.config.
您可以将另一个项目中的新
现有项
添加到您的类库中。然后将Web 上的
Build Action
更改为Embedded Resource
,并将Copy to Output Directory
更改为Copy if newer
.config 文件。另一个项目中的Web.config
类库测试文件
You can add new
Existing item
from another project to your class library. Then changeBuild Action
toEmbedded Resource
andCopy to Output Directory
toCopy if newer
on theWeb.config
file.Web.config in another project
Class library test file