建立WCF的客户端

 

【WCF系列】之三:建立WCF的客户端

  在前一篇文章中,我们介绍了如何构建一个WCF的基础服务架构,还以一个案例来演示了架构的搭建过程。服务既然已经发布,接下来我们就需要有一个客户端来访问这个服务了。

  搭建WCF的客户端,最重要就是要遵循服务端的契约,客户端通过代理(Proxy)来访问服务端点,而并不关心服务端的具体实现。代理要做的就是通过与服务端确认通讯协议,并通过信道(channels)交换数据。在服务端,ServiceHost会为每个端点创建一个信道侦听器,由侦听器产生信道。而客户端代理则产生一个信道发生器,产生客户端信道。只有在服务端信道和客户端信道一致的情况下,双方才允许进行通讯。信道会对通讯过程进行监控,保障通讯的安全性。

  为了简单的完成一个WCF客户端,微软为我们准备了一个小工具,就是Service Model Metadata Utility。这个工具能帮你快速的从服务地址中生成客户代理和配置文件。我们还是以前一篇的例子来演示。

  首先允许服务器端程序,等服务启动后。在VS2008命令行窗口中输入如下命令:svcutil.exe http://localhost:8080/MyWCF 回车后得到如下页面。

从上面画面中可以看到,wcf为客户端生成了一个客户代理类TemperatureService.cs和一个配置文件output.config。客户端只需要整合这两个文件就可以与服务端通讯了。我们来看看这两个文件的内容:

TemperatureService.cs

    //------------------------------------------------------------------------------
    // <auto-generated>
    //     此代码由工具生成。
    //     运行时版本:2.0.50727.3053
    //
    //     对此文件的更改可能会导致不正确的行为,并且如果
    //     重新生成代码,这些更改将会丢失。
    // </auto-generated>
    //------------------------------------------------------------------------------
    [System.CodeDom.Compiler.GeneratedCodeAttribute("System.ServiceModel", "3.0.0.0")]
    [System.ServiceModel.ServiceContractAttribute(ConfigurationName = "IContract")]
    public interface IContract
    {
        [System.ServiceModel.OperationContractAttribute(Action = "http://tempuri.org/IContract/GetFahrenheit", ReplyAction = "http://tempuri.org/IContract/GetFahrenheitResponse")]
        float GetFahrenheit(float celsius);
    }
    [System.CodeDom.Compiler.GeneratedCodeAttribute("System.ServiceModel", "3.0.0.0")]
    public interface IContractChannel : IContract, System.ServiceModel.IClientChannel
    {
    }
    [System.Diagnostics.DebuggerStepThroughAttribute()]
    [System.CodeDom.Compiler.GeneratedCodeAttribute("System.ServiceModel", "3.0.0.0")]
    public partial class ContractClient : System.ServiceModel.ClientBase<IContract>, IContract
    {
        public ContractClient()
        {
        }
        public ContractClient(string endpointConfigurationName) :
            base(endpointConfigurationName)
        {
        }
        public ContractClient(string endpointConfigurationName, string remoteAddress) :
            base(endpointConfigurationName, remoteAddress)
        {
        }
        public ContractClient(string endpointConfigurationName, System.ServiceModel.EndpointAddress remoteAddress) :
            base(endpointConfigurationName, remoteAddress)
        {
        }
        public ContractClient(System.ServiceModel.Channels.Binding binding, System.ServiceModel.EndpointAddress remoteAddress) :
            base(binding, remoteAddress)
        {
        }
        public float GetFahrenheit(float celsius)
        {
            return base.Channel.GetFahrenheit(celsius);
        }
    }

     从这个文件可以看到,客户端实际上是继承了两个接口,System.ServiceModel.ClientBase<IContract>和IContract。其中IContract是服务端契约的接口。

output.config
<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <system.serviceModel>
    <bindings>
      <basicHttpBinding>
        <binding name="BasicHttpBinding_IContract" closeTimeout="00:01:00"
            openTimeout="00:01:00" receiveTimeout="00:10:00" sendTimeout="00:01:00"
            allowCookies="false" bypassProxyOnLocal="false" hostNameComparisonMode="StrongWildcard"
            maxBufferSize="65536" maxBufferPoolSize="524288" maxReceivedMessageSize="65536"
            messageEncoding="Text" textEncoding="utf-8" transferMode="Buffered"
            useDefaultWebProxy="true">
          <readerQuotas maxDepth="32" maxStringContentLength="8192" maxArrayLength="16384"
              maxBytesPerRead="4096" maxNameTableCharCount="16384" />
          <security mode="None">
            <transport clientCredentialType="None" proxyCredentialType="None"
                realm="" />
            <message clientCredentialType="UserName" algorithmSuite="Default" />
          </security>
        </binding>
      </basicHttpBinding>
    </bindings>
    <client>
      <endpoint address="
http://localhost:8080/MyWCF" binding="basicHttpBinding"
          bindingConfiguration="BasicHttpBinding_IContract" contract="IContract"
          name="BasicHttpBinding_IContract" />
    </client>
  </system.serviceModel>
</configuration>

     output.config文件则定义了和服务端匹配的endpoint,有了这两个文件,最后要做的事情就是将其整合到客户端程序中,其步骤如下:

     1)建立一个空白解决方案,方案的名称叫MyWCFClient,添加一个名称为MyWCF.Client的ConsoleApplication项目。在该项目中添加System.ServiceModel的引用。

     2)另外在方案中再添加一个类库项目,项目名称叫MyWCF.ClientBase,为项目添加System.ServiceModel的引用,类名改为ClientBase。将TemperatureService.cs文件中的代码拷贝到ClientBase中的命名空间引用下。

     3)在项目MyWCF.Client项目中添加一个App.config文件,将output.config文件的代码粘贴到该文件中覆盖原来的代码。并为该项目添加对MyWCF.ClientBase项目和System.ServiceModel的引用。

     4)在项目MyWCF.Client的Main方法中添加如下代码。

using System;
using System.Collections.Generic;
using System.Text;
using MyWCF.ClientBase;
namespace MyWCF.Client
{
    class Program
    {
        static void Main(string[] args)
        {
            ContractClient CC = new ContractClient();
            float result = CC.GetFahrenheit(23.4f);
            Console.WriteLine("华氏温度为{0}度!", result);
        }
    }
}

     5)客户端代码编写完成,此时请首先运行服务端的MyWCF.Hosting项目,将服务端启动。

     6)回到客户端的MyWCF.Client项目,按Ctrl + F5执行程序,结果如下:

                 

     至此,客户端已成功从服务端获得数据。我们再来看看客户端的架构:

                  

     由此可见,客户端由两部分组成,一是用于同服务端确认通讯的代理层MyWCF.ClientBase,二是客户端的业务逻辑层MyWCF.Client。实际上,只要服务端确定后,我们就可以使用工具轻松的生成客户端架构。当然,这只是WCF的一个最为简单的示例,目的是使大家对WCF的各个部件有一个大致的了解,对架构有一个简单认识。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值