引用WCF服务经验分享

开发 开发工具
引用WCF服务可以通过在项目的ServiceReferences.ClientConfig文件中加入WCF服务定义来实现,或者,在CS文件中,直接定义WCF服务。

WCF服务的引用是一个比较简单的方式。我们可以通过两种方式来实现引用WCF服务。在这里,就向大家详细讲解引用WCF服务的方法。#t#

引用WCF服务1.在项目的ServiceReferences.ClientConfig文件中加入WCF服务定义,如下:

  1. view plaincopy to clipboardprint?  
  2. < configuration>   
  3. < system.serviceModel>   
  4. < bindings>   
  5. < basicHttpBinding>   
  6. < binding name="BasicHttpBinding_IService"
     maxBufferSize="2147483647"   
  7. maxReceivedMessageSize="2147483647">   
  8. < security mode="None" />   
  9. < /binding>   
  10. < /basicHttpBinding>   
  11. < /bindings>   
  12. < client>   
  13. < endpoint address="http://localhost:2442
    /Service1.svc"
     binding="basicHttpBinding"   
  14. bindingConfiguration="BasicHttpBinding_IService" 
    contract="ServiceReference1.IService1"   
  15. name="BasicHttpBinding_IService" />   
  16. < /client>   
  17. < /system.serviceModel>   
  18. < /configuration>   
  19. < configuration> 
  20. < system.serviceModel> 
  21. < bindings> 
  22. < basicHttpBinding> 
  23. < binding name="BasicHttpBinding_IService"
     maxBufferSize="2147483647" 
  24. maxReceivedMessageSize="2147483647"> 
  25. < security mode="None" /> 
  26. < /binding> 
  27. < /basicHttpBinding> 
  28. < /bindings> 
  29. < client> 
  30. < endpoint address="http://localhost:2442/
    Service1.svc"
     binding="basicHttpBinding" 
  31. bindingConfiguration="BasicHttpBinding_
    IService"
     contract="ServiceReference1.IService1" 
  32. name="BasicHttpBinding_IService" /> 
  33. < /client> 
  34. < /system.serviceModel> 
  35. < /configuration> 

在CS文件中,使用如下代码引用WCF服务

  1. view plaincopy to clipboardprint?  
  2. var client = new 
    ServiceReference1.Service1Client();   
  3. var client = new 
    ServiceReference1.Service1Client(); 

引用WCF服务2:在CS文件中,直接定义WCF服务,代码如下:

 

  1. view plaincopy to clipboardprint?  
  2. Binding binding = 
    new BasicHttpBinding();   
  3. EndpointAddress endPoint = 
    new EndpointAddress(   
  4. "http://localhost:2442/Service1.svc");   
  5. Service1Client client =
     
    new Service1Client(binding, endPoint);   
  6. Binding binding = 
    new BasicHttpBinding();  
  7. EndpointAddress endPoint = 
    new EndpointAddress(  
  8. "http://localhost:2442/Service1.svc");  
  9. Service1Client client = 
    new Service1Client(binding, endPoint);  

 

以上两种引用WCF服务方式都能设用WCF服务,比如第一种方式,如果没有定义配置文件,则会报 找不到键值的错误提示.

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

2009-12-22 16:03:03

WCF异常

2009-12-22 19:26:51

WCF绑定

2009-12-21 13:27:45

WCF服务配置信息

2010-03-19 14:03:34

云计算

2009-12-22 18:18:11

WCF客户端编程

2011-05-16 09:30:30

jQueryWCF

2010-02-22 17:43:19

WCF服务启动

2010-02-22 11:10:17

WCF获取客户端IP

2014-08-24 22:11:27

2009-12-21 16:37:41

WCF获取服务元数据

2010-02-25 16:52:12

引用WCF服务

2010-02-25 10:52:29

WCF响应服务

2010-03-01 09:48:23

WCF会话服务

2010-03-01 14:56:48

WCF服务引用

2009-12-07 15:02:46

WCF学习

2009-11-09 17:06:38

WCF选择绑定

2010-03-02 17:48:35

WCF寻址报头

2014-07-31 10:09:17

数据中心数字服务英特尔

2011-06-07 10:54:07

云计算

2010-02-22 17:58:06

WCF异步上传
点赞
收藏

51CTO技术栈公众号