是否可以从控制台应用程序加载 web-config?

Eri*_*son 5 c# configuration web-applications console-application

我的问题很简单。我有一个第三方库,它假设它将在网站中使用(需要 web.config 中的特殊配置部分)。我想在 C# 控制台应用程序中使用这个库。有可能的?

我已经尝试过 WebConfigurationManager.OpenWebConfiguration(); 将所有内容复制到 app.config 中,但它不起作用。我收到的错误是 ConfigurationErrorsException 说明 An error occurred creating the configuration section handler for ...: Could not load type 'Configuration.RenderingSection' from assembly 'System.Configuration, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a'.

Roh*_*ats 0

app.config代替使用。

与 web.config 完全相同的语法、选项等,但适用于控制台和 WinForms 应用程序。

另外,看这里,虽然我还没有测试过,但它似乎对这个人有用。但是,如果您的项目与网络无关,我仍然建议使用 app.config。

  • 对于相同的设置使用大量 .config 文件并不是一个好主意。当控制台应用程序与网站一起部署时,明智的做法是将所有设置集中在一个文件中。只需考虑依赖控制台应用程序来启动定期批处理的 Web 系统的情况。 (5认同)