电竞比分网-中国电竞赛事及体育赛事平台

分享

從 Windows 窗體調(diào)用 XML Web services

 落影殘沙 2012-04-12
【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。

調(diào)用同步 Web 服務(wù)方法包括調(diào)用該方法;等待在服務(wù)器上進行的計算并返回一個值;然后再繼續(xù)執(zhí)行 Windows 窗體中的其他代碼。

創(chuàng)建 XML Web services

  1. 創(chuàng)建 Web 服務(wù)應(yīng)用程序。有關(guān)更多信息,請參見使用托管代碼創(chuàng)建 XML Web services。

  2. 在“解決方案資源管理器”中,右鍵單擊 .asmx 文件并選擇“查看代碼”。

  3. 創(chuàng)建執(zhí)行相加的 Web 服務(wù)方法。下面的代碼示例顯示的 Web 服務(wù)方法以兩個整數(shù)作為輸入并將其相加,然后返回和。

    Visual Basic
    <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;
        }
        
  4. 創(chuàng)建另一個執(zhí)行相乘的 Web 服務(wù)方法。下面的代碼示例顯示的 Web 服務(wù)方法以兩個整數(shù)作為輸入并將其相乘,然后返回積。

    Visual Basic
    <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;
        }
        
  5. 從“生成”菜單中選擇“生成解決方案”。也可以瀏覽到在此項目中創(chuàng)建的 .asmx 文件,以便了解 Web 服務(wù)的更多信息。現(xiàn)在就可以從 Windows 窗體調(diào)用 Web 服務(wù)了。

同步調(diào)用 XML Web services

  1. 創(chuàng)建新的基于 Windows 的應(yīng)用程序。有關(guān)更多信息,請參見如何:創(chuàng)建 Windows 應(yīng)用程序項目。

    Security note安全注意

    調(diào)用 Web 方法需要 WebPermission 類授予的特權(quán)級別。如果在部分信任的上下文中運行,則該進程可能會引發(fā)異常。有關(guān)更多信息,請參見代碼訪問安全性基礎(chǔ)知識。

  2. 在“解決方案資源管理器”中,右鍵單擊項目的“引用”節(jié)點,然后單擊“添加 Web 引用”。

    “添加 Web 引用”對話框打開。

  3. 在“地址”框中鍵入下面的 Web 服務(wù) URI,然后按 Enter 鍵:

    http://localhost/WebService1/Service1.asmx

    這是您在上一過程中創(chuàng)建的 Web 服務(wù)的 URI。在“添加 Web 引用”對話框的左窗格中出現(xiàn) WebAdd 和 WebMultiply Web 方法。

  4. 單擊“添加引用”。

  5. 從“工具箱”中,添加三個 TextBox 控件和兩個 Button 控件。文本框用于數(shù)字,按鈕則用于計算和調(diào)用 Web 服務(wù)方法。

  6. 采用下面的方式設(shè)置控件的屬性:

    控件 屬性 文本

    TextBox1

    文本

    0

    TextBox2

    文本

    0

    TextBox3

    文本

    0

    Button1

    文本

    相加

    Button2

    文本

    相乘

  7. 右鍵單擊窗體并選擇“查看代碼”。

  8. 將 Web 服務(wù)的實例創(chuàng)建為類成員。需要知道創(chuàng)建上述 Web 服務(wù)所在的服務(wù)器的名稱。

    Visual Basic
    ' 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();
        
  9. 為 Button1 的 Click 事件創(chuàng)建一個事件處理程序。有關(guān)詳細信息,請參見如何:使用設(shè)計器創(chuàng)建事件處理程序。

    Visual Basic
    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);
        }
        
  10. 以相同的方式為 Button2 的 Click 事件創(chuàng)建事件處理程序,并添加以下代碼。

    Visual Basic
    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);
        }
        
  11. 按 F5 鍵運行應(yīng)用程序。

  12. 在前兩個文本框中輸入值。當(dāng)按“相加”按鈕時,第三個文本框?qū)@示兩個值的和。當(dāng)按“相乘”按鈕時,第三個文本框?qū)@示兩個值的積。

    Note注意

    因為 Web 服務(wù)要在服務(wù)器上實例化,所以服務(wù)器需要花費一段時間來處理第一個 Web 服務(wù)調(diào)用。在應(yīng)用程序中按這些按鈕時,要切記這一點。下面一節(jié)處理這種時間滯后。

    本站是提供個人知識管理的網(wǎng)絡(luò)存儲空間,所有內(nèi)容均由用戶發(fā)布,不代表本站觀點。請注意甄別內(nèi)容中的聯(lián)系方式、誘導(dǎo)購買等信息,謹(jǐn)防詐騙。如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請點擊一鍵舉報。
    轉(zhuǎn)藏 分享 獻花(0

    0條評論

    發(fā)表

    請遵守用戶 評論公約

    類似文章 更多