【IT168 技術(shù)文檔】
XML Web services 是 Visual Studio 的一項功能,使您能夠開發(fā)使用標(biāo)準(zhǔn)協(xié)議(如 HTTP、XML、XSD、SOAP 和 WSDL)在松耦合環(huán)境中交換消息的應(yīng)用程序。消息可以結(jié)構(gòu)化和類型化或者松散定義。因為 Web 服務(wù)功能基于標(biāo)準(zhǔn)協(xié)議,所以 Web 服務(wù)應(yīng)用程序可以與各種各樣不同的實現(xiàn)、平臺和設(shè)備通信。有關(guān)更多信息,請參見在托管代碼中使用的 XML Web services。
可以使用 Web 服務(wù)增強 Windows 窗體的功能。連接 Windows 窗體和 Web 服務(wù)與調(diào)用 Web 服務(wù)方法一樣簡單,這些方法在服務(wù)器上進行處理,然后返回方法調(diào)用的結(jié)果。
有兩種類型的 Web 服務(wù)方法:同步和異步。當(dāng)調(diào)用同步 Web 服務(wù)方法時,調(diào)用方等待 Web 服務(wù)響應(yīng)后再繼續(xù)執(zhí)行操作。當(dāng)調(diào)用異步 Web 服務(wù)方法時,可以在等待 Web 服務(wù)響應(yīng)的同時繼續(xù)使用調(diào)用線程。這使得您能夠在客戶端應(yīng)用程序中有效地使用現(xiàn)有的線程集合。有關(guān)使用同步和異步 Web 服務(wù)方法的更多信息,請參見使用托管代碼訪問 XML Web services。
同步 Web 服務(wù)方法
調(diào)用同步 Web 服務(wù)方法包括調(diào)用該方法;等待在服務(wù)器上進行的計算并返回一個值;然后再繼續(xù)執(zhí)行 Windows 窗體中的其他代碼。
創(chuàng)建 XML Web services
-
創(chuàng)建 Web 服務(wù)應(yīng)用程序。有關(guān)更多信息,請參見使用托管代碼創(chuàng)建 XML Web services。
-
在“解決方案資源管理器”中,右鍵單擊 .asmx 文件并選擇“查看代碼”。
-
創(chuàng)建執(zhí)行相加的 Web 服務(wù)方法。下面的代碼示例顯示的 Web 服務(wù)方法以兩個整數(shù)作為輸入并將其相加,然后返回和。
<WebMethod()> Public Function WebAdd(ByVal x As Integer, ByVal y As Integer) As Integer
Return x + y
End Function
[WebMethod]
public int WebAdd(int x, int y)
{
return x + y;
}
/** @attribute WebMethod() */
public int WebAdd(int x, int y)
{
return x + y;
}
-
創(chuàng)建另一個執(zhí)行相乘的 Web 服務(wù)方法。下面的代碼示例顯示的 Web 服務(wù)方法以兩個整數(shù)作為輸入并將其相乘,然后返回積。
<WebMethod()> Public Function WebMultiply(ByVal x As Integer, ByVal y As Integer) As Integer
Return x * y
End Function
[WebMethod]
public int WebMultiply(int x, int y)
{
return x * y;
}
/** @attribute WebMethod() */
public int WebMultiply(int x, int y)
{
return x * y;
}
-
從“生成”菜單中選擇“生成解決方案”。也可以瀏覽到在此項目中創(chuàng)建的 .asmx 文件,以便了解 Web 服務(wù)的更多信息。現(xiàn)在就可以從 Windows 窗體調(diào)用 Web 服務(wù)了。
同步調(diào)用 XML Web services
-
創(chuàng)建新的基于 Windows 的應(yīng)用程序。有關(guān)更多信息,請參見如何:創(chuàng)建 Windows 應(yīng)用程序項目。
-
在“解決方案資源管理器”中,右鍵單擊項目的“引用”節(jié)點,然后單擊“添加 Web 引用”。
“添加 Web 引用”對話框打開。
-
在“地址”框中鍵入下面的 Web 服務(wù) URI,然后按 Enter 鍵:
http://localhost/WebService1/Service1.asmx
這是您在上一過程中創(chuàng)建的 Web 服務(wù)的 URI。在“添加 Web 引用”對話框的左窗格中出現(xiàn) WebAdd 和 WebMultiply Web 方法。
-
單擊“添加引用”。
-
從“工具箱”中,添加三個 TextBox 控件和兩個 Button 控件。文本框用于數(shù)字,按鈕則用于計算和調(diào)用 Web 服務(wù)方法。
-
采用下面的方式設(shè)置控件的屬性:
| 控件 |
屬性 |
文本 |
|
TextBox1 |
文本 |
0 |
|
TextBox2 |
文本 |
0 |
|
TextBox3 |
文本 |
0 |
|
Button1 |
文本 |
相加 |
|
Button2 |
文本 |
相乘 |
-
右鍵單擊窗體并選擇“查看代碼”。
-
將 Web 服務(wù)的實例創(chuàng)建為類成員。需要知道創(chuàng)建上述 Web 服務(wù)所在的服務(wù)器的名稱。
' Replace localhost below with the name of the server where
' you created the Web service.
Dim MathServiceClass As New localhost.Service1()
localhost.Service1 MathServiceClass = new localhost.Service1();
localhost.Service1 MathServiceClass = new localhost.Service1();
-
為 Button1 的 Click 事件創(chuàng)建一個事件處理程序。有關(guān)詳細信息,請參見如何:使用設(shè)計器創(chuàng)建事件處理程序。
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
' Create instances of the operands and result.
Dim x, y, z As Integer
' Parse the contents of the text boxes into integers.
x = Integer.Parse(TextBox1.Text)
y = Integer.Parse(TextBox2.Text)
' Call the WebAdd Web service method from the instance of the Web service.
z = MathServiceClass.WebAdd(x, y)
TextBox3.Text = z.ToString
End Sub
private void button1_Click(object sender, System.EventArgs e)
{
// Create instances of the operands and result.
int x, y, z;
// Parse the contents of the text boxes into integers.
x = int.Parse(textBox1.Text);
y = int.Parse(textBox2.Text);
// Call the WebAdd Web service method from the instance of the Web service.
z = MathServiceClass.WebAdd(x, y);
textBox3.Text = z.ToString();
}
(Visual C#) 在窗體的構(gòu)造函數(shù)中放置以下代碼以注冊事件處理程序。
this.button1.Click += new System.EventHandler(this.button1_Click);
private void button1_Click (Object sender, System.EventArgs e)
{
// Create instances of the operands and result.
int x, y, z;
// Parse the contents of the text boxes into integers.
x = Integer.parseInt(textBox1.get_Text());
y = Integer.parseInt(textBox2.get_Text());
// Call the WebAdd Web service method from the instance of the Web service.
z = MathServiceClass.WebAdd(x, y);
textBox3.set_Text(""+z);
}
-
以相同的方式為 Button2 的 Click 事件創(chuàng)建事件處理程序,并添加以下代碼。
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
' Create instances of the operands and result.
Dim x, y, z As Integer
' Parse the contents of the text boxes into integers.
x = Integer.Parse(TextBox1.Text)
y = Integer.Parse(TextBox2.Text)
' Call the WebMultiply Web service method from the instance of the Web service.
z = MathServiceClass.WebMultiply(x, y)
TextBox3.Text = z.ToString
End Sub
private void button2_Click(object sender, System.EventArgs e)
{
// Create instances of the operands and result.
int x, y, z;
// Parse the contents of the text boxes into integers.
x = int.Parse(textBox1.Text);
y = int.Parse(textBox2.Text);
// Call the WebAdd Web service method from the instance of the Web service.
z = MathServiceClass.WebMultiply(x, y);
textBox3.Text = z.ToString();
}
(Visual C#) 在窗體的構(gòu)造函數(shù)中放置以下代碼以注冊事件處理程序。
this.button2.Click += new System.EventHandler(this.button2_Click);
private void button2_Click (Object sender, System.EventArgs e)
{
// Create instances of the operands and result.
int x, y, z;
// Parse the contents of the text boxes into integers.
x = Integer.parseInt(textBox1.get_Text());
y = Integer.parseInt(textBox2.get_Text());
// Call the WebAdd Web service method from the instance of the Web service.
z = MathServiceClass.WebMultiply(x, y);
textBox3.set_Text(""+z);
}
-
按 F5 鍵運行應(yīng)用程序。
-
在前兩個文本框中輸入值。當(dāng)按“相加”按鈕時,第三個文本框?qū)@示兩個值的和。當(dāng)按“相乘”按鈕時,第三個文本框?qū)@示兩個值的積。
注意 |
|
因為 Web 服務(wù)要在服務(wù)器上實例化,所以服務(wù)器需要花費一段時間來處理第一個 Web 服務(wù)調(diào)用。在應(yīng)用程序中按這些按鈕時,要切記這一點。下面一節(jié)處理這種時間滯后。 |
|