• 创建类库WCFServiceProxy
  • 添加System.ServiceModel、WCFService(见上篇文章)引用
  • 创建类:BookServiceClient
using System;
using System.Collections.Generic;
using System.Linq;
using System.ServiceModel;
using System.ServiceModel.Channels;
using System.Text;
using System.Threading.Tasks;
using WCFService;
using WCFService.Models; namespace WCFServiceProxy
{
public class BookServiceClient : ClientBase<IBookService>, IBookService
{
public BookServiceClient() : base() { }
public BookServiceClient(string endpointConfigurationName) : base(endpointConfigurationName) { }
public BookServiceClient(string endpointConfigurationName, string remoteAddress) : base(endpointConfigurationName, remoteAddress) { }
public BookServiceClient(string endpointConfigurationName, EndpointAddress remoteAddress) : base(endpointConfigurationName, remoteAddress) { }
public BookServiceClient(Binding binding, EndpointAddress remoteAddress) : base(binding, remoteAddress) { }
public bool Add(string name, double price)
{
return base.Channel.Add(name, price);
} public List<Book> GetList()
{
return base.Channel.GetList();
}
}
}

创建类BookServiceProxy

using System;
using System.Collections.Generic;
using System.Linq;
using System.ServiceModel;
using System.Text;
using System.Threading.Tasks;
using WCFService.Models; namespace WCFServiceProxy
{
public static class BookServiceProxy
{
private static string _clientEndpointName = "bookInfo";
static List<Book> list = new List<Book>();
public static bool Add(string name, double price)
{
BookServiceClient client = null;
try
{
client = new BookServiceClient(_clientEndpointName);
client.Add(name, price);
client.Close();
return true;
}
catch (Exception ex)
{
if (client != null && client.State != CommunicationState.Closed)
{
client.Abort();
client = null;
}
return false;
}
finally
{
client = null;
}
} public static List<Book> GetList()
{
BookServiceClient client = null;
try
{
client = new BookServiceClient(_clientEndpointName);
list = client.GetList();
client.Close();
return list;
}
catch (Exception ex)
{
if (client != null && client.State != CommunicationState.Closed)
{
client.Abort();
client = null;
}
return null;
}
finally
{
client = null;
}
}
}
}

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. BZOJ 1758 / Luogu P4292 [WC2010]重建计划 (分数规划(二分/迭代) + 长链剖分/点分治)

    题意 自己看. 分析 求这个平均值的最大值就是分数规划,二分一下就变成了求一条长度在[L,R]内路径的权值和最大.有淀粉质的做法但是我没写,感觉常数会很大.这道题可以用长链剖分做. 先对树长链剖分. ...

  2. 浅析flex 布局

    Flex基本概念: 容器默认存在两根轴:水平的主轴(main axis)和垂直的交叉轴(cross axis).主轴的开始位置(与边框的交叉点)叫做main start,结束位置叫做main end: ...

  3. fish-redux快速创建文件夹模板 FishReduxTemplate

    推荐一款插件: 在插件plugins中搜  FishReduxTemplate

  4. Sogou for Linux

    造冰箱的大熊猫,本文适用于Ubuntu 18.04@cnblogs 2019/5/8 在Ubuntu(安装时选择英文语言环境)下安装搜狗输入法 1)从搜狗官网下载sogou拼音输入法的deb安装包. ...

  5. flask框架(三):flask配置文件

    flask中的配置文件是一个flask.config.Config对象(继承字典),默认配置为: { 'DEBUG': get_debug_flag(default=False), 是否开启Debug ...

  6. HDU 6154 CaoHaha's staff(2017中国大学生程序设计竞赛 - 网络选拔赛)

    题目代号:HDU 6154 题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=6154 CaoHaha's staff Time Limit: 2000/1 ...

  7. Spring Boot教程(三十五)使用MongoDB数据库(1)

    MongoDB简介 MongoDB是一个基于分布式文件存储的数据库,它是一个介于关系数据库和非关系数据库之间的产品,其主要目标是在键/值存储方式(提供了高性能和高度伸缩性)和传统的RDBMS系统(具有 ...

  8. Primes and Multiplication

    C - Primes and Multiplication 思路:找到x的所有质数因子,用一个vector储存起来,然后对于每一个质因子来说,我们要找到它对最后的答案的贡献的大小,即要找到它在最后的乘 ...

  9. [CSP-S模拟测试]:慢无止境的八月(乱搞)

    题目传送门(内部题102) 输入格式 第一行三个正整数$n,k,q$,分别表示数列长度,操作长度和修改个数. 第二行$n$个数,表示给出的终止数列. 接下来$q$行,每行两个数$pos,dx$,表示将 ...

  10. linux_svn命令操作

    转至元数据起始   linux下svn命令大全 1.将文件checkout到本地目录 svn checkout path(path是服务器上的目录)例如:svn checkout svn://192. ...