`
lovnet
  • 浏览: 6704727 次
  • 性别: Icon_minigender_1
  • 来自: 武汉
文章分类
社区版块
存档分类
最新评论

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

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:ObjectDataSourceID="ObjectDataSource1"runat="server"TypeName="代理类名"SelectMethod="调用的方法名">
</asp:ObjectDataSource>

这样就可以了。

3.编程方案

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

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服务的应用。就写到这啦。

分享到:
评论

相关推荐

    WCF.Multi-Layer.Services.Development.with.Entity.Framework.4th.Edition

    Title: WCF Multi-Layer Services Development with Entity Framework, 4th Edition Author: Mike Liu Length: 388 pages Edition: 4 Language: English Publisher: Packt Publishing Publication Date: 2014-10-27 ...

    Programming WCF Services (4th).pdf 2015 第4版 无水印

    this guide provides unique insight, rather than documentation, to help you learn the topics and skills you need for building maintainable, extensible, and reusable WCF-based applications. Authors ...

    Programming WCF Services(THIRD EDITION)

    WCF-based applications, illustrating how to take advantage of built-in features such as service hosting, instance management, concurrency management, transactions, dis- connected queued calls, ...

    Programming.WCF.Services.1491944838

    Hailed as the definitive treatment of WCF, this book provides unique insight, rather than documentation, to help you learn the topics and skills you need for building WCF-based applications that are ...

    WCF & WPF 聊天程序源码

    WCF & WPF 聊天程序源码 For those that have read some of my other CodeProject articles you will probably know, that I am not shy about trying out new technologies. But one good thing about that is ...

    Programming WCF Services(O'Reilly,4ed,2015)

    this guide provides unique insight, rather than documentation, to help you learn the topics and skills you need for building maintainable, extensible, and reusable WCF-based applications. Authors ...

    Programming WCF Services, 3rd Edition

    Hailed as the definitive treatment of WCF, this book provides unique insight, rather than documentation, to help you learn the topics and skills you need for building WCF-based applications that are ...

    Packt.WCF.4.5.Multi-Layer.Services.Development.with.Entity.Framework.Dec.2012

    Explore and learn different debugging options for a WCF service. Build a multi-layer real-world WCF service from scratch to understand every layer of a WCF service and apply it to your real work. ...

    Programming WCF Services 4

    Learn WCF’s architecture and essential building blocks, including key concepts such as reliability and transport sessions Use built-in features such as service contracts, instance and concurrency ...

    Windows Communication Foundation 4 Step by Step

    微软出版的Windows Communication Foundation 4 Step by Step,一步一步学WCF英文原版 --------- Your hands-on, step-by-step guide to building connected, service-oriented applications. Teach yourself the...

    Programming.WCF.Services.3rd.Edition

    Developers and architects will learn not only the "how" of WCF programming, but also relevant design guidelines, best practices, and pitfalls. Original techniques and utilities provided by the author...

    WCF服务编程 (第3版)

    Hailed as the definitive treatment of WCF, this book provides unique insight, rather than documentation, to help you learn the topics and skills you need for building WCF-based applications that are ...

    [WCF] WCF 服务编程 第1版 (英文版)

    Developers and architects will learn not only the "how" of WCF programming, but also relevant design guidelines, best practices, and pitfalls. Original techniques and utilities provided by the author...

    Wrox.Professional.WCF.4

    Written by a team of Microsoft MVPs and WCF experts, this book explains how the pieces of WCF 4.0 build on each other to provide a comprehensive framework to support distributed enterprise ...

    Professional WCF 4: Windows Communication Foundation with .NET 4

    WCF is the technology in .NET that you use to build service - oriented applications, to exchange messages in various communication scenarios, and to run workfl ows built from service operations. With...

    Microsoft Windows Communication Foundation 4.0 Cookbook for Developing SOA Applications

    One big plus is that you will learn to extend the existing WCF framework to achieve advanced functionality. You will find a dedicated chapter for RESTful and AJAX-enabled service development. Moving ...

    Microsoft BizTalk Server 2010 Patterns

    will learn both polling and query approaches to working with the WCF-SQL Adapter and how to expose different services in different manners from BizTalk. Chapter 9, Expanding the Solution with Services...

Global site tag (gtag.js) - Google Analytics