Learn WCF (3)--开发WCF客户程序

前篇Learn WCF (2)--开发WCF服务介绍了开发服务程序,这篇开发一个客户程序,主要有三种方案:

添加一个Web引用

使用svcutil.exe工具

编程方案

1.添加一个Web引用

这个和添加引用Web服务的方法基本一致,在添加引用的对话框中输入URL:http://localhost:39113/WCFServiceText/WCFStudentText.svc

为WCF起个名字,点击添加引用按钮将会完成如下的任务:

(1)从指定的URL为学生管理服务下载WSDL文件

(2)生成代理类WCFStudentText,它是服务器WCFStudentText的代理,实现了服务器契约IStuServiceContract。

(3)生成响应的配置设置

现在我们就可以用代理类WCFStudentText与学生信息管理服务进行通信了。在站点中添加一个页面,放入一个GridView和ObjectDataSource

ContractedBlock.gif ExpandedBlockStart.gif Code
<!--<br /><br />Code highlighting produced by Actipro CodeHighlighter (freeware)<br />http://www.CodeHighlighter.com/<br /><br />--><div>
<asp:GridViewID="GridView1"runat="server"BackColor="White"
BorderColor
="#DEDFDE"BorderStyle="None"BorderWidth="1px"CellPadding="4"
DataSourceID
="ObjectDataSource1"ForeColor="Black"GridLines="Vertical">
<RowStyleBackColor="#F7F7DE"/>
<FooterStyleBackColor="#CCCC99"/>
<PagerStyleBackColor="#F7F7DE"ForeColor="Black"HorizontalAlign="Right"/>
<SelectedRowStyleBackColor="#CE5D5A"Font-Bold="True"ForeColor="White"/>
<HeaderStyleBackColor="#6B696B"Font-Bold="True"ForeColor="White"/>
<AlternatingRowStyleBackColor="White"/>
</asp:GridView>

<asp:ObjectDataSourceID="ObjectDataSource1"runat="server"TypeName="StuWcfService.WCFStudentText"SelectMethod="GetStudent">
</asp:ObjectDataSource>
</div>

ObjectDataSourse的优点是不需要编写一行代码就可以调用代理类中的方法。这里应当注意的是TypeName,SelectMethod两个重要属性的写法,必须与代理类一致。

2.使用svcutil.exe工具

WCF带有一个工具svcutil.exe,它自动从指定的URL下载WSDL文档,为代理类生成并保存一个指定的文件中,用相应的配置设置生成相应的配置文件,执行下面命令:

svcutil.exe工具将自动完成下列工作:

(1)通过URL下载元数据文档(WSDL文档)。

(2)为代理类生成代码,并将代码保持到WCFStudentServiceClient.cs文件中。

(3)生成配置设置并将其保存到Web.config中。

检查svcutil.exe多运行的目录,就会看到文件WCFStudentServiceClient.cs和Web.config。文件中的代码这里就不考出来了,大家可以自己试一下。将这两个文件导入到一个站点中。

只需将ObjectDataSource的代码改为:

<!--<br /><br />Code highlighting produced by Actipro CodeHighlighter (freeware)<br />http://www.CodeHighlighter.com/<br /><br />--> < asp:ObjectDataSource ID ="ObjectDataSource1" runat ="server" TypeName ="代理类名" SelectMethod ="调用的方法名" >
</ asp:ObjectDataSource >

这样就可以了。

3.编程方案

这里我们主要根据svcutil.exe给我们生成的文件,手动的编写自己的代码,实现同样的效果。svcutil.exe生成的代理类文件中包含了5个构造函数,目的是为了满足不同情况下的需要:

ContractedBlock.gif ExpandedBlockStart.gif Code
<!--<br /><br />Code highlighting produced by Actipro CodeHighlighter (freeware)<br />http://www.CodeHighlighter.com/<br /><br />-->[System.CodeDom.Compiler.GeneratedCodeAttribute("System.ServiceModel","3.0.0.0")]
publicpartialclassStuServiceContractClient:System.ServiceModel.ClientBase<IStuServiceContract>,IStuServiceContract
{

publicStuServiceContractClient()//默认构造函数
{
}

publicStuServiceContractClient(stringendpointConfigurationName):
base(endpointConfigurationName)//参数是一个字符串,包括端点的配置名
{
}

publicStuServiceContractClient(stringendpointConfigurationName,stringremoteAddress):
base(endpointConfigurationName,remoteAddress)//端点的配置名,端点的网络地址
{
}

publicStuServiceContractClient(stringendpointConfigurationName,System.ServiceModel.EndpointAddressremoteAddress):
base(endpointConfigurationName,remoteAddress)//端点的配置名,端点地址的EndPointAddress对象
{
}

publicStuServiceContractClient(System.ServiceModel.Channels.Bindingbinding,System.ServiceModel.EndpointAddressremoteAddress):
base(binding,remoteAddress)//端点绑定的Binding对象,端点地址的EndPointAddress对象

{
}

publicvoidAddStudent(WCFStudent.Studentstu)
{
base.Channel.AddStudent(stu);
}

publicWCFStudent.Student[]GetStudent()
{
returnbase.Channel.GetStudent();
}
}

其中端点的配置名是<client>下<endpoint>子元素下的name属性。

我们也可以编写同样的代码。来实现WCF服务的应用。就写到这啦。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值