• 创建类库WCFServiceProxy
  • 添加System.ServiceModel、WCFService(见上篇文章)引用
  • 创建类:BookServiceClient
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.ServiceModel;
  5. using System.ServiceModel.Channels;
  6. using System.Text;
  7. using System.Threading.Tasks;
  8. using WCFService;
  9. using WCFService.Models;
  10.  
  11. namespace WCFServiceProxy
  12. {
  13. public class BookServiceClient : ClientBase<IBookService>, IBookService
  14. {
  15. public BookServiceClient() : base() { }
  16. public BookServiceClient(string endpointConfigurationName) : base(endpointConfigurationName) { }
  17. public BookServiceClient(string endpointConfigurationName, string remoteAddress) : base(endpointConfigurationName, remoteAddress) { }
  18. public BookServiceClient(string endpointConfigurationName, EndpointAddress remoteAddress) : base(endpointConfigurationName, remoteAddress) { }
  19. public BookServiceClient(Binding binding, EndpointAddress remoteAddress) : base(binding, remoteAddress) { }
  20. public bool Add(string name, double price)
  21. {
  22. return base.Channel.Add(name, price);
  23. }
  24.  
  25. public List<Book> GetList()
  26. {
  27. return base.Channel.GetList();
  28. }
  29. }
  30. }

创建类BookServiceProxy

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.ServiceModel;
  5. using System.Text;
  6. using System.Threading.Tasks;
  7. using WCFService.Models;
  8.  
  9. namespace WCFServiceProxy
  10. {
  11. public static class BookServiceProxy
  12. {
  13. private static string _clientEndpointName = "bookInfo";
  14. static List<Book> list = new List<Book>();
  15. public static bool Add(string name, double price)
  16. {
  17. BookServiceClient client = null;
  18. try
  19. {
  20. client = new BookServiceClient(_clientEndpointName);
  21. client.Add(name, price);
  22. client.Close();
  23. return true;
  24. }
  25. catch (Exception ex)
  26. {
  27. if (client != null && client.State != CommunicationState.Closed)
  28. {
  29. client.Abort();
  30. client = null;
  31. }
  32. return false;
  33. }
  34. finally
  35. {
  36. client = null;
  37. }
  38. }
  39.  
  40. public static List<Book> GetList()
  41. {
  42. BookServiceClient client = null;
  43. try
  44. {
  45. client = new BookServiceClient(_clientEndpointName);
  46. list = client.GetList();
  47. client.Close();
  48. return list;
  49. }
  50. catch (Exception ex)
  51. {
  52. if (client != null && client.State != CommunicationState.Closed)
  53. {
  54. client.Abort();
  55. client = null;
  56. }
  57. return null;
  58. }
  59. finally
  60. {
  61. client = null;
  62. }
  63. }
  64. }
  65. }

WCF客户端代理的更多相关文章

  1. 终于解决:升级至.NET 4.6.1后VS2015生成WCF客户端代理类的问题

    在Visual Studio 2015中将一个包含WCF引用的项目的targetFramework从4.5改为4.6.1的时候,VS2015会重新生成WCF客户端代理类.如果WCF引用配置中选中了&q ...

  2. WCF 客户端代理生成 通过SvcUtil.exe

    WCF服务调用通过两种常用的方式:一种是借助代码生成工具SvcUtil.exe或者添加服务引用的方式,一种是通过ChannelFactory直接创建服务代理对象进行服务调用. 下面简单说下如何通过Sv ...

  3. 通过SvcUtil.exe 生成 Wcf 客户端代理

    WCF服务调用通过两种常用的方式:一种是借助代码生成工具SvcUtil.exe或者添加服务引用的方式,一种是通过ChannelFactory直接创建服务代理对象进行服务调用. SvcUtil.exe ...

  4. WCF生成客户端代理对象的两种方法的解释

    最近在封装WCF,有一些很好的实践就记录下来,大家可以放心使用,所有代码都已经调试过.如果有高手可以大家探讨一下. 在WCF中有两种不同的方法可以用于创建客户端服务对象,他们分别为: 1. 代理构造法 ...

  5. wcf生成客户端代理类步骤及语句

    通过svcutil.exe工具生成客户端代理类和客户端的配置文件 .在运行中输入cmd打开命令行 ()cd C:\Program Files (x86)\Microsoft SDKs\Windows\ ...

  6. wcf http 代理

    两个我都还没试,先记录着,其实我也不咋懂,所以记录着,权当一个线索 第一种 wcf中设置代理是在bing类中设置的,比如 WSHttpBinding ws = new WSHttpBinding(); ...

  7. WCF初探-10:WCF客户端调用服务

    创建WCF 服务客户端应用程序需要执行下列步骤: 获取服务终结点的服务协定.绑定以及地址信息 使用该信息创建 WCF 客户端 调用操作 关闭该 WCF 客户端对象 WCF客户端调用服务存在以下特点: ...

  8. WCF初探-11:WCF客户端异步调用服务

    前言: 在上一篇WCF初探-10:WCF客户端调用服务 中,我详细介绍了WCF客户端调用服务的方法,但是,这些操作都是同步进行的.有时我们需要长时间处理应用程序并得到返回结果,但又不想影响程序后面代码 ...

  9. 【WCF系列】(四)WCF客户端怎么消费服务

    WCF客户端怎么消费服务 获取服务绑定协议.绑定和地址:实现方式 SvcUtil方式:SvcUtil.exe是一个命令行工具,位于:C:\Program Files (x86)\Microsoft S ...

随机推荐

  1. CSS 定位详解

    CSS 有两个最重要的基本属性,前端开发必须掌握:display 和 position position 属性的作用 position属性用来指定一个元素在网页上的位置,一共有5种定位方式,即posi ...

  2. vue2.0 + element-ui2实现分页

    当我们向服务端请求大量数据的时候,并要在页面展示出来,怎么办?这个时候一定会用到分页. 本次所使用的是vue2.0+element-ui2.12实现一个分页功能,element-ui这个组件特别丰富, ...

  3. Xhorse Condor XC-Mini Plus回顾

    Condor是Xhorse生产的最新型电子钥匙切割机.该机器在激光,圆柱和Tibbe键上具有出色的切割性能,几乎可以复制,切割,编码和解码任何汽车钥匙! Condor XC-MINI Plus的亮点: ...

  4. IDEA运行有问题debug正常解决方案

    朋友们!有没有遇到这样的问题,IDEA运行有问题,debug确是正常的,不经怀疑人生! 不要慌!点击maven,clean一下,再compile一下,就好啦! 不要慌!点击maven,clean一下, ...

  5. [Python之路] 元类(引申 单例模式)

    一.类也是对象 当我们定义一个变量或者函数的时候,我们可以在globals()的返回值字典中找到响应的映射: def A(): print("This is function A" ...

  6. 洛谷 P1341 无序字母对(欧拉路)

    P1341 无序字母对 题目提供者yeszy 标签 福建省历届夏令营 难度 提高+/省选- 最新讨论 题目描述 给定n个各不相同的无序字母对(区分大小写,无序即字母对中的两个字母可以位置颠倒).请构造 ...

  7. 【CUDA 基础】0.0 腾讯云CUDA环境搭建

    title: [CUDA 基础]0.0 腾讯云CUDA环境搭建 categories: CUDA Freshman tags: CUDA 环境搭建 toc: true date: 2018-02-13 ...

  8. EasySwoole 在mac上装虚拟机centos共享mac目录报错处理

    [2019-09-26 21:37:09][DEBUG][ERROR] : [listen /media/psf/Home/wwwroot/myobj/easyswoole/Temp/TaskWork ...

  9. TCP->IP输出 之 ip_queue_xmit、ip_build_and_send_pkt、ip_send_unicast_reply

    概述 ip_queue_xmit是ip层提供给tcp层发送回调,大多数tcp发送都会使用这个回调,tcp层使用tcp_transmit_skb封装了tcp头之后,调用该函数,该函数提供了路由查找校验. ...

  10. TCP之11种状态变迁

    1. TCP 之11种状态变迁 TCP 为一个连接定义了 11 种状态,并且 TCP 规则规定如何基于当前状态及在该状态下所接收的分节从一个状态转换到另一个状态.如,当某个应用进程在 CLOSED 状 ...