xml web services 是 visual studio 的一個新功能,它提供在松耦合環境中使用標準協議(如 http、xml、xsd、soap 和 wsdl)交換消息的功能。可以結構化和類型化這些消息或對這些消息進行松散定義。因為 web 服務基于標準協議,所以 web 服務應用程序可以與各種不同的實現、平臺和設備通訊。有關更多信息,請參閱托管代碼中的 xml web services。 可以使用 web 服務增強 windows 窗體功能。連接 windows 窗體和 web 服務與調用 web 服務方法一樣簡單,這些方法在服務器上進行處理,然后返回方法調用的結果。 有兩種類型的 web 服務方法:同步和異步。當調用同步 web 服務方法時,調用方等待 web 服務響應后再繼續執行操作。當調用異步 web 服務方法時,可以在等待 web 服務響應的同時繼續使用調用線程。這使得您能夠在客戶端應用程序中有效地使用現有的線程集合。有關使用同步和異步 web 服務方法的更多信息,請參閱使用托管代碼訪問 xml web services。 同步 web 服務方法 調用同步 web 服務方法包括調用該方法;等待在服務器上進行的計算并返回一個值;然后再繼續執行 windows 窗體中的其他代碼。 創建 xml web services
創建 web 服務應用程序。有關更多信息,請參閱創建托管代碼中的 xml web services。
在解決方案資源管理器中,用右鍵單擊 .asmx 文件并選擇“查看代碼”。
創建執行相加的 web 服務方法。以下 web 服務方法將兩個整數相加,然后返回兩者的和:
4. ' visual basic 5. <webmethod()> public function webadd(byval x as integer, byval y as integer) as integer 6. return x + y 7. end function 8. 9. // c# 10. [webmethod] 11. public int webadd(int x, int y) 12. { 13. return x + y; }
創建另一個執行相乘的 web 服務方法。以下 web 服務方法將兩個整數相乘,并返回兩者的積:
15. ' visual basic 16. <webmethod()> public function webmultiply(byval x as integer, byval y as integer) as integer 17. return x * y 18. end function 19. 20. // c# 21. [webmethod] 22. public int webmultiply(int x, int y) 23. { 24. return x * y; }
從“生成”菜單中,選擇“生成解決方案”。也可以瀏覽到在此項目中創建的 .asmx 文件,以便了解 web 服務的更多信息。現在就可以從 windows 窗體調用 web 服務了。
同步調用 xml web services
創建新的 windows 應用程序。有關更多信息,請參閱創建 windows 應用程序項目。
添加對上面創建的 web 服務的引用。詳細信息,請參閱添加和移除 web 引用。
從工具箱中,添加三個 textbox 控件和兩個 button 控件。文本框用于數字,按鈕則用于計算和調用 web 服務方法。
按以下方式設置控件的屬性:
控件 屬性 文本 textbox1 text 0 textbox2 text 0 textbox3 text 0 button1 text 相加 button2 text 相乘
用右鍵單擊該窗體并選擇“查看代碼”。
將 web 服務的實例創建為類成員。需要知道創建上述 web 服務所在的服務器名稱。
7. ' visual basic 8. ' replace localhost below with the name of the server where 9. ' you created the web service. 10. dim mathserviceclass as new localhost.service1() 11. 12. // c# localhost.service1 mathserviceclass = new localhost.service1();
14. ' visual basic 15. private sub button1_click(byval sender as system.object, byval e as system.eventargs) handles button1.click 16. ' create instances of the operands and result. 17. dim x, y, z as integer 18. ' parse the contents of the text boxes into integers. 19. x = integer.parse(textbox1.text) 20. y = integer.parse(textbox2.text) 21. ' call the webadd web service method from the instance of the web service. 22. z = mathserviceclass.webadd(x, y) 23. textbox3.text = z.tostring 24. end sub 25. 26. // c# 27. private void button1_click(object sender, system.eventargs e) 28. { 29. // create instances of the operands and result. 30. int x, y, z; 31. // parse the contents of the text boxes into integers. 32. x = int.parse(textbox1.text); 33. y = int.parse(textbox2.text); 34. // call the webadd web service method from the instance of the web service. 35. z = mathserviceclass.webadd(x, y); 36. textbox3.text = z.tostring(); }
以相同方式為 button2 的 click 事件創建事件處理程序,并添加以下代碼。
38. ' visual basic 39. private sub button2_click(byval sender as system.object, byval e as system.eventargs) handles button2.click 40. ' create instances of the operands and result. 41. dim x, y, z as integer 42. ' parse the contents of the text boxes into integers. 43. x = integer.parse(textbox1.text) 44. y = integer.parse(textbox2.text) 45. ' call the webmultiply web service method from the instance of the web service. 46. z = mathserviceclass.webmultiply(x, y) 47. textbox3.text = z.tostring 48. end sub 49. 50. // c# 51. private void button2_click(object sender, system.eventargs e) 52. { 53. // create instances of the operands and result. 54. int x, y, z; 55. // parse the contents of the text boxes into integers. 56. x = int.parse(textbox1.text); 57. y = int.parse(textbox2.text); 58. // call the webadd web service method from the instance of the web service. 59. z = mathserviceclass.webmultiply(x, y); 60. textbox3.text = z.tostring(); }
注意 因為 web 服務要在服務器上實例化,所以服務器需要花費一段時間來處理第一個 web 服務調用。在應用程序中按這些按鈕時,要切記這一點。下面一節處理這種時間滯后。 異步 web 服務 當調用異步 web 服務方法時,應用程序在等待 web 服務響應的同時繼續運行。這使得您能夠在客戶端應用程序中有效地使用資源。這種在 windows 應用程序中實現 web 服務的方法非常節省資源。 詳細信息,請參閱異步訪問托管代碼中的 xml web services。