简单实现WCF基础开发

开发 开发工具
我们在这里为大家总结的WCF基础开发的实现步骤可以分为六步,分别为:定义WCF服务契约;实现WCF服务契约;启动WCF服务;创建一个基本的WCF客服端;WCF客服端基本配置;使用WCF客户端等。

WCF是微软的一个通信编程框架。很多人可能对于这样的一款新框架了解的还不是很全面。开发人员可以通过这款框架实现跨平台可靠的互联网解决方案。#t#

在这里我就用一个据于一个简单的场景:服务端为客服端提供获取客户信息的一个接口读取客户信息,来完成WCF基础开发的六个步骤。

WCF基础开发1. 定义WCF服务契约

A. 项目引用节点右键添加System.ServiceModel引用。

B. 在代码文件里,添加以下命名空间的引用

using System.ServiceModel;

using System;

C. 新建一个命为ICustomerService 接口,并添加一个获取客户信息的方法定义名为CustomerInfomation,返回字符串类型的客户信息。

D. 为接口ICustomerService添加ServiceContract的属性修饰使它成为WCF服务中公开的接口。

E. 为方法CustomerInfomation添加OperationContract的属性修饰使它成为WCF服务公开接口中公开的成员。

F. 代码:

  1. using System;  
  2. using System.ServiceModel;  
  3. namespace ConWCF  
  4. { [ServiceContract(Namespace =
     
    "http://Microsoft.Service
    Model.Samples"
    )]  
  5. public interface CustomerService  
  6. {    
  7. [OperationContract]  
  8. String CustomerInformation();  
  9. }   

WCF基础开发2. 实现WCF服务契约

实现WCF服务契约很简单,就是实现上一步聚定义的WCF服务契约定义的接口就可以。下面看代码

  1. using System;  
  2. using System.ServiceModel;  
  3. namespace ConWCF  
  4. { [ServiceContract(Namespace = 
    "http://Microsoft.ServiceModel.Samples")]  
  5. public interface ICustomerService  
  6. {    
  7. [OperationContract]  
  8. String CustomerInformation();  
  9. }  
  10. public class CustomerService:
    ICustomerService   
  11. {   
  12. #region ICustomerService 成员  
  13. public string CustomerInformation()  
  14. {  
  15. return "这是客户的信息!";  
  16. }  
  17. #endregion  
  18. }  

WCF基础开发3. 启动WCF服务

A.添加一个应用程序配置文件,文件件名为App.config。

B.配置WCF服务的基本地址,如下所示

  1. < host> 
  2. < baseAddresses> 
  3. < addbaseAddressaddbaseAddress=
    "http://localhost:8000/conwcfr"/> 
  4. < /baseAddresses> 
  5. < /host> 

C.配置WCF服务的端口。Address=“”,意思就是使用上面配置的基本地址,当然也可以在这里指定。Bingding=“wsHttpBinding”,意思是WCF服务使用的是HTTP协议。再接下来就是配置WCF服务契约了(命名空间.服务契约接口名),如下所示:

  1. < endpointaddressendpointaddress="" 
  2. binding="wsHttpBinding" 
  3. contract="ConWCF.ICustomerService" /> 

D.配置文件

E.启动服服就简单了

  1. ServiceHost host = new 
    ServiceHost(typeof(CustomerService));  
  2. host.Open();  
  3. Console.WriteLine("客户信息服务已启动");  
  4. Console.WriteLine("按任意键结束服务!");  
  5. Console.Read();  
  6. host.Close(); 

F.当服务启动时,在IE栏中输入: http://localhost:8000/conwcfr,将会收到一些帮助的提示信息。

G.异常:配置文件中的服务名称一定是:命名空间.实现WCF服务契约类的名称,否则将会发生找到不配置的异常。

  1. < service name=
    "ConWCF.CustomerService" 

异常信息: Service 'ConWCF.CustomerService' has zero application (non-infrastructure) endpoints. This might be because no configuration file was found for your application, or because no service element matching the service name could be found in the configuration file, or because no endpoints were defined in the service element.

这个异常搞得我昏了半天,害得我以为从IIS、端口到配置环境排除错误,就是搞不明白为什么会跟类的命称联系起来。不过,最终也解决了。

WCF基础开发4. 创建一个基本的WCF客服端

WCF服务端创建好啊,创建客户端就容易多了,直接用SVCUTIL 命令行工具去完成代码的生成。我安装了WINDOWS SDK,其带了一个CMDShell 命令行工具,打开后就可以运行SVCUTIL命令,这个命令是运行于 framework 3.0以上环境。查看详细帮助信息可以输入:svcutil /?,回车。

1. 启动上几步骤创建好的WCF服务端。

2. 在CMDShell工具中用CD 转到你要存放客户端代码的目录下,输入以下命令生成代码和配置文件。

D:"client>svcutil /language:c# /out:CustomerClient.cs /config:app.config http:/

/localhost:8000/conwcfr

上面命令指定了要生成代码的语言,代码文件和配置文件名,WCF服务端地址,注意运行命令时必须确定WCF服务端正在运行中。

WCF基础开发5. WCF客服端基本配置

WCF客户端配置就是配置调用WCF服务端的协议,输传宽带,服务地址,安全等等信息。下面就上一步骤命令自动生成的配置文件。

  1. < ?xml version="1.0" encoding="utf-8"?> 
  2. < configuration> 
  3. < system.serviceModel> 
  4. < bindings> 
  5. < wsHttpBinding> 
  6. < binding name="WSHttpBinding_
    ICustomerService"
     closeTimeout="00:01:00" 
  7. openTimeout="00:01:00" receiveTimeout=
    "00:10:00" sendTimeout="00:01:00" 
  8. bypassProxyOnLocal="false" 
    transactionFlow="false" hostName
    ComparisonMode
    ="StrongWildcard" 
  9. maxBufferPoolSize="524288" maxRece
    ivedMessageSize
    ="65536" 
  10. messageEncoding="Text" textEncoding
    ="utf-8" useDefaultWebProxy="true" 
  11. allowCookies="false"> 
  12. < readerQuotas maxDepth="32" maxStr
    ingContentLength
    ="8192" 
    maxArrayLength="16384" 
  13. maxBytesPerRead="4096" maxNameTab
    leCharCount
    ="16384" /> 
  14. < reliableSession ordered="true" 
    inactivityTimeout="00:10:00" 
  15. enabled="false" /> 
  16. < security mode="Message"> 
  17. < transport clientCredentialType=
    "Windows" proxyCredentialType="None" 
  18. realm="" /> 
  19. < message clientCredentialType=
    "Windows" negotiateServiceCredential="true" 
  20. algorithmSuite="Default" 
    establishSecurityContext="true" /> 
  21. < /security> 
  22. < /binding> 
  23. < /wsHttpBinding> 
  24. < /bindings> 
  25. < client> 
  26. < endpoint address="http:
    //localhost:8000/conwcfr"
     
    binding="wsHttpBinding" 
  27. bindingConfiguration="WSHttpBinding
    _ICustomerService"
     contract="ICustomerService" 
  28. name="WSHttpBinding_ICustomerService"> 
  29. < identity> 
  30. < userPrincipalName value=
    "30DA1D0B1D1E4D2\Administrator" /> 
  31. < /identity> 
  32. < /endpoint> 
  33. < /client> 
  34. < /system.serviceModel> 
  35. < /configuration> 

WCF基础开发6. 使用WCF客户端

在客户端项目中项目引用节点右键添加System.ServiceModel引用.
添加第四部中创建的客户端代码文件和配置文件。
客户端调用服务端的服务,只要创建生成客户端类的实例就可调用了,但要确认服务端正在起用状态,如下

  1. using System;  
  2. namespace ConWCFCustomerClient  
  3. {  
  4. class Program  
  5. {  
  6. static void Main(string[] args)  
  7. {   
  8. CustomerServiceClient client = 
    new CustomerServiceClient();  
  9. string message=client.
    CustomerInformation();  
  10. Console.WriteLine(message);  
  11. Console.Read();  
  12. }  
  13. }  

 

责任编辑:曹凯 来源: 博客园
相关推荐

2009-11-06 09:22:46

WCF应用

2009-11-06 16:48:03

WCF简介

2010-02-24 15:20:23

WCF Message

2010-02-22 13:56:35

WCF服务契约

2010-02-24 12:49:39

WCF枚举

2010-02-25 13:35:27

WCF tcpTrac

2012-04-20 10:05:16

WCF

2009-11-05 09:51:14

WCF基础

2010-03-02 16:22:31

WCF状态应用

2011-06-28 10:20:19

Ubuntu Qt Designer

2010-02-22 14:28:35

WCF实现loadin

2009-11-09 14:02:31

WCF传输数据

2009-12-21 17:48:30

WCF方法重载

2009-12-08 15:06:33

WCF传输DataSe

2011-10-27 16:24:48

API

2010-03-01 13:17:46

WCF单向服务

2010-02-26 14:19:03

WCF用户验证

2010-02-22 16:43:09

WCF负载平衡

2011-05-16 09:30:30

jQueryWCF

2010-03-01 16:31:58

WCF实现SOA
点赞
收藏

51CTO技术栈公众号