• 创建类库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. 3 触发器报警-->远程执行命令

    0.需求 上节课我们讲了,触发器报警,发送邮件,这节课主要讲下远程执行命令 流程图如下 item--> triggers-->action--->Email     |——>远 ...

  2. 【leetcode】1281. Subtract the Product and Sum of Digits of an Integer

    题目如下: Given an integer number n, return the difference between the product of its digits and the sum ...

  3. C#静态变量 总结

    在C#程序中,没有全局变量的概念,这意味着所有的成员变量只有该类的实例才能操作这些数据,这起到了“信息隐藏”的作用.但有些时候,这样做却不是个明智的选择. 假设我们要定义一个图书类,要求该类能保存图书 ...

  4. Javascript调试技巧整理

    整理一下网上看到的实用调试技巧! 1. 不要使用alert 首先,alert只能打印出字符串,如果打印的对象不是String,则会调用toString()方法将该对象转成字符串(比如转成[object ...

  5. FZU - 2103 Bin & Jing in wonderland

    FZU - 2103 Bin & Jing in wonderland 题目大意:有n个礼物,每次得到第i个礼物的概率是p[i],一个人一共得到了k个礼物,然后按编号排序后挑选出r个编号最大的 ...

  6. 类中定义成员方法。加不加public有什么区别?

    class Trangle{ double sideA, sideB, sideC, area, length; boolean flag; Trangle(double a, double b, d ...

  7. 快速乘(O(1))

    inline long long multi(long long x,long long y,long long mod) { long long tmp=(x*y-(long long)((long ...

  8. 创建docker静态化IP

    配置桥接网络 桥接本地物理网络的目的,是为了局域网内用户方便访问 docker 实例中服务,不需要各种端口映射即可访问服务. 但是这样做,又违背了 docker 容器的安全隔离的原则,工作中辩证的选择 ...

  9. Linux 变量 $$ $! $? $- $# $* $@ $0 $n

    [参考文章]:linux中shell变量$#,$@,$0,$1,$2的含义解释 1. 变量说明 1.1 $$ Shell本身的PID(ProcessID) 1.2 $! Shell最后运行的后台Pro ...

  10. 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头之后,调用该函数,该函数提供了路由查找校验. ...