App.config AppSettings返回null

Rya*_*n R 3 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));

    }
Run Code Online (Sandbox Code Playgroud)

我的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>
Run Code Online (Sandbox Code Playgroud)

当我编译项目时,appsetting总是返回null.我还注意到编译后app.config被重命名为Embedded_DCC_Client.dll.config.为什么无法找到我的appsettings?为什么它返回null?谢谢.

Tim*_*Tim 7

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

换一种说法:

MyWCF.dll - 这是您的WCF DLL.

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

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