日本综合一区二区|亚洲中文天堂综合|日韩欧美自拍一区|男女精品天堂一区|欧美自拍第6页亚洲成人精品一区|亚洲黄色天堂一区二区成人|超碰91偷拍第一页|日韩av夜夜嗨中文字幕|久久蜜综合视频官网|精美人妻一区二区三区

RELATEED CONSULTING
相關(guān)咨詢
選擇下列產(chǎn)品馬上在線溝通
服務(wù)時間:8:30-17:00
你可能遇到了下面的問題
關(guān)閉右側(cè)工具欄

新聞中心

這里有您想知道的互聯(lián)網(wǎng)營銷解決方案
建立ASP.NETWeb服務(wù)步驟詳解

建立ASP.NET Web服務(wù)步驟(1):創(chuàng)建Web服務(wù)

網(wǎng)站建設(shè)哪家好,找創(chuàng)新互聯(lián)!專注于網(wǎng)頁設(shè)計、網(wǎng)站建設(shè)、微信開發(fā)、微信平臺小程序開發(fā)、集團(tuán)企業(yè)網(wǎng)站建設(shè)等服務(wù)項(xiàng)目。為回饋新老客戶創(chuàng)新互聯(lián)還提供了橫縣免費(fèi)建站歡迎大家使用!

新建-項(xiàng)目-Web-Asp.net服務(wù)應(yīng)用程序,把HelloWorld給刪除,ReverseString方法,如下:

代碼:

 
 
 
 
  1. using System;
  2. using System.Collections;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Linq;
  6. using System.Web;
  7. using System.Web.Services;
  8. using System.Web.Services.Protocols;
  9. using System.Xml.Linq;
  10. namespace WebService2
  11. {
  12.     /// < summary>
  13.     /// Service1 的摘要說明
  14.     /// < /summary>
  15.     [WebService(Namespace = "http://tempuri.org/")]
  16.     [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
  17.     [ToolboxItem(false)]
  18.     // 若要允許使用 ASP.NET AJAX 從腳本中調(diào)用此 Web 服務(wù),請取消對下行的注釋。
  19.     // [System.Web.Script.Services.ScriptService]
  20.     public class Service1 : System.Web.Services.WebService
  21.     {
  22.         [WebMethod]
  23.         public string ReverseString(string message)  //新建這個方法
  24.         {
  25.             char[] arr = message.ToCharArray();
  26.             Array.Reverse(arr);
  27.             message = new string(arr);
  28.             return message;
  29.         }
  30.     }
  31. }

測試服務(wù):

點(diǎn)擊方法,輸入abcde,點(diǎn)調(diào)用

測試結(jié)果:

返回xml,edcba 測試正確。

建立ASP.NET Web服務(wù)步驟(2):在Windows Forms 中調(diào)用Web服務(wù)

新建Windows Forms 工程,注意上面服務(wù)不要關(guān),干脆雙開VS吧,免得出問題。項(xiàng)目-添加服務(wù)引用,地址中輸入Web服務(wù)的地址,上例:http://localhost:1241/Service1.asmx,如果Web服務(wù)已經(jīng)發(fā)布,請?zhí)顚懓l(fā)布的地址。

找到服務(wù)后確定:

在Form上加入兩個TextBox,一個Button,雙擊Button,編寫事件。

代碼:

 
 
 
 
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Drawing;
  6. using System.Linq;
  7. using System.Text;
  8. using System.Windows.Forms;
  9. namespace WindowsFormsApplication9
  10. {
  11.     public partial class Form1 : Form
  12.     {
  13.         public Form1()
  14.         {
  15.             InitializeComponent();
  16.         }
  17.         private void button1_Click(object sender, EventArgs e)
  18.         {
  19.             ServiceReference1.Service1SoapClient ws = new ServiceReference1.Service1SoapClient();
  20.             textBox2.Text = ws.ReverseString(textBox1.Text);
  21.         }
  22.     }
  23. }

運(yùn)行結(jié)果:

建立ASP.NET Web服務(wù)步驟(3):異步調(diào)用服務(wù)

由于web服務(wù)在網(wǎng)絡(luò)上使用,所以如果網(wǎng)速不好,或服務(wù)器端忙很長時間沒有相應(yīng)的話,那么執(zhí)行調(diào)用的程序?qū)⒆枞?,以至界面卡死,不能相?yīng)。如何在調(diào)用服務(wù)的時候不阻塞呢?就是采用異步調(diào)用服務(wù)的方式。服務(wù)端不用修改,只修改客戶端就可以了。

首先,在解決方案管理器的Service Reference中,右鍵該引用,點(diǎn)配置服務(wù)引用。如下畫面:

選中【生成異步操作】,確定。

代碼:

 
 
 
 
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Drawing;
  6. using System.Linq;
  7. using System.Text;
  8. using System.Windows.Forms;
  9. namespace WindowsFormsApplication9
  10. {
  11.     public partial class Form1 : Form
  12.     {
  13.         public Form1()
  14.         {
  15.             InitializeComponent();
  16.         }
  17.         
  18.         private void button1_Click(object sender, EventArgs e)
  19.         {
  20.             //ServiceReference1.Service1SoapClient ws = new ServiceReference1.Service1SoapClient();
  21.             //textBox2.Text = ws.ReverseString(textBox1.Text);
  22.             
  23.             ServiceReference1.Service1SoapClient client = new ServiceReference1.Service1SoapClient();
  24.             //client.ReverseStringCompleted += new EventHandler< ServiceReference1.ReverseStringCompletedEventArgs>(client_ReverseStringCompleted); 
  25.             client.ReverseStringCompleted += client_ReverseStringCompleted;  //簡易寫法
  26.             client.ReverseStringAsync(textBox1.Text);            
  27.         }
  28.         private void client_ReverseStringCompleted(object sender, ServiceReference1.ReverseStringCompletedEventArgs e) 
  29.         { 
  30.             textBox2.Text = e.Result; 
  31.         }        
  32.     }
  33. }

為了讓測試更加逼真,可以在服務(wù)器端,加入演示,模擬服務(wù)器或網(wǎng)絡(luò)的延時。

服務(wù)端代碼:

 
 
 
 
  1. using System;
  2. using System.Collections;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Linq;
  6. using System.Web;
  7. using System.Web.Services;
  8. using System.Web.Services.Protocols;
  9. using System.Xml.Linq;
  10. namespace WebService2
  11. {
  12.     /// < summary>
  13.     /// Service1 的摘要說明
  14.     /// < /summary>
  15.     [WebService(Namespace = "http://tempuri.org/")]
  16.     [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
  17.     [ToolboxItem(false)]
  18.     // 若要允許使用 ASP.NET AJAX 從腳本中調(diào)用此 Web 服務(wù),請取消對下行的注釋。
  19.     // [System.Web.Script.Services.ScriptService]
  20.     public class Service1 : System.Web.Services.WebService
  21.     {
  22.         [WebMethod]
  23.         public string ReverseString(string message)  //新建這個方法
  24.         {
  25.             char[] arr = message.ToCharArray();
  26.             Array.Reverse(arr);
  27.             message = new string(arr);
  28.             System.Threading.Thread.Sleep(5000);//為了證明異步的效果特添加延時
  29.             return message;
  30.         }
  31.     }
  32. }

運(yùn)行結(jié)果:在等待服務(wù)器返回的時間內(nèi),界面沒有卡死,證明異步調(diào)用成功。

建立ASP.NET Web服務(wù)步驟(4):ASP.NET客戶程序

上面是在Windows Forms 里完成的Web服務(wù)調(diào)用,現(xiàn)在用ASP.NET來調(diào)用相同的服務(wù)。

基本與Windows Forms類似,首先添加Web服務(wù)引用,然后添加代碼如下:

 
 
 
 
  1. using System;
  2. using System.Configuration;
  3. using System.Data;
  4. using System.Linq;
  5. using System.Web;
  6. using System.Web.Security;
  7. using System.Web.UI;
  8. using System.Web.UI.HtmlControls;
  9. using System.Web.UI.WebControls;
  10. using System.Web.UI.WebControls.WebParts;
  11. using System.Xml.Linq;
  12. public partial class _Default : System.Web.UI.Page 
  13. {
  14.     protected void Page_Load(object sender, EventArgs e)
  15.     {
  16.     }
  17.     protected void Button1_Click(object sender, EventArgs e)
  18.     {
  19.         ServiceReference1.Service1SoapClient  client = new ServiceReference1.Service1SoapClient();
  20.         TextBox2.Text = client.ReverseString(TextBox1.Text);
  21.     }
  22. }

運(yùn)行結(jié)果:

建立ASP.NET Web服務(wù)步驟(5):類的傳遞

上面的例子是簡單是數(shù)據(jù)類型,現(xiàn)在試一下傳遞一個自定義類。

服務(wù)器端代碼:

 
 
 
 
  1. using System;
  2. using System.Collections;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Linq;
  6. using System.Web;
  7. using System.Web.Services;
  8. using System.Web.Services.Protocols;
  9. using System.Xml.Linq;
  10. namespace WebService2
  11. {
  12.     public enum TemperatureType
  13.     {
  14.         Fahrenheit,
  15.         Celsius
  16.     }
  17.     public enum TemparatureCondition
  18.     {
  19.         Rainy,
  20.         Sunny,
  21.         Cloudy,
  22.         Thunderstorm
  23.     }
  24.     public class GetWeatherRequest
  25.     {
  26.         public string City;
  27.         public TemperatureType TemperatureType;
  28.     }
  29.     public class GetWeatherResponse
  30.     {
  31.         public TemparatureCondition Condition;
  32.         public int Temperature;
  33.     }
  34.     /// < summary>
  35.     /// Service1 的摘要說明
  36.     /// < /summary>
  37.     [WebService(Namespace = "http://tempuri.org/")]
  38.     [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
  39.     [ToolboxItem(false)]
  40.     // 若要允許使用 ASP.NET AJAX 從腳本中調(diào)用此 Web 服務(wù),請取消對下行的注釋。
  41.     // [System.Web.Script.Services.ScriptService]
  42.     public class Service1 : System.Web.Services.WebService
  43.     {
  44.         [WebMethod]
  45.         public string ReverseString(string message)  //新建這個方法
  46.         {
  47.             char[] arr = message.ToCharArray();
  48.             Array.Reverse(arr);
  49.             message = new string(arr);
  50.             System.Threading.Thread.Sleep(5000);//為了證明異步的效果特添加延時
  51.             return message;
  52.         }
  53.         [WebMethod]
  54.         public GetWeatherResponse GetWeather(GetWeatherRequest req)
  55.         {
  56.             GetWeatherResponse resp = new GetWeatherResponse();
  57.             Random r = new Random();
  58.             int celsius = r.Next(-20, 50);
  59.             if (req.TemperatureType == TemperatureType.Celsius)
  60.                 resp.Temperature = celsius;
  61.             else
  62.                 resp.Temperature = (212 - 32) / 100 * celsius + 32;
  63.             if (req.City == "Redmond")
  64.                 resp.Condition = TemparatureCondition.Rainy;
  65.             else
  66.                 resp.Condition = (TemparatureCondition)r.Next(0, 3);
  67.             return resp;
  68.         }
  69.     }
  70. }

客戶端代碼:

 
 
 
 
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Drawing;
  6. using System.Text;
  7. using System.Windows.Forms;
  8. using WindowsFormsApplication10.ServiceReference1;//加上,認(rèn)識一下需要傳遞的參數(shù)類,以及返回的類。
  9. namespace WindowsFormsApplication10
  10. {
  11.     public partial class Form1 : Form
  12.     {
  13.         public Form1()
  14.         {
  15.             InitializeComponent();
  16.         }
  17.         private void label2_Click(object sender, EventArgs e)
  18.         {
  19.         }
  20.         private void button1_Click(object sender, EventArgs e)
  21.         {
  22.             GetWeatherRequest req = new GetWeatherRequest();//有了最上面那個using這下找到這個類了吧,不然肯定找不到。
  23.             if (radioButton1.Checked)
  24.             {
  25.                 req.TemperatureType = TemperatureType.Celsius;
  26.             }
  27.             else
  28.             {
  29.                 req.TemperatureType = TemperatureType.Fahrenheit;
  30.             }
  31.             req.City = textBox1.Text;
  32.             Service1SoapClient client = new Service1SoapClient();
  33.             GetWeatherResponse resp = client.GetWeather(req);
  34.             textBox2.Text = resp.Condition.ToString();
  35.             textBox3.Text = resp.Temperature.ToString();
  36.             
  37.         }
  38.     }
  39. }

運(yùn)行結(jié)果:


網(wǎng)站欄目:建立ASP.NETWeb服務(wù)步驟詳解
本文來源:http://www.dlmjj.cn/article/cdgcpdd.html