blueskyc's blog
永不放弃

 本章内容主要是根据我做的实验来阐述这2种添加服务针对WCF的不同之处,我们按照示例一步一步来看。

 

如下是工程的结构:

该WCF服务是通过控制台程序(Host)以自宿的形式发布的,绑定使用wsHttpBinding。我们在Client端分别添加

服务引用(add service references)和添加Web引用(add Web Reference )来引用WCF服务。

 

以下是客户端的代码,分别使用添加服务引用和添加Web引用的服务代理来调用WCF的方法: 

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Yingchao.Client.localhost;
using Yingchao.Client.ServiceReference1;

namespace Yingchao.Client
{
    class Program
    {
        static void Main(string[] args)
        {
            // add service reference's proxy
            Service1Client client = new Service1Client();
            Console.WriteLine(client.GetData(111));

            // add web reference's proxy
            Service1 s = new Service1();
            Console.WriteLine(s.GetData(1234, true));

            Console.Read();
        }
    }
}

 

客户端配置文件:

<?xml version="1.0" encoding="utf-8"?>
<configuration>
    <configSections>
        <sectionGroup name="applicationSettings" type="System.Configuration.ApplicationSettingsGroup, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" >
            <section name="Yingchao.Client.Properties.Settings" type="System.Configuration.ClientSettingsSection, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />
        </sectionGroup>
    </configSections>
  <system.serviceModel>
    <client>
      <!-- 添加服务引用时自动生成 -->
      <endpoint address="http://localhost:8732/service" binding="wsHttpBinding"
          contract="ServiceReference1.IService1" name="WSHttpBinding_IService1">
        <identity>
          <dns value="localhost" />
        </identity>
      </endpoint>
    </client>
  </system.serviceModel>
    <applicationSettings>
      <!-- 添加Web服务引用时自动生成 -->
        <Yingchao.Client.Properties.Settings>
            <setting name="Yingchao_Client_localhost_Service1" serializeAs="String">
                <value>http://localhost:8732/service</value>
            </setting>
        </Yingchao.Client.Properties.Settings>
    </applicationSettings>
</configuration>

服务端配置文件:

<?xml version="1.0" encoding="utf-8" ?>
<configuration>

  <system.web>
    <compilation debug="true" />
  </system.web>
    <system.serviceModel>
    <services>
      <service name="Yingchao.Service.Service1">
        <host>
          <baseAddresses>
            <add baseAddress = "http://localhost:8732/service" />
          </baseAddresses>
        </host>
        <!-- Service Endpoints -->
        <!-- 除非完全限定,否则地址将与上面提供的基址相关 -->
        <endpoint address ="" binding="wsHttpBinding" contract="Yingchao.Contract.IService1">
          <identity>
            <dns value="localhost"/>
          </identity>
        </endpoint>
        <!-- Metadata Endpoints -->
        <!-- 元数据交换终结点供相应的服务用于向客户端做自我介绍。 -->
        <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange"/>
      </service>
    </services>
    <behaviors>
      <serviceBehaviors>
        <behavior>
          <serviceMetadata httpGetEnabled="True"/>
          <serviceDebug includeExceptionDetailInFaults="False" />
        </behavior>
      </serviceBehaviors>
    </behaviors>
  </system.serviceModel>

</configuration>

 

我们启动服务后,运行客户端,我们看看结果是什么:

 

我们看到这里添加Web服务代理调用WCF的方法的结果没有显示出来,而是出现了"操作超时"错误。

 

那我们更改服务端配置文件的绑定:wsHttpBinding 改成 basicHttpBinding,编译后更新引用的服务。

然后再次运行客户端,我们看看结果:

 我们看到这次2个引用服务都成功调用。可见添加Web服务应该只能使用basicHttpBinding,也许微软是为了向前兼容留下的。

然后,分别添加的服务引用生成的Reference.cs里面生成的代码也不一样。添加服务引用更偏向WCF规则。

 

我查资料也发现跟我想的差不多.(http://social.microsoft.com/Forums/zh-CN/xmlwebserviceszhchs/thread/808d870b-49f1-47ac-b105-4beb580bcec6)

 

posted on 2012-04-28 00:18  Cherry Chen  阅读(10441)  评论(0编辑  收藏  举报