熱點推薦:
您现在的位置: 電腦知識網 >> 編程 >> .NET編程 >> 正文

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

2022-06-13   來源: .NET編程 

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

  一WebService為何物我們為什麼需要它

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

  另外WebService的調用非常簡單簡而言之調用互聯網上的WebService就如同調用本地的組件一樣簡單就是通過HTTP協議來調用互聯網上的組件至於具體的調用方法請參閱本文第五節第七段的內容所以Web Service就是互聯網上的組件調用

  二和Web Service相關的標准協議

  Web Service是通過一系列標准和協議來保證和程序之間的動態連接和實現其安全調用的其中主要的標准和協議是XMLWSDLSOAPHTTPUDDI下面就簡要介紹這些標准和協議

   XMLWeb Service之間和Web Service和應用程序之間都是采用XML進行數據交換的Web Service由於基於了XML這樣Web Service在具備XML帶來的優勢的同時也擁有了由於XML所帶來的缺點其中XML所帶來的最重要缺點就是Web Service將大量的占有CPU的資源因為XML數據要經過多步處理才能被系統使用所以即使調用一個功能較小的Web Service也會感覺速度很慢所以網絡中對運行Web Service的主機要求是很高的

   HTTP應用程序是提供HTTP協議來調用Web Service的所以HTTP在Web Service調用過程中起著通道的作用

   WSDL是Web Service描述語言的簡寫它是XML格式其作用是描述Web Service指示應用程序和與Web Servie交互的方法當實現了某種Web Service服務時為了讓別的程序調用就必須告訴此Web Service的接口服務名稱服務所在的機器名稱監聽端口號傳遞參數的類型等等WSDL就是規定了有關Web Services描述的標准

   UDDI是Universal Description Discovery and Integration的縮寫簡單說UDDI用於集中存放和查找WSDL描述文件起著目錄服務器的作用

   SOAPSimple Object Access Protocol的縮寫簡單對象訪問協議SOAP是一種消息傳遞的協議它規定了Web Services之間傳遞信息的方式

  三本文章的程序設計調試和運行的環境

  (微軟公司視窗中文企業版

  (Visual Studio Net 企業構建版Net FrameWork SDK 版本號

  (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的具體步驟

   啟動Visual Studio Net

   選擇菜單「文件」|「新建」|「項目」後彈出「新建項目」對話框

   將「項目類型」設置為「Visual Basic項目」

   將「模板」設置為「ASPNET Web 服務」

   在「位置」的文本框中輸入//localhost/UpdateDataWebService單擊「確定」按鈕這樣在Visual Studio Net就會計算機Internet信息服務的默認目錄中創建一個名稱為UpdateDataWebService文件夾裡面存放的是此項目的文件具體如圖所示

  

  圖創建Web Service項目對話框

   選中「解決方案資源管理器」中的Serviceasmx文件單擊鼠標右鍵在彈出的菜單中選擇「查看代碼」則進入Serviceasmxvb的編輯界面

   在Serviceasmx……vb的首部在導入命名空間的代碼區中添加下列代碼下列代碼作用是導入命名空間SystemDataSqlClient

  Imports SystemDataSqlClient

   在Serviceasmx……vb文件的Public Class Service Inherits SystemWebServicesWebService代碼後添加下列代碼下列代碼是在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 ( )
 daCustFill( 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 )
 daCustUpdate ( ds Cust )
 Return ds
End Function

   保存上述的修改一個簡單的操作Sql Server數據庫的Web Service就完成了此時單擊快捷鍵F此Web Service就開始運行並可以對外提供服務了具體如圖所示:

  

  圖:Web Service提供服務是的界面

  Serviceasmxvb的代碼清單如下:

  Imports SystemWebServices
Imports SystemDataSqlClient
<WebService ( Namespace := ) > _
Public Class Service
Inherits SystemWebServicesWebService
<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 ( )
 daCustFill ( 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 )
 daCustUpdate ( ds Cust )
 Return ds
End Function
#Region Web 服務設計器生成的代碼
Public Sub New ( )
 MyBaseNew ( )
 該調用是 Web 服務設計器所必需的
 InitializeComponent ( )
 在 InitializeComponent ( ) 調用之後添加您自己的初始化代碼
End Sub
Web 服務設計器所必需的
Private components As SystemComponentModelIContainer
 注意以下過程是 Web 服務設計器所必需的
 可以使用 Web 服務設計器修改此過程
 不要使用代碼編輯器修改它
 <SystemDiagnosticsDebuggerStepThrough ( ) > Private Sub InitializeComponent ( )
 components = New SystemComponentModelContainer ( )
End Sub
Protected Overloads Overrides Sub Dispose ( ByVal disposing As Boolean )
CODEGEN: 此過程是 Web 服務設計器所必需的
不要使用代碼編輯器修改它
If disposing Then
 If Not ( components Is Nothing ) Then
  componentsDispose ( )
 End If
End If
MyBaseDispose ( disposing )
End Sub
#End Region
Web 服務示例
HelloWorld ( ) 示例服務返回字符串 Hello World
若要生成項目請取消注釋以下行然後保存並生成項目
若要測試此 Web 服務請確保 asmx 文件為起始頁
並按 F

<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提供服務來編寫數據庫應用程序的具體步驟:

   啟動Visual Studio Net

   選擇菜單【文件】|【新建】|【項目】後彈出【新建項目】對話框

   將【項目類型】設置為【Visual Basic項目】

   將【模板】設置為【Windows應用程序】

   在【名稱】文本框中輸入【TestWebService】

   在【位置】的文本框中輸入【E:\VSNET項目】然後單擊【確定】按鈕這樣在E:\VSNET項目中就產生了名稱為TestWebService文件夾裡面存放的就是TestWebService項目的所有文件

   選擇【解決方案資源管理器】|【引用】後單擊鼠標右鍵在彈出的菜單中選擇【添加Web 引用】在彈出的【添加Web引用】對話框中的【地址】文本框中輸入單擊回車鍵後可得圖所示界面單擊圖中【添加引用】按鈕則在【TestWebService】項目中加入了Web引用請注意就是上面完成的Web Service對外提供服務的URL地址具體可參閱圖所示:

  

  圖:在【TestWebService】添加Web Service提供的服務

   從【工具箱】中的【Windows窗體組件】選項卡中往Form窗體中拖入下列組件並執行相應的操作:

  一個DataGrid組件

  二個Button組件分別是Button至Button並在這二個Button組件拖入Form的設計窗體後分別雙擊它們則系統會在Formvb文件分別產生這二個組件的Click事件對應的處理代碼


   按照表所示調整窗體中各組件屬性的數值

  
組件類型
組件名稱
屬性
設置結果
Form
Form
Text
測試Web Service
Form
MaximizeBox
False
Form
FormBorderStyle
FixedSingle
Button
Button
Text
綁定
Button
FlatStyle
Flat
Button
Text
修改
Button
FlatStyle
Flat  

  表:【TestWebService】項目中組件的主要屬性及其對應數值

  在調整完組件屬性值後再按照圖所示調整組件的位置和排列順序:

  

  圖:【TestWebService】項目中組件排列位置和順序

   把Visual Studio Net的當前窗口切換到Formvb的代碼編輯窗口並用下列代碼替換Formvb中的Button的Click事件對應的處理代碼下列代碼功能是使用Web Service中提供的Binding服務對DataGrid組件實現數據綁定:

  Private Sub Button_Click ( ByVal sender As SystemObject ByVal e As SystemEventArgs ) Handles ButtonClick
 Dim MyService As New localhostService ( )
 DataGridDataSource = MyServiceBinding ( )
 DataGridDataMember = Cust
End Sub

   用下列代碼替換Formvb中的Button的Click事件對應的處理代碼下列代碼功能是使用Web Service中提供的Update服務實現通過DataGrid來修改數據庫數據:

  Private Sub Button_Click ( ByVal sender As SystemObject ByVal e As SystemEventArgs ) Handles ButtonClick
 Dim MyService As New localhostService ( )
 Dim ds As DataSet = DataGridDataSource
 Dim dsChanges As DataSet = dsGetChanges ( )
 If Not ( dsChanges Is Nothing ) Then
  dsMerge ( MyServiceUpdate ( dsChanges ) True )
 End If
End Sub

   至此 【TestWebService】項目的全部工作就完成了調用Web Service是不是很簡單此時單擊快捷鍵F運行程序後單擊程序中的【綁定】按鈕就會對程序中的DataGrid組件實現數據綁定單擊程序中的【修改】按鈕則程序會根據DataGrid中的內容來更新數據庫就是【TestWebService】的運行界面:

  

  圖:【TestWebService】的運行界面

   Formvb的代碼清單如下:

  Public Class Form
Inherits SystemWindowsFormsForm
#Region Windows 窗體設計器生成的代碼
Public Sub New ( )
 MyBaseNew ( )
 該調用是 Windows 窗體設計器所必需的
 InitializeComponent ( )
 在 InitializeComponent ( ) 調用之後添加任何初始化
End Sub
窗體重寫處置以清理組件列表
Protected Overloads Overrides Sub Dispose ( ByVal disposing As Boolean )
 If disposing Then
  If Not ( components Is Nothing ) Then
   componentsDispose ( )
  End If
 End If
 MyBaseDispose ( disposing )
End Sub
Windows 窗體設計器所必需的
Private components As SystemComponentModelIContainer
 注意以下過程是 Windows 窗體設計器所必需的
 可以使用 Windows 窗體設計器修改此過程
 不要使用代碼編輯器修改它
 Friend WithEvents Button As SystemWindowsFormsButton
 Friend WithEvents Button As SystemWindowsFormsButton
 Friend WithEvents DataGrid As SystemWindowsFormsDataGrid
 <SystemDiagnosticsDebuggerStepThrough ( ) > Private Sub InitializeComponent ( )
 MeButton = New SystemWindowsFormsButton ( )
 MeButton = New SystemWindowsFormsButton ( )
 MeDataGrid = New SystemWindowsFormsDataGrid ( )
 CType ( MeDataGrid SystemComponentModelISupportInitialize ) BeginInit ( )
 MeSuspendLayout ( )
 MeButtonFlatStyle = SystemWindowsFormsFlatStyleFlat
 MeButtonLocation = New SystemDrawingPoint ( )
 MeButtonName = Button
 MeButtonSize = New SystemDrawingSize ( )
 MeButtonTabIndex =
 MeButtonText = 綁定
 MeButtonFlatStyle = SystemWindowsFormsFlatStyleFlat
 MeButtonLocation = New SystemDrawingPoint ( )
 MeButtonName = Button
 MeButtonSize = New SystemDrawingSize ( )
 MeButtonTabIndex =
 MeButtonText = 修改
 MeDataGridDataMember =
 MeDataGridDock = SystemWindowsFormsDockStyleTop
 MeDataGridHeaderForeColor = SystemDrawingSystemColorsControlText
 MeDataGridName = DataGrid
 MeDataGridSize = New SystemDrawingSize ( )
 MeDataGridTabIndex =
 MeAutoScaleBaseSize = New SystemDrawingSize ( )
 MeClientSize = New SystemDrawingSize ( )
 MeControlsAddRange ( New SystemWindowsFormsControl ( ) {MeDataGrid MeButton MeButton} )
 MeName = Form
 MeText = 測試Web Service
 CType ( MeDataGrid SystemComponentModelISupportInitialize ) EndInit ( )
 MeResumeLayout ( False )
End Sub
#End Region
Private Sub Button_Click ( ByVal sender As SystemObject ByVal e As SystemEventArgs ) Handles ButtonClick
 Dim MyService As New localhostService ( )
 DataGridDataSource = MyServiceBinding ( )
 DataGridDataMember = Cust
End Sub
Private Sub Button_Click ( ByVal sender As SystemObject ByVal e As SystemEventArgs ) Handles ButtonClick
 Dim MyService As New localhostService ( )
 Dim ds As DataSet = DataGridDataSource
 Dim dsChanges As DataSet = dsGetChanges ( )
 If Not ( dsChanges Is Nothing ) Then
  dsMerge ( MyServiceUpdate ( 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在數據庫方面應用所顯示


From:http://tw.wingwit.com/Article/program/net/201311/11839.html
  • 上一篇文章:

  • 下一篇文章:
  • 推薦文章
    Copyright © 2005-2022 電腦知識網 Computer Knowledge   All rights reserved.