学习 WCF (3)--开发WCF客户程序

来源:互联网 发布:数据库技术研究方向 编辑:程序博客网 时间:2024/04/29 17:40

前篇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

 


<div>
        
<asp:GridView ID="GridView1" runat="server" BackColor="White" 
            BorderColor
="#DEDFDE" BorderStyle="None" BorderWidth="1px" CellPadding="4" 
            DataSourceID
="ObjectDataSource1" ForeColor="Black" GridLines="Vertical">
            
<RowStyle BackColor="#F7F7DE" />
            
<FooterStyle BackColor="#CCCC99" />
            
<PagerStyle BackColor="#F7F7DE" ForeColor="Black" HorizontalAlign="Right" />
            
<SelectedRowStyle BackColor="#CE5D5A" Font-Bold="True" ForeColor="White" />
            
<HeaderStyle BackColor="#6B696B" Font-Bold="True" ForeColor="White" />
            
<AlternatingRowStyle BackColor="White" />
        
</asp:GridView>
    
    
<asp:ObjectDataSource ID="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的代码改为:

<asp:ObjectDataSource ID="ObjectDataSource1" runat="server" TypeName="代理类名" SelectMethod="调用的方法名">
    
</asp:ObjectDataSource>

这样就可以了。

3.编程方案

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


[System.CodeDom.Compiler.GeneratedCodeAttribute("System.ServiceModel""3.0.0.0")]
public partial class StuServiceContractClient : System.ServiceModel.ClientBase<IStuServiceContract>, IStuServiceContract
{
    
    
public StuServiceContractClient()//默认构造函数
    {
    }
    
    
public StuServiceContractClient(string endpointConfigurationName) : 
            
base(endpointConfigurationName)//参数是一个字符串,包括端点的配置名
    {
    }
    
    
public StuServiceContractClient(string endpointConfigurationName, string remoteAddress) : 
            
base(endpointConfigurationName, remoteAddress)//端点的配置名,端点的网络地址
    {
    }
    
    
public StuServiceContractClient(string endpointConfigurationName, System.ServiceModel.EndpointAddress remoteAddress) : 
            
base(endpointConfigurationName, remoteAddress)//端点的配置名,端点地址的EndPointAddress对象
    {
    }
    
    
public StuServiceContractClient(System.ServiceModel.Channels.Binding binding, System.ServiceModel.EndpointAddress remoteAddress) : 
            
base(binding, remoteAddress)//端点绑定的Binding对象,端点地址的EndPointAddress对象

    {
    }
    
    
public void AddStudent(WCFStudent.Student stu)
    {
        
base.Channel.AddStudent(stu);
    }
    
    
public WCFStudent.Student[] GetStudent()
    {
        
return base.Channel.GetStudent();
    }
}

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

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

 

原创粉丝点击