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

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

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

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

WCF随录(3)—建立WCF的客户端

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




[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是服务端契约的接口。


    <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)建立一个ConsoleApplication的解决方案,方案的名称叫MyWCF,项目的名称为MyWCF.MyWCF.Client。在该项目中添加System.ServiceModeld的引用。

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

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

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

using System;
using MyWCF.ClientBase;

namespace MyWCF.MyWCFClient
{
    
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.ConsoleApplication1项目,按Ctrl + F5执行程序,结果如下:

WCF随录(3)—建立WCF的客户端

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

WCF随录(3)—建立WCF的客户端

     由此可见,客户端由两部分组成,一是用于同服务端确认通讯的代理层MyWCF.ClientBase,二是客户端的业务逻辑层MyWCF.Client。实际上,只要服务端确定后,我们就可以使用工具轻松的生成客户端架构。当然,这只是WCF的一个最为简单的势力,目的是使大家对WCF的各个部件有一个大致的了解,对架构有一个简单认识。在后面的文章中将进一部从每一个细节入手,深入讲解WCF技术。

相关文章:

  • 2022-12-23
  • 2021-08-11
  • 2022-01-16
  • 2021-08-01
  • 2022-12-23
猜你喜欢
  • 2021-06-18
  • 2021-10-04
  • 2022-12-23
  • 2022-02-11
  • 2021-08-07
相关资源
相似解决方案