(2)WCF客户端调用

一、visual studion引用生成代理

用服务端是控制台程序

例子1

服务端的配置

  <system.serviceModel>
    <services>
      <service name="WcfServiceLibrary6.Service1" behaviorConfiguration="HelloServiceBehavior">
        <!--host这段可以测试时启动wcf测试客户端,运行后除了弹出测试客户端,也可以在浏览器中用此address访问-->
        <host>
          <baseAddresses>
            <add baseAddress = "http://localhost:8733/" />
          </baseAddresses>
        </host>
        <endpoint address="" binding="basicHttpBinding" contract="WcfServiceLibrary6.IService1" />
      </service>
    </services>
    <!--元数据-->
    <behaviors>
      <serviceBehaviors >
        <behavior name="HelloServiceBehavior">
          <serviceMetadata httpGetEnabled="True"/>
        </behavior>
      </serviceBehaviors>
    </behaviors>
  </system.serviceModel>

 用右键添加服务引用的方式

最后在客户端配置文件会自动添加xml

    <system.serviceModel>
        <bindings>
            <basicHttpBinding>
                <binding name="BasicHttpBinding_IService1" />
            </basicHttpBinding>
        </bindings>
        <client>
            <endpoint address="http://localhost:8733/" binding="basicHttpBinding"
                bindingConfiguration="BasicHttpBinding_IService1" contract="ServiceReference1.IService1"
                name="BasicHttpBinding_IService1" />
        </client>
    </system.serviceModel>

例子2

把例子1的服务端终结点添加一个address属性

<endpoint address="Service1" binding="basicHttpBinding" contract="WcfServiceLibrary6.IService1" />

客户端还是引用 http://localhost:8733/

生成xml,发现地址多出了一个Service1

 

例子3

把例子1的服务端终结点添加一个address属性

<endpoint address="http://localhost:22222/" binding="basicHttpBinding" contract="WcfServiceLibrary6.IService1" />

客户端引用依然是  http://localhost:8733/

生成xml,生成的地址变成了终结点的地址

    <system.serviceModel>
        <bindings>
            <basicHttpBinding>
                <binding name="BasicHttpBinding_IService1" />
            </basicHttpBinding>
        </bindings>
        <client>
            <endpoint address="http://localhost:22222/" binding="basicHttpBinding"
                bindingConfiguration="BasicHttpBinding_IService1" contract="ServiceReference1.IService1"
                name="BasicHttpBinding_IService1" />
        </client>
    </system.serviceModel>

二、使用SvcUtil生成代理

猜你喜欢

转载自www.cnblogs.com/buchizaodian/p/10000642.html
今日推荐