程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> Visual Basic語言 >> VB.NET >> VB.Net編程實現Web Service的基礎

VB.Net編程實現Web Service的基礎

編輯:VB.NET

Web Service目前可是目前計算機界一個非常流行的技術了,以至於有些人把Web Service列入目前最熱門的十大技術之一。本文的主要內容是簡要介紹一下Web Service的相關知識,以及使用Visual Basic .Net實現Web Services的具體方法和典型步驟。

Web Service目前可是目前計算機界一個非常流行的技術了,以至於有些人把Web Service列入目前最熱門的十大技術之一。的確隨著互聯網的廣泛應用和發展,尤其是電子商務的發展,出於互聯網上各種復雜的應用系統和對更高安全性的要求,Web Service的橫空出世的確滿足了當前這些的要求和需要,其中的原因在下文中有詳細的介紹。本文的主要內容是簡要介紹一下Web Service的相關知識,以及使用Visual Basic .Net實現Web Services的具體方法和典型步驟。

一.Web Service為何物,我們為什麼需要它:

Web Service的主要功能就是可以實現實現跨平台的功能調用。同時由於Web Service中使用XML來進行數據交換,所以在使用Web Service時不用擔心防火牆的影響。由於Web Service集成了各種功能,並提供了一個友好的界面。所以在Web Service能夠實現軟件的重用。

另外Web Service的調用非常簡單,簡而言之調用互聯網上的Web Service 就如同調用本地的組件一樣簡單,就是通過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所示調整窗體中各組件屬性的數值:

組件類型 組件名稱 屬性 設置結果 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

五.總結:

本文介紹了Web Service在目前大行其道的原因,並結合二個示例介紹Visual Basic .Net在Web Service的實現和調用上的強大功能,正如本文前面所說,Visual Studio .Net的出現使得web Service的這項原先繁雜的工作變得異常的簡單,因為Visual Basic .Net已經替代我們作了這些描述性的、基礎的底層工作,以至於你不需要了解為什麼,只需要知道你要實現什麼就可以編寫、調用Web Service了。 在實現Web Service在數據庫方面應用所顯示

Web Service雖然以其強大功能和其優越性正在受到越來越多人的青睐,但也不可以回避它的缺點,譬如其速度慢就使得我幾乎無法忍受。等等這樣的原因也決定了Web Service並不適用於所有的環境。如單機運行程序和局域網上的同構應用程序等就盡量不采用Web Service方式。本文只是展示了Visual Studio .Net在Web Service上的初步用途,至於更復雜的使用、調用方法只能在後續的文章中介紹了,感興趣的朋友,我們下一文再見!

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved