引用WCF服务正确实现方法介绍

当我们在实际编程中需要对WCF服务进行引用的时候,需要两种方法来实现。那么今天我们就针对引用WCF服务的相关方法来进行一个详细介绍。希望这里介绍的内容可以解决朋友在实际应用中碰到的问题。

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

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

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

 
 
 
  1. var client = new ServiceReference1.Service1Client();   
  2. var client = new ServiceReference1.Service1Client(); 

第二种方式:在CS文件中,直接定义引用WCF服务,代码如下:

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

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

【编辑推荐】

  1. 两个基本WCF应用技巧总结
  2. WCF扩展点在自定义运行时使用
  3. WCF客户端具体搭建方法解析
  4. WCF IDisposable接口应用功能详解
  5. WCF REST正确定义方法介绍
THE END