使用客户端配置文件类库访问 web.config
我在类库中有一些代码是针对 .Net Framework 4 Client Profile 构建的。该代码访问消费应用程序的配置。对于客户端应用程序(WinForms 应用程序、控制台应用程序等),为 App.Config 获取正确的对象很容易:
ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
我的类库还需要适用于 Web 应用程序。访问 Web.Config 的正确方法也很简单:
WebConfigurationManager.OpenWebConfiguration("~");
问题是 WebConfigurationManager 是 System.Web 的一部分,而 System.Web 不能作为 .Net Framework 4 客户端配置文件的一部分提供。
有没有一种方法可以编写我的代码或构建我的项目,以便它在这两种情况下都能工作?它需要运行得足够好才能在仅安装了客户端配置文件的系统上访问 app.config。必要时它还需要能够访问 web.config。也许有某种方法可以在 system.web 或另一个程序集可用或需要时动态加载它?
I have some code in a class library built to target the .Net Framework 4 Client Profile. The code accesses the configuration of the consuming applications. For client apps (WinForms apps, console apps, etc.) getting the right object for App.Config is easy:
ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
My class library also needs to work for web applications. The proper way to get access to Web.Config is also easy:
WebConfigurationManager.OpenWebConfiguration("~");
The problem is that WebConfigurationManager is part of System.Web which is not available as part of the .Net Framework 4 Client Profile.
Is there a way I can write my code or structure my project so that it will work in both cases? It needs to run well enough to access app.config on systems that only have the client profile installed. It also needs to be able to access web.config when necessary. Perhaps there's some way I can dynamically load system.web or another assembly when it is available and when it is needed?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
非常有趣的问题;-)
如果您必须阅读 appSettings,只需使用需要
System.Configuration
的ConfigurationManager.AppSettings
并测试它在 Web 应用程序上的工作方式。如果它有效,那么你就可以开始了,最终的限制是你必须将你需要的所有内容放在appSettings
部分中,或者探索ConfigurationManger
类的其他类和选项, 不要使用类似WebConfigurationManager
的东西very interesting question ;-)
if you have to read appSettings just use
ConfigurationManager.AppSettings
which requiresSystem.Configuration
and test how it works on a web application. If it works you are good to go, with eventually the limitation that you have to put all you need in theappSettings
section, or explore other classes and options ofConfigurationManger
class and do not use anything likeWebConfigurationManager
正如 Davide Piras 所提到的,
ConfigurationManager.AppSettings[]
适用于 AppSettings 部分中的条目。在该部分之外,可以使用ConfigurationManager.GetSection()
。奇怪的是,
ConfigurationManager.GetSection()
的返回值与Configuration.GetSection()
的返回值不同。 ConfigurationManager 版本不会返回可以转换为AppSettingsSection
或其他内容的对象。相反,您必须将其转换为 System.Collections.Specialized.NameValueCollection。但是,只要您只想使用键/值字符串,它就足够好了。完整的代码如下所示:As mentioned by Davide Piras,
ConfigurationManager.AppSettings[]
will work for the entries that are in the AppSettings section. Outside of that section,ConfigurationManager.GetSection()
can be used.Strangely, the return value on
ConfigurationManager.GetSection()
isn't the same as the return value onConfiguration.GetSection()
. The ConfigurationManager version doesn't return an object that you can cast to anAppSettingsSection
or whatever. Instead you have to cast it to aSystem.Collections.Specialized.NameValueCollection
. However, as long as you only are looking to work with key/value strings it works well enough. The complete code looks like this: