WCF客户端代理
- 创建类库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客户端代理的更多相关文章
- 终于解决:升级至.NET 4.6.1后VS2015生成WCF客户端代理类的问题
在Visual Studio 2015中将一个包含WCF引用的项目的targetFramework从4.5改为4.6.1的时候,VS2015会重新生成WCF客户端代理类.如果WCF引用配置中选中了&q ...
- WCF 客户端代理生成 通过SvcUtil.exe
WCF服务调用通过两种常用的方式:一种是借助代码生成工具SvcUtil.exe或者添加服务引用的方式,一种是通过ChannelFactory直接创建服务代理对象进行服务调用. 下面简单说下如何通过Sv ...
- 通过SvcUtil.exe 生成 Wcf 客户端代理
WCF服务调用通过两种常用的方式:一种是借助代码生成工具SvcUtil.exe或者添加服务引用的方式,一种是通过ChannelFactory直接创建服务代理对象进行服务调用. SvcUtil.exe ...
- WCF生成客户端代理对象的两种方法的解释
最近在封装WCF,有一些很好的实践就记录下来,大家可以放心使用,所有代码都已经调试过.如果有高手可以大家探讨一下. 在WCF中有两种不同的方法可以用于创建客户端服务对象,他们分别为: 1. 代理构造法 ...
- wcf生成客户端代理类步骤及语句
通过svcutil.exe工具生成客户端代理类和客户端的配置文件 .在运行中输入cmd打开命令行 ()cd C:\Program Files (x86)\Microsoft SDKs\Windows\ ...
- wcf http 代理
两个我都还没试,先记录着,其实我也不咋懂,所以记录着,权当一个线索 第一种 wcf中设置代理是在bing类中设置的,比如 WSHttpBinding ws = new WSHttpBinding(); ...
- WCF初探-10:WCF客户端调用服务
创建WCF 服务客户端应用程序需要执行下列步骤: 获取服务终结点的服务协定.绑定以及地址信息 使用该信息创建 WCF 客户端 调用操作 关闭该 WCF 客户端对象 WCF客户端调用服务存在以下特点: ...
- WCF初探-11:WCF客户端异步调用服务
前言: 在上一篇WCF初探-10:WCF客户端调用服务 中,我详细介绍了WCF客户端调用服务的方法,但是,这些操作都是同步进行的.有时我们需要长时间处理应用程序并得到返回结果,但又不想影响程序后面代码 ...
- 【WCF系列】(四)WCF客户端怎么消费服务
WCF客户端怎么消费服务 获取服务绑定协议.绑定和地址:实现方式 SvcUtil方式:SvcUtil.exe是一个命令行工具,位于:C:\Program Files (x86)\Microsoft S ...
随机推荐
- this 的用法 为原始类型扩展方法
namespace Demo { public static class Extends { // string类型扩展ToJson方法 public static object ToJson(thi ...
- 激活WebStorm2017.3.1
亲测有效,测试日期:20181202 转发自:https://blog.csdn.net/bjzhaoxiao/article/details/81486490
- sh_15_字符串统计操作
sh_15_字符串统计操作 hello_str = "hello hello" # 1. 统计字符串长度 print(len(hello_str)) # 2. 统计某一个小(子)字 ...
- MySQL_(Java)使用JDBC向数据库中删除(delete)数据
MySQL_(Java)使用JDBC向数据库发起查询请求 传送门 MySQL_(Java)使用JDBC向数据库中插入(insert)数据 传送门 MySQL_(Java)使用JDBC向数据库中删除(d ...
- MS14-068提权
• Ms14- • 库 • https://github.com/bidord/pykek • ms14-.py -u user@lab.com -s userSID -d dc.lab.com • ...
- BZOJ刷题列表【转载于hzwer】
沿着黄学长的步伐~~ 红色为已刷,黑色为未刷,看我多久能搞完吧... Update on 7.26 :之前咕了好久...(足见博主的flag是多么emmm......)这几天开始会抽时间刷的,每天几道 ...
- Java微信服务号的开发(简单入门)
进行微信服务号开发的步骤 一个别人已经搭建好的项目参考地址:下载链接 1.搭建微信服务号的服务器 项目配置maven <dependency> <groupId>com.soe ...
- Spring常用的jar+普通构造注入
常用工具 jar 说明 提供AOP(面向切面编程)实现:spring -aop spring提供对AspectJ框架的整合:spring-aspects 提供 IoC(控制反转)的基础实现:sprin ...
- rtmp 协议详解
1. handshake 1.1 概述 rtmp 连接从握手开始.它包含三个固定大小的块.客户端发送的三个块命名为 C0,C1,C2:服务端发送的三个块命名为 S0,S1,S2. 握手序列: 客户端通 ...
- Node.js自学完全总结
零.什么是Node.js? 引用Node.js官方网站的解释如下: Node.js® is a JavaScript runtime built on Chrome's V8 JavaScript e ...