关于WCF的引用,添加服务和添加web服务的区别

本章内容主要是根据我做的实验来阐述这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)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值