频 道 直 达 - 新闻 - 读书 - 培训 - 教程 - 前沿 - 组网 - 系统应用 - 安全 - 编程 - 存储 - 操作系统 - 数据库 - 服务器 - 专题 - 产品 - 案例库 - 技术圈 - 博客 - BBS
51CTO.COM_中国领先的IT技术网站
找资料:

WCF体验之旅(1):创建一个简单的WCF程序(4)

作者: 张逸 出处:Artech 2008-03-05 17:19    砖    好    评论   进入论坛
阅读提示:我们知道,和一个相对独立的应用不同,我们开发一个分布式应用, 尤其是开发一个企业级的分布式应用, 我们需要考虑较多的东西。

在System。Service。Model 命名空间里,定义了一个类abstract class ClientBase,给我们调用Service提供极大的便利。我们只要是我们的Client继承这样一个类,并为它指定Endpoint的三要素就一切OK了,下面我们来看看我们可以以那些方式来指定这些内容

1、Conract:我们看到了ClientBase是一个Generic的类,我们在创建一个继承自这个类的时候必须给它指定特定的TChannel.我们可以把Contract对应的类型作为Client的generic类型。

2、Binding和Address:和Service端的Endpoint一样,我们可以把相关的信息放在我们的Client端代码里面,也可以放在Client的Config里面。那个这些数据如何应用要我们创建的派生自ClientBase的类的对象上呢。其实很简单,ClientBase给我们定义了若干重载的构造函数,我们只要定义我们相应的构造函数应简单地调用基类的构造函数。下面列出了ClientBase定义的全部的构造函数。

protected ClientBase():这个构造函数没有任何的参数,它用于Endpoint的信息全部存放于Config

protected ClientBase(InstanceContext callbackInstance):指定一个Callback instance用于Service回调Client代码,这用Deplex Communication。

protected ClientBase(string EndpointConfigurationName):指定一个ID,它标识configuration 文件中定义的某一个Endpoint。这个方法在使用不同的Endpoint调用同一个Service的情况下用到的比较多。

ClientBase(Binding Binding, EndpointAddress remoteAddress);显示的指定Binding 和Address
ClientBase(InstanceContext callbackInstance, string EndpointConfigurationName)
ClientBase(string EndpointConfigurationName, EndpointAddress remoteAddress)
ClientBase(string EndpointConfigurationName, string remoteAddress)
ClientBase(InstanceContext callbackInstance, Binding Binding, EndpointAddress remoteAddress)
ClientBase(InstanceContext callbackInstance, string EndpointConfigurationName, EndpointAddress

remoteAddress)
ClientBase(InstanceContext callbackInstance, string EndpointConfigurationName, string

remoteAddress)
介绍完ClientBase后, 我们来创建我们自己的CalculatorClient。下面的相应的Code


using System;

using System.Collections.Generic;

using System.Text;

using System.ServiceModel;

using System.ServiceModel.Channels;

using Artech.WCFService.Contract;

namespace Artech.WCFService.Client

{

class CalculatorClient:ClientBase,ICalculator

{

internal CalculatorClient()

: base()

{ }

#region ICalculator Members

public double Add(double x, double y)

{

return this.Channel.Add(x, y);

}

#endregion

}

}

上面的例子中我们仅仅定义了一个无参的构造函数,因为我们会把所有的Endpoint信息放在Config文件里面:

http://localhost:8080/WCFService/CalculatorService " Binding="basicHttpBinding"

contract="Artech.WCFService.

Contract.ICalculator" />

然后我们再Client Project中的Program class里面通过这样的方式调用CalculatorService;

using System;

using System.Collections.Generic;

using System.Text;

using System.ServiceModel;

using System.ServiceModel.Channels;

using Artech.WCFService.Contract;

namespace Artech.WCFService.Client

{

class Program

{

static void Main(string[] args)

{

try

{

using (CalculatorClient caluclator = new CalculatorClient())

{

Console.WriteLine("Begin to invocate the calculator Service");

Console.WriteLine("x + y = {2} where x = {0} and y = {1}", 1,

2, caluclator.Add(1, 2)); 

Console.Read();

}

}

}

catch (Exception ex)

{

Console.WriteLine("StackTrace:{0}", ex.StackTrace);

Console.WriteLine("Message:{0}", ex.Message);

Console.Read();

}           

}       

}

【相关文章】

【责任编辑:碧海蓝天 TEL:(010)68476606】


共4页: 上一页 [1] [2] [3] 4
【内容导航】
专题
XML可扩展标识语言详解
体验Visual Studio 2008的魅力
Visual Studio 2005开发基础
测试开发人员参考手册
甲骨文85亿美元收购BEA
我也说两句

匿名发表

(如果看不清请点击图片进行更换)


中 国 领 先 的 IT 技 术 网 站 ·
技 术 成 就 梦 想
·Java基础教程 (查看69912次)
·UML类图详解 (查看63757次)
·UML统一建模语言 (查看34694次)
·C#技术开发指南 (查看33015次)
·Java编程开发手册 (查看31041次)
·Java编程开发手册 (1196个砖)
·Java基础教程 (429个砖)
·C#技术开发指南 (309个砖)
·.NET开发手册 (240个砖)
·PB开发教程 (223个砖)
·Java编程开发手册 (654个好)
·Java基础教程 (574个好)
·.NET开发手册 (271个好)
·PB开发教程 (212个好)
·Delphi开发技术手册 (188个好)
订阅技术快讯
电子杂志下载
名称:SQL Server数据库管理精品黄皮书
简介:书中文章经过精挑细选,便于用户能根据自己的实际工作和学习,快速在本书寻找到相关资料。内容涵盖了SQL Server的安装与升级、语句查询、数据备份和恢复、自动化任务、数据同步、数据字典、安全和预防、性能和优化、集群等各方面应用信息,以及DBA管理人员在数据库管理工作中
名称:2007路由技术大全
简介:《2007路由技术大全》由51CTO.com网站特别策划制作,该书包括路由器技术、路由器产品、路由器配置、安全设置、路由器故障处理、路由器密码恢复,以及广大网友在实践使用中的心得经验和技巧文章,内容注重实用性,适用于初学者入门,也适合多年从业者提高,是一本实践和理论完
名称:网络安全精品应用黄皮书
简介:《2007精品网络安全黄皮书》包括了9个大类24个小类, 800余篇文章,内容包含了熊猫烧香病毒、DDOS攻击、ARP病等热点问题的介绍及解决方案。从病毒查杀、防范、系统、数据等各方面的安全设置到黑客技术的了解、防范,涉及到了安全应用的全部领域, 由浅至深内容全面。