【问题标题】:App.config AppSettings return nullApp.config AppSettings 返回 null
【发布时间】:2011-12-12 19:00:27
【问题描述】:

我有一个使用 netTCP 端点的 WCF 客户端项目。该项目编译为另一个项目引用的 DLL。我使用 AppSettings 在本地和远程 ip 端点之间切换,如下所示:

    public EmbeddedClient()
    {
        //Grab ip to use: remote or local (used for simulator)
        String location = ConfigurationSettings.AppSettings["ipAddress"];
        String ip = ConfigurationSettings.AppSettings[location];

        //Default to localhost if no appsetting was found
        if (ip == null)
            ip = "localhost";

        String address = String.Format("net.tcp://{0}:9292/EmbeddedService", ip);

        //Setup the channel to the service...
        channelFactory = new ChannelFactory<IEmbeddedService>(binding, new EndpointAddress(address));

    }

我的 App.Config 是我的 AppSettings 和 WCF 端点:

  <?xml version="1.0" encoding="utf-8" ?>
  <configuration>
  <appSettings>
    <add key="ipAddress" value="local"/>
    <!-- Replace above value to "local" (Simulator) or "remote" (Harware)-->
    <add key="local" value="localhost"/>
    <add key="remote" value="192.168.100.42"/>
  </appSettings>

  <system.serviceModel>
      <!--WCF Endpoints go here--->
  </system.serviceModel>
  </configuration>

当我编译项目时,appsetting 总是返回一个空值。我还注意到 app.config 在编译后被重命名为 Embedded_DCC_Client.dll.config 之类的东西。为什么找不到我的应用设置?为什么它返回null?谢谢。

【问题讨论】:

  • 你能显示所涉及的(配置)文件吗?

标签: c# wcf app-config


【解决方案1】:

听起来您正在尝试将配置文件与 DLL 一起使用 - 这不起作用。您需要在引用 WCF DLL 的应用程序的应用程序文件中设置应用程序设置和 WCF 特定设置。 DLL 将使用调用应用程序的配置文件。

换句话说:

MyWCF.dll - 这是您的 WCF DLL。

MyApplication.exe - 这是一个引用 WCF.DLL 的应用程序。

您可以将应用设置和 system.serviceModel 设置放在 MyApplication.exe 的 app.config 文件中。然后 MyWCF.DLL 应该从该配置中读取值。

【讨论】:

  • 谢谢蒂姆。那行得通。我注意到 App.config 被重命名为 MyApplication.exe.config 之类的东西。一定要这样吗?
  • @RyanR - .NET 编译器默认执行此操作。我不知道是否有办法覆盖或更改它 - 我只是保持原样。
【解决方案2】:

应用程序设置文件是从启动的应用程序的上下文中加载的,因此它需要位于该项目中或从启动项目中引用。

【讨论】:

  • 感谢您的回答 Decker 但我只能选择一个。
  • 你不需要在引用的dll中有配置文件,只需启动exe即可。
【解决方案3】:

用于安装实用程序的文件夹应包含exe文件,支持dll和exe.config文件

【讨论】:

    猜你喜欢
    • 2011-10-22
    • 2017-09-18
    • 2015-06-05
    • 1970-01-01
    • 2016-01-19
    • 2011-12-17
    • 2018-11-09
    • 2011-09-13
    • 1970-01-01
    相关资源
    最近更新 更多