简体   繁体   中英

What is <client> 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 ?

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:

<system.serviceModel>
  <services>
    <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" />
    </service>
  </services>
</system.serviceModel>

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

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

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.

By adding references to the System.Configuration assembly, you can also load the <client> section into memory if needed, and examine it there:

using System.Configuration;
using System.ServiceModel.Configuration;

.....

ClientSection clientSection = 
   (ClientSection)ConfigurationManager.GetSection("system.serviceModel/client");

But typically, you would only use the approach described by Gavin Schultz - let the ServiceModel handle the reading and interpreting of the section and give you a client proxy to use to call your service.

Marc

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM