【发布时间】:2019-09-06 08:26:08
【问题描述】:
我有一个名为 WebServiceProject 的解决方案。
在这个解决方案中,我有三个项目:
- 通用 (类库)
- 用户界面 (WinForms 项目)
- WebService (WCF 服务项目)
在 WebService 项目中,我有一个包含以下内容的 app.config 文件:
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<configSections>
<sectionGroup name="applicationSettings" type="System.Configuration.ApplicationSettingsGroup, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b72a5b561321d079">
<section name="WebService.Properties.Settings" type="System.Configuration.ClientSettingsSection, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b72a5b561321d079" requirePermission="false"/>
</sectionGroup>
</configSections>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5"/></startup>
<appSettings>
<add key="ConnectionString" value="mydatabase@localhost"/>
</appSettings>
<system.serviceModel>
<bindings/>
<client/>
</system.serviceModel>
</configuration>
在 WebService 项目中,我执行一些计划的例程,调用 Common 项目类,从 app.config 文件中读取 ConnectionString:
if (!string.IsNullOrEmpty(ConfigurationManager.AppSettings["ConnectionString"]))
{
// I do something
}
如果我单独启动 WebService 一切正常。
在 WinForms 项目 UserInterface 中,我有一个按钮,用于启动 Common 项目中的例程作为 WebService强>会。
但是如果我“Set as StartUp project”UserInterface 项目前面的代码ConfigurationManager.AppSettings["ConnectionString"] 会抛出错误,因为我没有指定@987654328 @ 在 UserInterface 项目中的app.config。
所以,我的问题是:如果我“Set as StartUp project”UserInterface,我如何从 WebService 项目事件中读取 ConnectionString 属性 项目?更一般地说,我如何从与已执行项目不同的另一个项目中读取 app.config 属性?
【问题讨论】:
-
您可以将 AppSettings 放在自己的文件中,并将相同的文件信息链接到多个配置文件。如这个问题:stackoverflow.com/questions/17437340/…
-
@DanielStackenland 谢谢,这个资源很有用!
标签: c# .net wcf app-config