×

VB.Net编程实现Web Service的基础

Kalet Kalet 发表于2009-03-20 12:00:14 浏览150 评论0

抢沙发发表评论

  WebService目前可是目前计算机界一个非常流行的技术了,以至于有些人把WebService列入目前最热门的十大技术之一。的确随着互联网的广泛应用和发展,尤其是电子商务的发展,出于互联网上各种复杂的应用系统和对更高安全性的要求,WebService的横空出世的确满足了当前这些的要求和需要,其中的原因在下文中有详细的介绍。本文的主要内容是简要介绍一下WebService的相关知识,以及使用VisualBasic.Net实现WebServices的具体方法和典型步骤。


  一。WebService为何物,我们为什么需要它:


  WebService的主要功能就是可以实现实现跨平台的功能调用。同时由于WebService中使用XML来进行数据交换,所以在使用WebService时不用担心防火墙的影响。由于WebService集成了各种功能,并提供了一个友好的界面。所以在WebService能够实现软件的重用。

VB.Net编程实现Web Service的基础

  另外WebService的调用非常简单,简而言之调用互联网上的WebService就如同调用本地的组件一样简单,就是通过HTTP协议来调用互联网上的组件。至于具体的调用方法,请参阅本文第五节第七段的内容。所以Web Service就是互联网上的组件调用。


  二。和Web Service相关的标准、协议:


  Web Service是通过一系列标准和协议来保证和程序之间的动态连接和实现其安全调用的。其中主要的标准和协议是:XML、WSDL、SOAP、HTTP、UDDI.下面就简要介绍这些标准和协议。


  1. XML:Web Service之间和Web Service和应用程序之间都是采用XML进行数据交换的。Web Service由于基于了XML,,这样Web Service在具备XML带来的优势的同时,也拥有了由于XML所带来的缺点。其中XML所带来的最重要缺点就是Web Service将大量的占有CPU的资源,因为XML数据要经过多步处理才能被系统使用。所以,即使调用一个功能较小的Web Service,也会感觉速度很慢,所以网络中对运行Web Service的主机要求是很高的。


  2. HTTP:应用程序是提供HTTP协议来调用Web Service的。所以HTTP在Web Service调用过程中,起着通道的作用。


  3. WSDL::是Web Service描述语言的简写。它是XML格式,其作用是描述Web Service,指示应用程序和与Web Servie交互的方法。当实现了某种Web Service服务时,为了让别的程序调用,就必须告诉此Web Service的接口。如:服务名称,服务所在的机器名称,监听端口号,传递参数的类型等等。WSDL就是规定了有关Web Services描述的标准。


  4. UDDI:是Universal Description, Discovery, and Integration的缩写。简单说,UDDI用于集中存放和查找WSDL描述文件,起着目录服务器的作用。


  5. SOAP:是"Simple Object Access Protocol"的缩写,即:简单对象访问协议。SOAP是一种消息传递的协议,它规定了Web Services之间传递信息的方式。


  三。本文章的程序设计、调试和运行的环境:


  (1)。微软公司视窗2003中文企业版。


  (2)。Visual Studio .Net 2003企业构建版,。Net FrameWork SDK 1.1版本号4322.


  (3)。IIS服务启动。


  四。Visual Basic .Net实现Web Service:


  。Net 的大的推动了Web Service的发展,而Visual Studio .Net的出现又极大的推动了Web Service的的广泛应用。在Visual Studio .Net推出之前,编写一个Web Service是一项非常复杂的工作,同样调用这个Web Service也十分麻烦。由于Visual Studio .Net对Web Service提供了较强的支持,很多细致、烦杂的工作都由Visual Studio .Net自动完成了。这样就使得上述工作变得非常简单。甚至不了解Web Service和其相关的标准、协议,也可以使用Visual Studio .Net编写Web Service,并使用这个Web Service.下面就来用Visual Basic .Net实现一个Web Service,此Web Service和数据库相关,数据库类型选用的是Sql Server.此Web Service提供了二个函数功能调用,其一名称为Binding,用以实现数据绑定,其二名称为Update,用以更新数据库中的数据。


  以下就是Visual Basic .Net实现此Web Service的具体步骤:


  1. 启动Visual Studio .Net.


  2. 选择菜单「文件」|「新建」|「项目」后,弹出「新建项目」对话框。


  3. 将「项目类型」设置为「Visual Basic项目」。


  4. 将「模板」设置为「ASP.NET Web 服务」。


  5. 在「位置」的文本框中输入"http://localhost/UpdateDataWebService"后,单击「确定」按钮,这样在Visual Studio .Net就会计算机Internet信息服务的默认目录中创建一个名称为"UpdateDataWebService"文件夹,里面存放的是此项目的文件。具体如图01所示:




  图01:创建Web Service项目对话框


  6. 选中「解决方案资源管理器」中的"Service1.asmx"文件,单击鼠标右键,在弹出的菜单中选择「查看代码」,则进入Service1.asmx.vb的编辑界面。


  7. 在Service1.asmx……vb的首部,在导入命名空间的代码区中添加下列代码,下列代码作用是导入命名空间System.Data.SqlClient:


  Imports System.Data.SqlClient


  8. 在Service1.asmx……vb文件的"Public Class Service1 Inherits System.Web.Services.WebService"代码后,添加下列代码,下列代码是在Web Service中定义二个功能调用:


<WebMethod ( ) > Public Function Binding ( ) As DataSet
 Dim con As New SqlConnection ( "Server = localhost ; uid = sa ; pwd = ; database = northwind" )
 Dim daCust As New SqlDataAdapter ( "Select * From Customers" , con )
 Dim ds As New DataSet ( )
 daCust.Fill( ds , "Cust" )
 Return ds
End Function
<WebMethod ( ) > Public Function Update ( ByVal ds As DataSet ) As DataSet
 Dim con As New SqlConnection ( "Server = localhost ; uid = sa ; pwd = ; database = northwind " )
 Dim daCust As New SqlDataAdapter ( "Select * From Customers" , con )
 Dim cbCust As New SqlCommandBuilder ( daCust )
 daCust.Update ( ds , "Cust" )
 Return ds
End Function


  9. 保存上述的修改,一个简单的操作Sql Server数据库的Web Service就完成了,此时单击快捷键F5,此Web Service就开始运行,并可以对外提供服务了。具体如图02所示:



  图02:Web Service提供服务是的界面


  Service1.asmx.vb的代码清单如下:


Imports System.Web.Services
Imports System.Data.SqlClient
<WebService ( Namespace := "http://tempuri.org/" ) > _
Public Class Service1
Inherits System.Web.Services.WebService
<WebMethod ( ) > Public Function Binding ( ) As DataSet
'Modify this Connection string to use your SQL Server and log on.
 Dim con As New SqlConnection ( "Server=localhost;uid=sa;pwd=;database=northwind" )
 Dim daCust As New SqlDataAdapter ( "Select * From Customers" , con )
 Dim ds As New DataSet ( )
 daCust.Fill ( ds , "Cust" )
 Return ds
End Function
<WebMethod ( ) > Public Function Update ( ByVal ds As DataSet ) As DataSet
 Dim con As New SqlConnection ( "Server=localhost;uid=sa;pwd=;database=northwind" )
 Dim daCust As New SqlDataAdapter ( "Select * From Customers" , con )
 Dim cbCust As New SqlCommandBuilder ( daCust )
 daCust.Update ( ds , "Cust" )
 Return ds
End Function
#Region " Web 服务设计器生成的代码 "
Public Sub New ( )
 MyBase.New ( )
 '该调用是 Web 服务设计器所必需的。
 InitializeComponent ( )
 '在 InitializeComponent ( ) 调用之后添加您自己的初始化代码
End Sub
'Web 服务设计器所必需的
Private components As System.ComponentModel.IContainer
 '注意:以下过程是 Web 服务设计器所必需的
 '可以使用 Web 服务设计器修改此过程。
 '不要使用代码编辑器修改它。
 <System.Diagnostics.DebuggerStepThrough ( ) > Private Sub InitializeComponent ( )
 components = New System.ComponentModel.Container ( )
End Sub
Protected Overloads Overrides Sub Dispose ( ByVal disposing As Boolean )
'CODEGEN: 此过程是 Web 服务设计器所必需的
'不要使用代码编辑器修改它。
If disposing Then
 If Not ( components Is Nothing ) Then
  components.Dispose ( )
 End If
End If
MyBase.Dispose ( disposing )
End Sub
#End Region
' Web 服务示例
' HelloWorld ( ) 示例服务返回字符串 Hello World。
' 若要生成项目,请取消注释以下行,然后保存并生成项目。
' 若要测试此 Web 服务,请确保 .asmx 文件为起始页
' 并按 F5 键。
'
'<WebMethod ( ) > Public Function HelloWorld ( ) As String
' HelloWorld = "Hello World"
' End Function
End Class


  下面就来介绍Visual Basic .Net中使用这个Web Service提供的服务来更新数据库的实现方法。


  五.在Visual Basic .Net调用Web Service提供的服务:


  当Web Service已经处于对外提供服务状态,Visual Basic .Net就可以通过HTTP"调用"来使用这些服务了。当然前提是要了解Web Service对外提供服务所对应的URL,当了解到Web Service对应的URL后,Visual Basic .Net就像是使用本地的类库一样使用Web Service中提供的各种功能。所以有些人说,Web Service从实质上说,就是通过HTTP调用远程组件的一种方式。在Visual Basic .Net具体实现加入Web Service可参阅下面步骤中的第七步。


  在下面介绍的这个数据库应用程序是通过使用上面的Web Service中提供的"Binding"服务,对程序中DataGrid组件实现数据绑定,提供使用Web Service中提供的"Update"服务,通过程序中的DataGrid来修改数据库。下面就是Visual Basic .Net中使用Web Service提供服务来编写数据库应用程序的具体步骤,:


  1. 启动Visual Studio .Net。


  2. 选择菜单【文件】|【新建】|【项目】后,弹出【新建项目】对话框。


  3. 将【项目类型】设置为【Visual Basic项目】。


  4. 将【模板】设置为【Windows应用程序】。


  5. 在【名称】文本框中输入【TestWebService】。


  6. 在【位置】的文本框中输入【E:\VS.NET项目】,然后单击【确定】按钮,这样在"E:\VS.NET项目"中就产生了名称为"TestWebService"文件夹,里面存放的就是TestWebService项目的所有文件。


  7. 选择【解决方案资源管理器】|【引用】后,单击鼠标右键,在弹出的菜单中选择【添加Web 引用】,在弹出的【添加Web引用】对话框中的【地址】文本框中输入"http://localhost/ UpdateDataWebService /Service1.asmx "后,单击回车键后,可得图03所示界面。单击图03中【添加引用】按钮,则在【TestWebService】项目中加入了Web引用。请注意"http://localhost/ UpdateDataWebService /Service1.asmx "就是上面完成的Web Service对外提供服务的URL地址,具体可参阅图02所示:



  图03:在【TestWebService】添加Web Service提供的服务


  8. 从【工具箱】中的【Windows窗体组件】选项卡中往Form1窗体中拖入下列组件,并执行相应的操作:


  一个DataGrid组件。


  二个Button组件,,分别是Button1至Button2,并在这二个Button组件拖入Form1的设计窗体后,分别双击它们,则系统会在Form1.vb文件分别产生这二个组件的Click事件对应的处理代码。


  9. 按照表01所示调整窗体中各组件属性的数值:













VB.Net编程实现Web Service的基础

























组件类型

组件名称

属性

设置结果

Form

Form1

Text

测试Web Service

Form1

MaximizeBox

False

Form1

FormBorderStyle

FixedSingle

Button

Button1

Text

绑定

Button1

FlatStyle

Flat

Button2

Text

修改

Button2

FlatStyle

Flat
 


  表01:【TestWebService】项目中组件的主要属性及其对应数值


  在调整完组件属性值后,再按照图04所示调整组件的位置和排列顺序:



  图04:【TestWebService】项目中组件排列位置和顺序


  10. 把Visual Studio .Net的当前窗口切换到Form1.vb的代码编辑窗口,并用下列代码替换Form1.vb中的Button1的Click事件对应的处理代码,下列代码功能是使用Web Service中提供的"Binding"服务对DataGrid组件实现数据绑定:


Private Sub Button1_Click ( ByVal sender As System.Object , ByVal e As System.EventArgs ) Handles Button1.Click
 Dim MyService As New localhost.Service1 ( )
 DataGrid1.DataSource = MyService.Binding ( )
 DataGrid1.DataMember = "Cust"
End Sub


  11. 用下列代码替换Form1.vb中的Button2的Click事件对应的处理代码,下列代码功能是使用Web Service中提供的"Update"服务实现通过DataGrid来修改数据库数据:


Private Sub Button2_Click ( ByVal sender As System.Object , ByVal e As System.EventArgs ) Handles Button2.Click
 Dim MyService As New localhost.Service1 ( )
 Dim ds As DataSet = DataGrid1.DataSource
 Dim dsChanges As DataSet = ds.GetChanges ( )
 If Not ( dsChanges Is Nothing ) Then
  ds.Merge ( MyService.Update ( dsChanges ) , True )
 End If
End Sub


  12. 至此, 【TestWebService】项目的全部工作就完成了,调用Web Service是不是很简单。此时单击快捷键F5运行程序后。单击程序中的【绑定】按钮就会对程序中的DataGrid组件实现数据绑定,单击程序中的【修改】按钮,则程序会根据DataGrid中的内容来更新数据库,图05就是【TestWebService】的运行界面:



  图05:【TestWebService】的运行界面


  13. Form1.vb的代码清单如下:


Public Class Form1
Inherits System.Windows.Forms.Form
#Region " Windows 窗体设计器生成的代码 "
Public Sub New ( )
 MyBase.New ( )
 '该调用是 Windows 窗体设计器所必需的。
 InitializeComponent ( )
 '在 InitializeComponent ( ) 调用之后添加任何初始化
End Sub
'窗体重写处置以清理组件列表。
Protected Overloads Overrides Sub Dispose ( ByVal disposing As Boolean )
 If disposing Then
  If Not ( components Is Nothing ) Then
   components.Dispose ( )
  End If
 End If
 MyBase.Dispose ( disposing )
End Sub
'Windows 窗体设计器所必需的
Private components As System.ComponentModel.IContainer
 '注意:以下过程是 Windows 窗体设计器所必需的
 '可以使用 Windows 窗体设计器修改此过程。
 '不要使用代码编辑器修改它。
 Friend WithEvents Button1 As System.Windows.Forms.Button
 Friend WithEvents Button2 As System.Windows.Forms.Button
 Friend WithEvents DataGrid1 As System.Windows.Forms.DataGrid
 <System.Diagnostics.DebuggerStepThrough ( ) > Private Sub InitializeComponent ( )
 Me.Button1 = New System.Windows.Forms.Button ( )
 Me.Button2 = New System.Windows.Forms.Button ( )
 Me.DataGrid1 = New System.Windows.Forms.DataGrid ( )
 CType ( Me.DataGrid1 , System.ComponentModel.ISupportInitialize ) .BeginInit ( )
 Me.SuspendLayout ( )
 Me.Button1.FlatStyle = System.Windows.Forms.FlatStyle.Flat
 Me.Button1.Location = New System.Drawing.Point ( 56 , 216 )
 Me.Button1.Name = "Button1"
 Me.Button1.Size = New System.Drawing.Size ( 75 , 32 )
 Me.Button1.TabIndex = 0
 Me.Button1.Text = "绑定"
 Me.Button2.FlatStyle = System.Windows.Forms.FlatStyle.Flat
 Me.Button2.Location = New System.Drawing.Point ( 168 , 216 )
 Me.Button2.Name = "Button2"
 Me.Button2.Size = New System.Drawing.Size ( 75 , 32 )
 Me.Button2.TabIndex = 1
 Me.Button2.Text = "修改"
 Me.DataGrid1.DataMember = ""
 Me.DataGrid1.Dock = System.Windows.Forms.DockStyle.Top
 Me.DataGrid1.HeaderForeColor = System.Drawing.SystemColors.ControlText
 Me.DataGrid1.Name = "DataGrid1"
 Me.DataGrid1.Size = New System.Drawing.Size ( 292 , 192 )
 Me.DataGrid1.TabIndex = 2
 Me.AutoScaleBaseSize = New System.Drawing.Size ( 6 , 14 )
 Me.ClientSize = New System.Drawing.Size ( 292 , 273 )
 Me.Controls.AddRange ( New System.Windows.Forms.Control ( ) {Me.DataGrid1 , Me.Button2 , Me.Button1} )
 Me.Name = "Form1"
 Me.Text = "测试Web Service"
 CType ( Me.DataGrid1 , System.ComponentModel.ISupportInitialize ) .EndInit ( )
 Me.ResumeLayout ( False )
End Sub
#End Region
Private Sub Button1_Click ( ByVal sender As System.Object , ByVal e As System.EventArgs ) Handles Button1.Click
 Dim MyService As New localhost.Service1 ( )
 DataGrid1.DataSource = MyService.Binding ( )
 DataGrid1.DataMember = "Cust"
End Sub
Private Sub Button2_Click ( ByVal sender As System.Object , ByVal e As System.EventArgs ) Handles Button2.Click
 Dim MyService As New localhost.Service1 ( )
 Dim ds As DataSet = DataGrid1.DataSource
 Dim dsChanges As DataSet = ds.GetChanges ( )
 If Not ( dsChanges Is Nothing ) Then
  ds.Merge ( MyService.Update ( dsChanges ) , True )
 End If
End Sub
End Class


  六。总结:

VB.Net编程实现Web Service的基础

  本文介绍了Web Service在目前大行其道的原因,并结合二个示例介绍Visual Basic .Net在Web Service的实现和调用上的强大功能,正如本文前面所说,Visual Studio .Net的出现使得web Service的这项原先繁杂的工作变得异常的简单,因为Visual Basic .Net已经替代我们作了这些描述性的、基础的底层工作,以至于你不需要了解为什么,只需要知道你要实现什么就可以编写、调用Web Service了。 在实现Web Service在数据库方面应用所显示。



群贤毕至

访客