麻豆小视频在线观看_中文黄色一级片_久久久成人精品_成片免费观看视频大全_午夜精品久久久久久久99热浪潮_成人一区二区三区四区

首頁 > 開發 > XML > 正文

演練:從 Windows 窗體調用 XML Web services

2024-09-05 20:55:50
字體:
來源:轉載
供稿:網友
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
  1. 創建 web 服務應用程序。有關更多信息,請參閱創建托管代碼中的 xml web services。
  2. 在解決方案資源管理器中,用右鍵單擊 .asmx 文件并選擇“查看代碼”。
  3. 創建執行相加的 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;
}
  1. 創建另一個執行相乘的 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;
}
  1. 從“生成”菜單中,選擇“生成解決方案”。也可以瀏覽到在此項目中創建的 .asmx 文件,以便了解 web 服務的更多信息。現在就可以從 windows 窗體調用 web 服務了。

同步調用 xml web services
  1. 創建新的 windows 應用程序。有關更多信息,請參閱創建 windows 應用程序項目。
  2. 添加對上面創建的 web 服務的引用。詳細信息,請參閱添加和移除 web 引用。
  3. 從工具箱中,添加三個 textbox 控件和兩個 button 控件。文本框用于數字,按鈕則用于計算和調用 web 服務方法。
  4. 按以下方式設置控件的屬性:

控件
屬性
文本
textbox1
text
0
textbox2
text
0
textbox3
text
0
button1
text
相加
button2
text
相乘
  1. 用右鍵單擊該窗體并選擇“查看代碼”。
  2. 將 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();
  1. 為 button1 的 click 事件創建事件處理程序。詳細信息,請參閱在“windows 窗體設計器”上創建事件處理程序。

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();
}
  1. 以相同方式為 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();
}
  1. f5 鍵運行應用程序。在前兩個文本框中輸入值。當按“添加”按鈕時,第三個文本框將顯示兩個值的和。當按“乘”按鈕時,第三個文本框將顯示兩個值的積。

注意 因為 web 服務要在服務器上實例化,所以服務器需要花費一段時間來處理第一個 web 服務調用。在應用程序中按這些按鈕時,要切記這一點。下面一節處理這種時間滯后。
異步 web 服務
當調用異步 web 服務方法時,應用程序在等待 web 服務響應的同時繼續運行。這使得您能夠在客戶端應用程序中有效地使用資源。這種在 windows 應用程序中實現 web 服務的方法非常節省資源。
詳細信息,請參閱異步訪問托管代碼中的 xml web services。
 
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 日本羞羞影院 | omofun 动漫在线观看 | 日韩av电影免费在线观看 | 国产一区二区欧美 | 久久久精品99 | 超级av在线 | 国产99久久精品一区二区 | 日韩欧美高清片 | 91色爱| 羞羞色网站 | 黄 色 免费网 站 成 人 | 精品国产91久久久 | 最近日本电影hd免费观看 | 美国av免费看 | 超碰97国产在线 | 日韩毛片在线看 | 成人福利电影在线观看 | 欧美一级理论 | 一级性生活视频 | 欧美日韩成人一区二区 | 男女生羞羞视频网站在线观看 | 亚洲精品成人久久久 | 久久久aa | 黄色网电影 | 精品一区二区免费视频视频 | 欧洲精品久久久 | 国产免费小视频在线观看 | 91 成人| 欧美成人激情在线 | 又黄又爽免费无遮挡在线观看 | 久草最新| 国产精品视频成人 | 特级毛片a级毛片100免费 | 视频一区国产精品 | 日本在线观看高清完整版 | 日韩视频1 | 国产美女精品视频 | 色播av在线 | 九九视频精品在线观看 | 婷婷一区二区三区 | 国产一级毛片高清视频 |