Ryan R
Ryan R

Reputation: 8461

App.config AppSettings return null

I have a project that is a WCF client using netTCP endpoints. The project compiles into a DLL that is referenced by another project. I use AppSettings to switch between local and remote ip endpoints like so:

    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));

    }

My App.Config is where I have my AppSettings and WCF endpoints:

  <?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>

When I compile the project the appsetting always returns a null. I also noticed that app.config is renamed to something like Embedded_DCC_Client.dll.config after compiling. Why is it not able to find my appsettings? Why is it returning null? Thanks.

Upvotes: 3

Views: 11668

Answers (3)

Bala
Bala

Reputation: 55

The folder which is used to Install utility should contain the Exe file, supporting dll and exe.config file

Upvotes: 0

Decker97
Decker97

Reputation: 1653

The app settings file is loaded from the context of the application that is started, so it needs to either be in that project or referenced from the startup project.

Upvotes: 3

Tim
Tim

Reputation: 28530

It sounds like you're trying to use a config file with the DLL - that won't work. You need to set your app settings and WCF-specific settings in the app file of the application that references the WCF DLL. Th DLL will use the config file of the calling application.

In other words:

MyWCF.dll - this is your WCF DLL.

MyApplication.exe - this is an application that references WCF.DLL.

You would put your app settings and system.serviceModel settings in the app.config file of MyApplication.exe. MyWCF.DLL should then read the values from that config.

Upvotes: 7

Related Questions