WCF框架基础(三)

1.首先生成配置文件。服务代理,首先添

加服务引用在地址栏里输入上文服务端创建的Uri地址

2.点击确定将生成配置文件

<system.serviceModel>
    <bindings>
      <wsHttpBinding>
        <binding name="WSHttpBinding_IService" closeTimeout="00:01:00"
          openTimeout="00:01:00" receiveTimeout="00:10:00" sendTimeout="00:01:00"
          bypassProxyOnLocal="false" transactionFlow="false" hostNameComparisonMode="StrongWildcard"
          maxBufferPoolSize="524288" maxReceivedMessageSize="65536" messageEncoding="Text"
          textEncoding="utf-8" useDefaultWebProxy="true" allowCookies="false">
          <readerQuotas maxDepth="32" maxStringContentLength="8192" maxArrayLength="16384"
            maxBytesPerRead="4096" maxNameTableCharCount="16384" />
          <reliableSession ordered="true" inactivityTimeout="00:10:00"
            enabled="false" />
          <security mode="Message">
            <transport clientCredentialType="Windows" proxyCredentialType="None"
              realm="" />
            <message clientCredentialType="Windows" negotiateServiceCredential="true"
              algorithmSuite="Default" />
          </security>
        </binding>
      </wsHttpBinding>
    </bindings>
    <client>
      <endpoint address="http://localhost:8000/ServiceModelSamples/Service/CalculatorService"
        binding="wsHttpBinding" bindingConfiguration="WSHttpBinding_IService"
        contract="ServiceReference.IService" name="WSHttpBinding_IService">
        <identity>
          <userPrincipalName value="PC-201206011335\Administrator" />
        </identity>
      </endpoint>
    </client>
  </system.serviceModel>

3.使用WCF客户端

//创建端终结点  ServiceReference属性在配置文件有终节点处查找
EndpointAddress epAddress = new EndpointAddress("http://localhost:8000/ServiceModelSamples/Service/CalculatorService");
ServiceReference.ServiceClient client = new ServiceReference.ServiceClient(new WSHttpBinding(),epAddress);

至此就可以调用服务端定义的服务

  double x=client.Add(3.2, 2.1);
原文地址:https://www.cnblogs.com/akingyao/p/2673300.html