App.config AppSettings返回null

时间:2011-10-21 17:56:37

标签: c# wcf app-config

我有一个使用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总是返回null。我还注意到编译后app.config被重命名为Embedded_DCC_Client.dll.config。为什么无法找到我的appsettings?为什么它返回null?感谢。

3 个答案:

答案 0 :(得分:7)

听起来你正在尝试使用带有DLL的配置文件 - 这是行不通的。您需要在引用 WCF DLL的应用程序的应用程序文件中设置应用程序设置和WCF特定设置。该DLL将使用调用应用程序的配置文件。

换句话说:

MyWCF.dll - 这是你的WCF DLL。

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

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

答案 1 :(得分:3)

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

答案 2 :(得分:0)

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