【问题标题】:Read config properties from another project in the same solution C#从同一解决方案 C# 中的另一个项目读取配置属性
【发布时间】: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 projectUserInterface 项目前面的代码ConfigurationManager.AppSettings["ConnectionString"] 会抛出错误,因为我没有指定@987654328 @ 在 UserInterface 项目中的app.config

所以,我的问题是:如果我“Set as StartUp projectUserInterface,我如何从 WebService 项目事件中读取 ConnectionString 属性 项目?更一般地说,我如何从与已执行项目不同的另一个项目中读取 app.config 属性?

【问题讨论】:

  • 您可以将 AppSettings 放在自己的文件中,并将相同的文件信息链接到多个配置文件。如这个问题:stackoverflow.com/questions/17437340/…
  • @DanielStackenland 谢谢,这个资源很有用!

标签: c# .net wcf app-config


【解决方案1】:

只需读取文件并将其解析为XML。然后检索您的value 阅读感兴趣的key

【讨论】:

  • 我正在尝试使用System.Configuration 程序集。问题是我不知道如何从另一个项目app.config获取该属性。
【解决方案2】:

我发现这个链接非常有用:

AppSettings in App or Web Config Using a Linked File

感谢@Daniel Stackendland

【讨论】:

    【解决方案3】:

    您可以将您的应用程序设置从您的 web 服务项目放入您的 UI 项目的 app.config 中。

    【讨论】:

    • 在生产中我将运行这两个项目。所以,我想要一个定义配置设置的app.config 文件。
    • 是的,这正是你所做的。您将带有 webservice-project 值的行放入您的 UI-project 的配置中
    • 但我不想复制配置。我想要一个可以使用config 的文件。并且这个文件应该放在WebService项目中。
    猜你喜欢
    • 2017-07-19
    • 1970-01-01
    • 1970-01-01
    • 2011-07-31
    • 1970-01-01
    • 1970-01-01
    • 2022-11-30
    • 1970-01-01
    • 2016-10-29
    相关资源
    最近更新 更多