国产一级a片免费看高清,亚洲熟女中文字幕在线视频,黄三级高清在线播放,免费黄色视频在线看

打開(kāi)APP
userphoto
未登錄

開(kāi)通VIP,暢享免費(fèi)電子書(shū)等14項(xiàng)超值服

開(kāi)通VIP
建立ASP.NET Web服務(wù)步驟詳解 - 51CTO.COM
    本文從創(chuàng)建Web服務(wù)、在Windows Forms 中調(diào)用Web服務(wù)、異步調(diào)用服務(wù)等方面介紹了建立ASP.NET Web 服務(wù)的步驟。

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

    新建-項(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.  
    11. namespace WebService2  
    12. {  
    13.     /// < summary>  
    14.     /// Service1 的摘要說(shuō)明  
    15.     /// < /summary>  
    16.     [WebService(Namespace = "http://tempuri.org/")]  
    17.     [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]  
    18.     [ToolboxItem(false)]  
    19.     // 若要允許使用 ASP.NET AJAX 從腳本中調(diào)用此 Web 服務(wù),請(qǐng)取消對(duì)下行的注釋。  
    20.     // [System.Web.Script.Services.ScriptService]  
    21.     public class Service1 : System.Web.Services.WebService  
    22.     {  
    23.  
    24.         [WebMethod]  
    25.         public string ReverseString(string message)  //新建這個(gè)方法  
    26.         {  
    27.             char[] arr = message.ToCharArray();  
    28.             Array.Reverse(arr);  
    29.             message = new string(arr);  
    30.             return message;  
    31.         }  
    32.     }  

    測(cè)試服務(wù):

     

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

     

    測(cè)試結(jié)果:

     

    返回xml,edcba 測(cè)試正確。

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

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

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

     

    在Form上加入兩個(gè)TextBox,一個(gè)Button,雙擊Button,編寫(xiě)事件。

    代碼:

            
    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.  
    10. namespace WindowsFormsApplication9  
    11. {  
    12.     public partial class Form1 : Form  
    13.     {  
    14.         public Form1()  
    15.         {  
    16.             InitializeComponent();  
    17.         }  
    18.  
    19.         private void button1_Click(object sender, EventArgs e)  
    20.         {  
    21.             ServiceReference1.Service1SoapClient ws = new ServiceReference1.Service1SoapClient();  
    22.             textBox2.Text = ws.ReverseString(textBox1.Text);  
    23.         }  
    24.     }  

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

     

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

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

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

     

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

    代碼:

            
    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.  
    10. namespace WindowsFormsApplication9  
    11. {  
    12.     public partial class Form1 : Form  
    13.     {  
    14.         public Form1()  
    15.         {  
    16.             InitializeComponent();  
    17.         }  
    18.  
    19.           
    20.         private void button1_Click(object sender, EventArgs e)  
    21.         {  
    22.             //ServiceReference1.Service1SoapClient ws = new ServiceReference1.Service1SoapClient();  
    23.             //textBox2.Text = ws.ReverseString(textBox1.Text);  
    24.               
    25.             ServiceReference1.Service1SoapClient client = new ServiceReference1.Service1SoapClient();  
    26.             //client.ReverseStringCompleted += new EventHandler< ServiceReference1.ReverseStringCompletedEventArgs>(client_ReverseStringCompleted);   
    27.             client.ReverseStringCompleted += client_ReverseStringCompleted;  //簡(jiǎn)易寫(xiě)法  
    28.             client.ReverseStringAsync(textBox1.Text);              
    29.         }  
    30.         private void client_ReverseStringCompleted(object sender, ServiceReference1.ReverseStringCompletedEventArgs e)   
    31.         {   
    32.             textBox2.Text = e.Result;   
    33.         }          
    34.     }  

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

    服務(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.  
    11. namespace WebService2  
    12. {  
    13.     /// < summary>  
    14.     /// Service1 的摘要說(shuō)明  
    15.     /// < /summary>  
    16.     [WebService(Namespace = "http://tempuri.org/")]  
    17.     [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]  
    18.     [ToolboxItem(false)]  
    19.     // 若要允許使用 ASP.NET AJAX 從腳本中調(diào)用此 Web 服務(wù),請(qǐng)取消對(duì)下行的注釋。  
    20.     // [System.Web.Script.Services.ScriptService]  
    21.     public class Service1 : System.Web.Services.WebService  
    22.     {  
    23.  
    24.         [WebMethod]  
    25.         public string ReverseString(string message)  //新建這個(gè)方法  
    26.         {  
    27.             char[] arr = message.ToCharArray();  
    28.             Array.Reverse(arr);  
    29.             message = new string(arr);  
    30.  
    31.             System.Threading.Thread.Sleep(5000);//為了證明異步的效果特添加延時(shí)  
    32.  
    33.             return message;  
    34.         }  
    35.     }  

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

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

    上面是在Windows Forms 里完成的Web服務(wù)調(diào)用,現(xiàn)在用ASP.NET來(lái)調(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.  
    13. public partial class _Default : System.Web.UI.Page   
    14. {  
    15.     protected void Page_Load(object sender, EventArgs e)  
    16.     {  
    17.  
    18.     }  
    19.     protected void Button1_Click(object sender, EventArgs e)  
    20.     {  
    21.         ServiceReference1.Service1SoapClient  client = new ServiceReference1.Service1SoapClient();  
    22.         TextBox2.Text = client.ReverseString(TextBox1.Text);  
    23.     }  

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

     

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

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

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

    客戶端代碼:

            
    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í)一下需要傳遞的參數(shù)類,以及返回的類。  
    9.  
    10. namespace WindowsFormsApplication10  
    11. {  
    12.     public partial class Form1 : Form  
    13.     {  
    14.         public Form1()  
    15.         {  
    16.             InitializeComponent();  
    17.         }  
    18.  
    19.         private void label2_Click(object sender, EventArgs e)  
    20.         {  
    21.  
    22.         }  
    23.  
    24.         private void button1_Click(object sender, EventArgs e)  
    25.         {  
    26.             GetWeatherRequest req = new GetWeatherRequest();//有了最上面那個(gè)using這下找到這個(gè)類了吧,不然肯定找不到。  
    27.  
    28.             if (radioButton1.Checked)  
    29.             {  
    30.                 req.TemperatureType = TemperatureType.Celsius;  
    31.             }  
    32.             else 
    33.             {  
    34.                 req.TemperatureType = TemperatureType.Fahrenheit;  
    35.             }  
    36.  
    37.             req.City = textBox1.Text;  
    38.  
    39.             Service1SoapClient client = new Service1SoapClient();  
    40.             GetWeatherResponse resp = client.GetWeather(req);  
    41.  
    42.             textBox2.Text = resp.Condition.ToString();  
    43.             textBox3.Text = resp.Temperature.ToString();  
    44.               
    45.  
    46.         }  
    47.     }  

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

     

本站僅提供存儲(chǔ)服務(wù),所有內(nèi)容均由用戶發(fā)布,如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請(qǐng)點(diǎn)擊舉報(bào)。
打開(kāi)APP,閱讀全文并永久保存 查看更多類似文章
猜你喜歡
類似文章
使用StructureMap擴(kuò)展ASP.NET MVC三層架構(gòu)5
using 用法
C#動(dòng)態(tài)調(diào)用Web服務(wù)的3種方法 - 51CTO.COM
ASP.NET MVC 緩存使用示例
C#編程命名規(guī)范推薦
Entity Framework4.0 (三)概述(EF4 的Code First方法)
更多類似文章 >>
生活服務(wù)
分享 收藏 導(dǎo)長(zhǎng)圖 關(guān)注 下載文章
綁定賬號(hào)成功
后續(xù)可登錄賬號(hào)暢享VIP特權(quán)!
如果VIP功能使用有故障,
可點(diǎn)擊這里聯(lián)系客服!

聯(lián)系客服