.net – element of app.config


What is <client> element of app.config ?

Why it's added to client side when i add service reference?

How can i access this element programmatically ?

Best Solution

It's the counterpart to the <service> section on the WCF service side. It's primarily used to configure the endpoints used to connect the client and service together. In other words, it says where to connect to the service, and which binding to use.

For example if you have a WCF service hosted in IIS, you might have a section like this:

    <service name="MyService">
      <endpoint address="http://localhost:8080/MEX" binding="mexHttpBinding" bindingConfiguration="" name="mex" contract="IMetadataExchange" />
      <endpoint address="http://localhost:8111" binding="wsHttpBinding" bindingConfiguration="WS_HTTP_Secure" name="WS_HTTP_Endpoint" contract="IMyService" />

So on the client side, you would have a corresponding set of entries for the <client> section, e.g.

    <endpoint address="http://localhost:8111/" binding="wsHttpBinding" 
      bindingConfiguration="WS_HTTP_Endpoint_Binding" contract="MyService" name="WS_HTTP_Endpoint" />

There should generally be no need to access this section programmatically. Adding the service reference to your project would have added the proxy classes to your project though, and when using these classes you can specify the endpoint to use there. For example say you called your service classes "MyService", you may initialise it like this:

MyServicesClient client = new MyServicesClient("WS_HTTP_Endpoint");

There's no need to actually specify it in the constructor like this unless you have multiple endpoints though.