一:用ASP.NET調用Web Service
打開VS2010,打開“文件-新建-網站”,選擇“ASP.NET網站”
選好存儲位置,語言后點擊確定,進入默認頁面。然后先添加Web引用,把WebService引到當前的工程里面。方法是:在資源管理器中點擊右鍵,選擇添加Web 引用,(該webservice為上一篇的例子)調出對話框:
在URL中填入,前面寫好的WebService運行后瀏覽器上面顯示的地址,點擊“前往”按鈕,如上圖,就會顯示出所引用的WebService中可以調用的方法,然后點擊“添加引用”,就將webservice引用到了當前的工程里面 ,如下圖,解決方案中會出現引進來的WebService文件
如果添加的是ServiceReference的服務引用,實例化服務是應該這樣,如下:
hong.Service1SoapClient login = new hong.Service1SoapClient(); if (txtName.Text.Trim().Length == 0) { MessageBox.Show("請輸入用戶名!"); return; } else if (pwdInfo.Password.Trim().Length == 0) { MessageBox.Show("請輸入密碼!"); return; }
我們在這就練習調用webservice的四個方法,做一個簡單的調用的例子,先在網站的前台添加幾個控件,代碼如下:
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="WebApplication2._Default" %> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" > <head id="Head1" runat="server"> <title>Webservice調用實例</title> </head> <body> <form id="form2" runat="server"> <div> <asp:TextBox ID="Num1" runat="server"></asp:TextBox> <select id="selectOper" runat = "server"> <option>+</option> <option>-</option> <option>*</option> <option>/</option> </select> <asp:TextBox ID="Num2" runat="server"></asp:TextBox> <span id = E runat = "server"></span> <asp:TextBox ID="Result" runat="server"></asp:TextBox> </div> </form> </body> </html>
然后在后台寫調用的代碼,調用之前和使用其它的對象一樣,要先實例化,實例化的方法是localhost.Service a =new localhost.Service();然后就可以通過a來訪問WebService里面提供的方法了。在這個例子里面,動態的創建了一個button控件來觸發WebService的調用,后台代碼如下:
using System; using System.Collections.Generic; using System.Web; using System.Web.UI; using System.Web.UI.WebControls; namespace WebApplication2 { public partial class _Default : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { //在頁面加載的時候動態創建一個按鈕,在它的事件里調用Webservice Button btn = new Button(); btn.Width = 20; btn.Text = " = "; btn.Click += new EventHandler(btn_Click); E.Controls.Add(btn); } /// <summary> /// 定義動態創建Button的Click事件,在這個事件中調用Webservice /// </summary> /// <param name="sender"></param> /// <param name="e"></param> void btn_Click(object sender, EventArgs e) { if (Num1.Text != "" && Num2.Text != "") { //實例化引用的webservice對象 localhost.Service1 WebserviceInstance = new localhost.Service1(); int Oper = selectOper.SelectedIndex; switch (Oper) { //通過實例化的webservice對象來調用Webservice暴露的方法 case 0: Result.Text = WebserviceInstance.addition(double.Parse(Num1.Text), double.Parse(Num2.Text)).ToString(); break; case 1: Result.Text = WebserviceInstance.subtract(double.Parse(Num1.Text), double.Parse(Num2.Text)).ToString(); break; case 2: Result.Text = WebserviceInstance.multiplication(double.Parse(Num1.Text), double.Parse(Num2.Text)).ToString(); break; case 3: Result.Text = WebserviceInstance.division(double.Parse(Num1.Text), double.Parse(Num2.Text)).ToString(); break; } } } } }
運行后可以看到效果,如下圖所示,在前面兩個Textbox里面輸入兩個操作數,在中間的下拉列表中選擇操作符,然后點擊“=”號,將計算的結果輸出到第三個Textbox里面。
其中的報錯解決
整個計算並不是在本地進行的,是在Web服務端進行計算的然后將結果通過XML返還給了調用方的,所以,在運行該程序的時候,WebService程序還必須啟動,否則會報無法連接遠程服務器的異常
二:winform程序調用Web Service
運行效果圖:
新建一個winform項目,添加一個添加Web引用,和ASP.Net添加引用相同,使用的同樣是上一篇的webservic
e
界面設計代碼:
namespace winform { partial class Form1 { /// <summary> /// 必需的設計器變量。 /// </summary> private System.ComponentModel.IContainer components = null; /// <summary> /// 清理所有正在使用的資源。 /// </summary> /// <param name="disposing">如果應釋放托管資源,為 true;否則為 false。</param> protected override void Dispose(bool disposing) { if (disposing && (components != null)) { components.Dispose(); } base.Dispose(disposing); } #region Windows 窗體設計器生成的代碼 /// <summary> /// 設計器支持所需的方法 - 不要 /// 使用代碼編輯器修改此方法的內容。 /// </summary> private void InitializeComponent() { this.btnCompute = new System.Windows.Forms.Button(); this.tbNumX = new System.Windows.Forms.TextBox(); this.tbNumY = new System.Windows.Forms.TextBox(); this.tbResult = new System.Windows.Forms.TextBox(); this.label2 = new System.Windows.Forms.Label(); this.cmbWays = new System.Windows.Forms.ComboBox(); this.SuspendLayout(); // // btnCompute // this.btnCompute.Location = new System.Drawing.Point(212, 90); this.btnCompute.Name = "btnCompute"; this.btnCompute.Size = new System.Drawing.Size(75, 23); this.btnCompute.TabIndex = 0; this.btnCompute.Text = "計算"; this.btnCompute.UseVisualStyleBackColor = true; this.btnCompute.Click += new System.EventHandler(this.btnCompute_Click); // // tbNumX // this.tbNumX.Location = new System.Drawing.Point(41, 40); this.tbNumX.Name = "tbNumX"; this.tbNumX.Size = new System.Drawing.Size(100, 21); this.tbNumX.TabIndex = 1; // // tbNumY // this.tbNumY.Location = new System.Drawing.Point(222, 39); this.tbNumY.Name = "tbNumY"; this.tbNumY.Size = new System.Drawing.Size(100, 21); this.tbNumY.TabIndex = 2; // // tbResult // this.tbResult.Location = new System.Drawing.Point(349, 39); this.tbResult.Name = "tbResult"; this.tbResult.Size = new System.Drawing.Size(100, 21); this.tbResult.TabIndex = 3; // // label2 // this.label2.AutoSize = true; this.label2.Location = new System.Drawing.Point(330, 43); this.label2.Name = "label2"; this.label2.Size = new System.Drawing.Size(11, 12); this.label2.TabIndex = 5; this.label2.Text = "="; // // cmbWays // this.cmbWays.FormattingEnabled = true; this.cmbWays.Location = new System.Drawing.Point(149, 40); this.cmbWays.Name = "cmbWays"; this.cmbWays.Size = new System.Drawing.Size(65, 20); this.cmbWays.TabIndex = 6; // // Form1 // this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F); this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; this.ClientSize = new System.Drawing.Size(471, 131); this.Controls.Add(this.cmbWays); this.Controls.Add(this.label2); this.Controls.Add(this.tbResult); this.Controls.Add(this.tbNumY); this.Controls.Add(this.tbNumX); this.Controls.Add(this.btnCompute); this.Name = "Form1"; this.Text = "Form1"; this.Load += new System.EventHandler(this.Form1_Load); this.ResumeLayout(false); this.PerformLayout(); } #endregion private System.Windows.Forms.Button btnCompute; private System.Windows.Forms.TextBox tbNumX; private System.Windows.Forms.TextBox tbNumY; private System.Windows.Forms.TextBox tbResult; private System.Windows.Forms.Label label2; private System.Windows.Forms.ComboBox cmbWays; } }
后台代碼:
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Text; using System.Windows.Forms; namespace winform { public partial class Form1 : Form { //Sign s = new Sign(); public Form1() { InitializeComponent(); } private void btnCompute_Click(object sender, EventArgs e) { if (tbNumX.Text != "" && tbNumY.Text != "") { //實例化引用的webservice對象 Compute.Service1 WebserviceInstance = new Compute.Service1 (); int a =Convert.ToInt32( cmbWays.SelectedItem.ToString().Trim().Substring(0,1)); switch (a) { //通過實例化的webservice對象來調用Webservice暴露的方法 case 0: tbResult.Text = WebserviceInstance.addition(double.Parse(tbNumX.Text), double.Parse(tbNumY.Text)).ToString(); break; case 1: tbResult.Text = WebserviceInstance.subtract(double.Parse(tbNumX.Text), double.Parse(tbNumY.Text)).ToString(); break; case 2: tbResult.Text = WebserviceInstance.multiplication(double.Parse(tbNumX.Text), double.Parse(tbNumY.Text)).ToString(); break; case 3: tbResult.Text = WebserviceInstance.division(double.Parse(tbNumX.Text), double.Parse(tbNumY.Text)).ToString(); break; } } } //載入加減乘除的符號 private void Form1_Load(object sender, EventArgs e) { Sign add = new Sign { Name = "+", Num = 0 }; Sign sub = new Sign { Name = "-", Num = 1 }; Sign mul = new Sign { Name = "*", Num = 2 }; Sign div = new Sign { Name = "/", Num = 3 }; //cmbWays.Items.Add(add); cmbWays.Items.Add(add.Num+"---> "+ add.Name); cmbWays.Items.Add(sub.Num + "---> " + sub.Name); cmbWays.Items.Add(mul.Num + "---> " + mul.Name); cmbWays.Items.Add(div.Num + "---> " + div.Name); cmbWays.SelectedItem = add.Num + "---> " + add.Name;//去掉了空格,默認選擇項 } } }
自定義類:
using System; using System.Collections.Generic; using System.Text; namespace winform { public class Sign { public int Num { get; set; } public string Name { get; set; } } }