xml web services 是 visual studio 的一個(gè)新功能,它提供在松耦合環(huán)境中使用標(biāo)準(zhǔn)協(xié)議(如 http、xml、xsd、soap 和 wsdl)交換消息的功能。可以結(jié)構(gòu)化和類型化這些消息或?qū)@些消息進(jìn)行松散定義。因?yàn)?web 服務(wù)基于標(biāo)準(zhǔn)協(xié)議,所以 web 服務(wù)應(yīng)用程序可以與各種不同的實(shí)現(xiàn)、平臺(tái)和設(shè)備通訊。有關(guān)更多信息,請(qǐng)參閱托管代碼中的 xml web services。 可以使用 web 服務(wù)增強(qiáng) windows 窗體功能。連接 windows 窗體和 web 服務(wù)與調(diào)用 web 服務(wù)方法一樣簡(jiǎn)單,這些方法在服務(wù)器上進(jìn)行處理,然后返回方法調(diào)用的結(jié)果。 有兩種類型的 web 服務(wù)方法:同步和異步。當(dāng)調(diào)用同步 web 服務(wù)方法時(shí),調(diào)用方等待 web 服務(wù)響應(yīng)后再繼續(xù)執(zhí)行操作。當(dāng)調(diào)用異步 web 服務(wù)方法時(shí),可以在等待 web 服務(wù)響應(yīng)的同時(shí)繼續(xù)使用調(diào)用線程。這使得您能夠在客戶端應(yīng)用程序中有效地使用現(xiàn)有的線程集合。有關(guān)使用同步和異步 web 服務(wù)方法的更多信息,請(qǐng)參閱使用托管代碼訪問 xml web services。 同步 web 服務(wù)方法 調(diào)用同步 web 服務(wù)方法包括調(diào)用該方法;等待在服務(wù)器上進(jìn)行的計(jì)算并返回一個(gè)值;然后再繼續(xù)執(zhí)行 windows 窗體中的其他代碼。 創(chuàng)建 xml web services
創(chuàng)建 web 服務(wù)應(yīng)用程序。有關(guān)更多信息,請(qǐng)參閱創(chuàng)建托管代碼中的 xml web services。
在解決方案資源管理器中,用右鍵單擊 .asmx 文件并選擇“查看代碼”。
創(chuàng)建執(zhí)行相加的 web 服務(wù)方法。以下 web 服務(wù)方法將兩個(gè)整數(shù)相加,然后返回兩者的和:
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; }
創(chuàng)建另一個(gè)執(zhí)行相乘的 web 服務(wù)方法。以下 web 服務(wù)方法將兩個(gè)整數(shù)相乘,并返回兩者的積:
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; }
從“生成”菜單中,選擇“生成解決方案”。也可以瀏覽到在此項(xiàng)目中創(chuàng)建的 .asmx 文件,以便了解 web 服務(wù)的更多信息。現(xiàn)在就可以從 windows 窗體調(diào)用 web 服務(wù)了。
同步調(diào)用 xml web services
創(chuàng)建新的 windows 應(yīng)用程序。有關(guān)更多信息,請(qǐng)參閱創(chuàng)建 windows 應(yīng)用程序項(xiàng)目。
添加對(duì)上面創(chuàng)建的 web 服務(wù)的引用。詳細(xì)信息,請(qǐng)參閱添加和移除 web 引用。
從工具箱中,添加三個(gè) textbox 控件和兩個(gè) button 控件。文本框用于數(shù)字,按鈕則用于計(jì)算和調(diào)用 web 服務(wù)方法。
按以下方式設(shè)置控件的屬性:
控件 屬性 文本 textbox1 text 0 textbox2 text 0 textbox3 text 0 button1 text 相加 button2 text 相乘
用右鍵單擊該窗體并選擇“查看代碼”。
將 web 服務(wù)的實(shí)例創(chuàng)建為類成員。需要知道創(chuàng)建上述 web 服務(wù)所在的服務(wù)器名稱。
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(); }
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(); }
注意 因?yàn)?web 服務(wù)要在服務(wù)器上實(shí)例化,所以服務(wù)器需要花費(fèi)一段時(shí)間來處理第一個(gè) web 服務(wù)調(diào)用。在應(yīng)用程序中按這些按鈕時(shí),要切記這一點(diǎn)。下面一節(jié)處理這種時(shí)間滯后。 異步 web 服務(wù) 當(dāng)調(diào)用異步 web 服務(wù)方法時(shí),應(yīng)用程序在等待 web 服務(wù)響應(yīng)的同時(shí)繼續(xù)運(yùn)行。這使得您能夠在客戶端應(yīng)用程序中有效地使用資源。這種在 windows 應(yīng)用程序中實(shí)現(xiàn) web 服務(wù)的方法非常節(jié)省資源。 詳細(xì)信息,請(qǐng)參閱異步訪問托管代碼中的 xml web services。