WCF客户端具体搭建方法解析

开发 开发工具
WCF客户端的正确搭建对于初学者来说是非常重的,他们需要熟练的掌握这一应用技术,才能方便将来的应用,并提高我们的开发能力。

我们在一系列的文章中为大家详细介绍了有关WCF的相关基础内容,相信大家应该可以通过我们介绍的内容能够充分掌握这一工具的应用方法。在这里我们继续对WCF客户端的相关应用方法做一个介绍。#t#

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

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

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

 

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

 

  1. TemperatureService.cs  
  2. // < auto-generated> 
  3. // 此代码由工具生成。  
  4. // 运行时版本:2.0.50727.3053  
  5. //  
  6. // 对此文件的更改可能会导致不正确的行为,并且如果  
  7. // 重新生成代码,这些更改将会丢失。  
  8. // < /auto-generated>   
  9. [System.CodeDom.Compiler.GeneratedCodeAttribute
    ("System.ServiceModel", "3.0.0.0")]  
  10. [System.ServiceModel.ServiceContractAttribute
    (
    ConfigurationName = "IContract")]  
  11. public interface IContract  
  12. {  
  13. [System.ServiceModel.OperationContractAttribute(Action = 
    "http://tempuri.org/IContract/GetFahrenheit"ReplyAction = 
    "http://tempuri.org/IContract/GetFahrenheitResponse")]  
  14. float GetFahrenheit(float celsius);  
  15. }  
  16. [System.CodeDom.Compiler.GeneratedCodeAttribute
    ("System.ServiceModel", "3.0.0.0")]  
  17. public interface IContractChannel : IContract, System.
    ServiceModel.IClientChannel  
  18. {  
  19. }  
  20. [System.Diagnostics.DebuggerStepThroughAttribute()]  
  21. [System.CodeDom.Compiler.GeneratedCodeAttribute("System.
    ServiceModel", "3.0.0.0")]  
  22. public partial class ContractClient : System.ServiceModel.
    ClientBase
    < IContract>, IContract  
  23. {  
  24. public ContractClient()  
  25. {  
  26. }  
  27. public ContractClient(string endpointConfigurationName) :  
  28. base(endpointConfigurationName)  
  29. {  
  30. }  
  31. public ContractClient(string endpointConfigurationName, string remoteAddress) :  
  32. base(endpointConfigurationName, remoteAddress)  
  33. {  
  34. }  
  35. public ContractClient(string endpointConfigurationName, 
    System.ServiceModel.EndpointAddress remoteAddress) :  
  36. base(endpointConfigurationName, remoteAddress)  
  37. {  
  38. }  
  39. public ContractClient(System.ServiceModel.Channels.Binding binding, 
    System.ServiceModel.EndpointAddress remoteAddress) :  
  40. base(binding, remoteAddress)  
  41. {  
  42. }  
  43. public float GetFahrenheit(float celsius)  
  44. {  
  45. return base.Channel.GetFahrenheit(celsius);  
  46. }  

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

output.config

  1. < ?xml version="1.0" encoding="utf-8"?> 
  2. < configuration> 
  3. < system.serviceModel> 
  4. < bindings> 
  5. < basicHttpBinding> 
  6. < binding name="BasicHttpBinding_IContract" closeTimeout="00:01:00" 
  7. openTimeout="00:01:00" receiveTimeout="00:10:00" sendTimeout="00:01:00" 
  8. allowCookies="false" bypassProxyOnLocal="false" 
    hostNameComparisonMode="StrongWildcard" 
  9. maxBufferSize="65536" maxBufferPoolSize="524288" 
    maxReceivedMessageSize="65536" 
  10. messageEncoding="Text" textEncoding="utf-8" transferMode="Buffered" 
  11. useDefaultWebProxy="true"> 
  12. < readerQuotas maxDepth="32" maxStringContentLength="8192" 

    maxArrayLength="16384" 
  13. maxBytesPerRead="4096" maxNameTableCharCount="16384" /> 
  14. < security mode="None"> 
  15. < transport clientCredentialType="None" proxyCredentialType="None" 
  16. realm="" /> 
  17. < message clientCredentialType="UserName" algorithmSuite="Default" /> 
  18. < /security> 
  19. < /binding> 
  20. < /basicHttpBinding> 
  21. < /bindings> 
  22. < client> 
  23. < endpoint address="http://localhost:8080/MyWCF" 
    binding="basicHttpBinding" 
  24. bindingConfiguration="BasicHttpBinding_IContract" contract="IContract" 
  25. name="BasicHttpBinding_IContract" /> 
  26. < /client> 
  27. < /system.serviceModel> 
  28. < /configuration> 

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

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方法中添加如下代码。

  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Text;  
  4. using MyWCF.ClientBase;  
  5. namespace MyWCF.Client  
  6. {  
  7. class Program  
  8. {  
  9. static void Main(string[] args)  
  10. {  
  11. ContractClient CC = new ContractClient();  
  12. float result = CC.GetFahrenheit(23.4f);  
  13. Console.WriteLine("华氏温度为{0}度!", result);  
  14. }  
  15. }  

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

6)回到客户端的MyWCF.Client项目,按Ctrl + F5执行程序。

 

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

责任编辑:曹凯 来源: CSDN
相关推荐

2010-02-24 16:39:27

WCF客户端处理

2009-12-22 10:29:59

WCF客户端处理

2009-11-05 13:00:25

WCF客户端

2009-12-22 18:18:11

WCF客户端编程

2009-12-07 18:26:36

WCF客户端

2009-12-08 16:47:06

WCF IP

2009-11-25 13:21:30

PHP作为memcac

2009-12-22 18:43:00

WCF异步调用

2009-11-09 15:49:01

WCF异步调用

2009-11-05 13:08:44

WCF客户端配置

2009-12-21 15:53:56

WCF获取客户端IP

2010-02-22 11:10:17

WCF获取客户端IP

2011-09-09 09:44:23

WCF

2010-02-23 09:58:21

WCF客户端验证

2010-07-06 15:21:25

UDP客户端

2010-02-23 15:12:25

WCF客户端

2009-12-21 10:19:05

Silverlight

2010-02-24 16:17:09

WCF获取客户端IP

2009-12-21 10:09:26

WCF创建客户端服务对

2015-06-03 09:27:05

JavaScript客户端检测技术
点赞
收藏

51CTO技术栈公众号