WCF学习——构建一个简单的WCF应用(二)
我们接着上一篇文章进行讲解 http://www.cnblogs.com/songjianhui/p/7060698.html
一:客户端通过添加引用调用服务
WCF应用服务被成功寄宿后,WCF服务应用便开始了服务调用请求的监听工作。此外,服务寄宿将服务描述通过元数据的形式发布出来,相应的客户端就可以获取这些元数据。接下来我们来创建客户端程序进行服务的调用。
1)先运行服务寄宿程序(Hosting.exe)
2) 在Visual Studio 2013的“解决方案资源管理器”中,把Client项目展开,左键选中“引用”,点击鼠标右键,弹出菜单,在弹出的上下文菜单中选择“添加服务引用(Add Service References)”。如下图。

3) 此时会弹出一个对话框,如下图所示。在对话框中的“地址”输入框中输入服务元数据发布的源地址:http://127.0.0.1:3721/calculatorService/metadata,并在“命名空间”输入框中输入一个命名空间,然后点击“确定”按钮(如下图)。Visual studio 2013会自动生成一系列用于服务调用的代码和配置。

4) 在Visual Studio 2013自动生成的类中,包含一个服务协定接口、一个服务代理对象和其他相关的类。

5) 我们可以实例化CalculatorServiceClient对象,执行相应方法调用WCF服务操作。客户端进行WCF服务调用的代码如下:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Client
{
class Program
{
static void Main(string[] args)
{
MyWcfService.CalculatorServiceClient client = new MyWcfService.CalculatorServiceClient();
Console.WriteLine(, , client.Add(, ));
Console.WriteLine(, , client.Add(, ));
Console.WriteLine(, , client.Add(, ));
Console.WriteLine(, , client.Add(, ));
Console.ReadLine();
}
}
}
6)结果

二:客户端通过ChannelFactory<T>方式调用WCF服务
1) WCF采用基于契约的服务调用方法。从上面的例子也可以看到,Visual Studio2013 在进行服务引用添加的过程中会在客户端创建一个与服务端等效的服务契 约接口。由于服务端和客户端在同一个解决方案中。因此完全可以让服务端和客户端引用相同的契约
2) 为了演示这种场景,我们将添加的服务引用移除,并为Client项目添加Service.Interface项目的引用。在客户端程序中基于地址和绑定对象创建一个 ChannelFactory<ICalculator>,然后调用它的CreateChannel方法 创建的服务代理对象完成服务调用(这里我们就直接创建一个控制台来进行演示)
3)创建一个控制台应用程序 引用Service.Interface和System.ServiceModel;

4) 编写ChanelClient的代码
1.通过代码的方式配置终结点
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Service.Interface;
using System.ServiceModel;
namespace ChannelClient
{
class Program
{
static void Main(string[] args)
{
//基于地址和绑定对象创建一个ChannelFactory<ICalculator> 通过代码
using (ChannelFactory<ICalculator> channelFactory = new ChannelFactory<ICalculator>(new WSHttpBinding(), "http://127.0.0.1:3721/calculatorService"))
{
//创建服务代理对象
ICalculator proxy = channelFactory.CreateChannel();
//调用计算器方法
Console.WriteLine(,,proxy.Add(,));
Console.WriteLine(, , proxy.Add(, ));
Console.WriteLine(, , proxy.Add(, ));
Console.WriteLine(, , proxy.Add(, ));
Console.ReadLine();
}
}
}
}
2.通过配置文件的方式来配置终结点(在app.config中进行配置)
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" />
</startup>
<system.serviceModel>
<client>
<endpoint name="calculatorService" address="http://127.0.0.1:3721/CalculatorService" binding="wsHttpBinding" contract="Service.Interface.ICalculator"/>
</client>
</system.serviceModel>
</configuration>
ChannelClient中的代码就要进行更改
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Service.Interface;
using System.ServiceModel;
namespace ChannelClient
{
class Program
{
static void Main(string[] args)
{
//基于地址和绑定对象创建一个ChannelFactory<ICalculator> 通过代码
//using (ChannelFactory<ICalculator> channelFactory = new ChannelFactory<ICalculator>(new WSHttpBinding(), "http://127.0.0.1:3721/CalculatorService"))
//{
// //创建服务代理对象
// ICalculator proxy = channelFactory.CreateChannel();
// //调用计算器方法
// Console.WriteLine("x+y={2} when x={0} and y={1}",1,2,proxy.Add(1,2));
// Console.WriteLine("x-y={2} when x={0} and y={1}", 1, 2, proxy.Add(1, 2));
// Console.WriteLine("x*y={2} when x={0} and y={1}", 1, 2, proxy.Add(1, 2));
// Console.WriteLine("x/y={2} when x={0} and y={1}", 1, 2, proxy.Add(1, 2));
// Console.ReadLine();
//}
//基于地址和绑定对象创建一个ChannelFactory<ICalculator> 通过配置文件
using (ChannelFactory<ICalculator> channelFactory = new ChannelFactory<ICalculator>("calculatorService"))
{
//创建服务代理对象
ICalculator proxy = channelFactory.CreateChannel();
//调用计算器方法
Console.WriteLine(, , proxy.Add(, ));
Console.WriteLine(, , proxy.Add(, ));
Console.WriteLine(, , proxy.Add(, ));
Console.WriteLine(, , proxy.Add(, ));
Console.ReadLine();
}
}
}
}
5) 执行hosting.exe应用程序
6)运行ChanelClient

持续更新中.................................
需要源码的发送邮件到 1163598274@qq.com 每周星期天统一发送
WCF学习——构建一个简单的WCF应用(二)的更多相关文章
- WCF学习——构建一个简单的WCF应用(一)
本文的WCF服务应用功能很简单,却涵盖了一个完整WCF应用的基本结构.希望本文能对那些准备开始学习WCF的初学者提供一些帮助. 在这个例子中,我们将实现一个简单的计算器和传统的分布式通信框架一样,WC ...
- 学习构建一个简单的wcf服务
入门,构建第一个WCF程序 1.服务端 建立一个控制台应用程序作为Server,新建一个接口IData作为服务契约.这个契约接口一会儿也要放到Client端,这样双方才能遵循相同的标准.别忘了添加对 ...
- 重温WCF之构建一个简单的WCF(一)(2)通过Windows Service寄宿服务和WCF中实现操作重载
参考地址:http://www.cnblogs.com/zhili/p/4039111.html 一.如何在Windows Services中寄宿WCF服务 第一步:创建Windows 服务项目,具体 ...
- 重温WCF之构建一个简单的WCF(一)(1)通过控制台和IIS寄宿服务
一.理解什么是WCFWCF就是.NET平台下各种分布式技术的集成,并提供了一套统一的编程接口 二.WCF的定义WCF(Windows Communication Foundation)是微软为构建面向 ...
- 构建一个简单的WCF应用——WCF学习笔记(1)
通过<WCF全面解析>来知识分享....感谢蒋金楠老师@Artech 一.VS中构建解决方案 Client一个控制台程序模拟的客户端,引用Service.ServiceModel.dl ...
- [WCF学习笔记] 我的WCF之旅(1):创建一个简单的WCF程序
近日学习WCF,找了很多资料,终于找到了Artech这个不错的系列.希望能从中有所收获. 本文用于记录在学习和实践WCF过程中遇到的各种基础问题以及解决方法,以供日后回顾翻阅.可能这些问题都很基础,可 ...
- WCF入门, 到创建一个简单的WCF应用程序
什么是WCF? WCF, 英文全称(windows Communication Foundation) , 即为windows通讯平台. windows想到这里大家都知道了 , WCF也正是由微软公 ...
- WCF学习之旅—TCP双工模式(二十一)
WCF学习之旅—请求与答复模式和单向模式(十九) WCF学习之旅—HTTP双工模式(二十) 五.TCP双工模式 上一篇文章中我们学习了HTTP的双工模式,我们今天就学习一下TCP的双工模式. 在一个基 ...
- 【Android Developers Training】 3. 构建一个简单UI
注:本文翻译自Google官方的Android Developers Training文档,译者技术一般,由于喜爱安卓而产生了翻译的念头,纯属个人兴趣爱好. 原文链接:http://developer ...
随机推荐
- add,update,list.jsp源码
add:<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncod ...
- bootstrap+masonry.js写瀑布流
最近在用bootstrap写一个网站,其中有个图文展示的页面要用到瀑布流的效果.因为项目要求,项目要以bootstrap为基准,不准私自添加内联样式.内部样式,所以,自己写瀑布流就不行了,所以,根据要 ...
- Installing MySQL on Microsoft Windows Using a noinstall Zip Archive
这两天在自己的windows上安装了一下mySql数据库,安装使用的是5.7.18版本的 noinstall Zip Archive安装包mysql-5.7.18-win32.zip.由于5.7版本相 ...
- scala练手之数字转汉字小工具
输入数字,转换成汉字,在统计数据量时很好用,而输入数字转成大写汉字,可以用于填写收据报销单哦 下载链接 https://pan.baidu.com/s/1nv3Ci6l 效果图如下: 直接上代码 ob ...
- python基本语法-加密解密等
1. 编写函数,要求输入x与y,返回x和y的平方差 2. 计算1到100的平方的和 3. 编写函数,若输入为小于100的数,返回TRUE,大于100的数,返回FALSE 4. 某个公司采用公用电话传递 ...
- Gephi安装
Gephi for mac https://gephi.org/users/download/ 在官网上下载gephi-0.9.1-macos.dmg双击拖到Application里面就好了,注意有的 ...
- java的List分页 取出数据后使用List分页
以前一直是在DAO层直接从数据库里分页,但是今天因为有些数据,需要混合展示,就是根据条件取出了多个对象的集合,然后把这些多个List放到一个List里,然后在从这个List里进行分页. MemberA ...
- (window,parent,opener,top).location.reload方法汇总
今天在火狐浏览器上碰到个bug,调用parent.location.reload()时只刷新子页面,没有整个浏览器刷新,谷歌上没有问题,网上搜了一下 改成parent.location.reload( ...
- mysql5.6 主从复制
Master 192.168.59.128 Slave 192.168.59.129 默认认为已安装mysql5.6 mysql5.6 rpm安装配置 修改Master my.cnf文件 # ...
- 搭建eureka服务
1.pom.xml <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www ...