建立ASP.NETWeb服务步骤详解

建立ASP.NET Web服务步骤(1):创建Web服务

网站建设哪家好,找创新互联!专注于网页设计、网站建设、微信开发、微信平台小程序开发、集团企业网站建设等服务项目。为回馈新老客户创新互联还提供了横县免费建站欢迎大家使用!

新建-项目-Web-Asp.net服务应用程序,把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 从脚本中调用此 Web 服务,请取消对下行的注释。
  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. }

测试服务:

点击方法,输入abcde,点调用

测试结果:

返回xml,edcba 测试正确。

建立ASP.NET Web服务步骤(2):在Windows Forms 中调用Web服务

新建Windows Forms 工程,注意上面服务不要关,干脆双开VS吧,免得出问题。项目-添加服务引用,地址中输入Web服务的地址,上例:http://localhost:1241/Service1.asmx,如果Web服务已经发布,请填写发布的地址。

找到服务后确定:

在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. }

运行结果:

建立ASP.NET Web服务步骤(3):异步调用服务

由于web服务在网络上使用,所以如果网速不好,或服务器端忙很长时间没有相应的话,那么执行调用的程序将阻塞,以至界面卡死,不能相应。如何在调用服务的时候不阻塞呢?就是采用异步调用服务的方式。服务端不用修改,只修改客户端就可以了。

首先,在解决方案管理器的Service Reference中,右键该引用,点配置服务引用。如下画面:

选中【生成异步操作】,确定。

代码:

 
 
 
 
  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. }

为了让测试更加逼真,可以在服务器端,加入演示,模拟服务器或网络的延时。

服务端代码:

 
 
 
 
  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 从脚本中调用此 Web 服务,请取消对下行的注释。
  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. }

运行结果:在等待服务器返回的时间内,界面没有卡死,证明异步调用成功。

建立ASP.NET Web服务步骤(4):ASP.NET客户程序

上面是在Windows Forms 里完成的Web服务调用,现在用ASP.NET来调用相同的服务。

基本与Windows Forms类似,首先添加Web服务引用,然后添加代码如下:

 
 
 
 
  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. }

运行结果:

建立ASP.NET Web服务步骤(5):类的传递

上面的例子是简单是数据类型,现在试一下传递一个自定义类。

服务器端代码:

 
 
 
 
  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 从脚本中调用此 Web 服务,请取消对下行的注释。
  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;//加上,认识一下需要传递的参数类,以及返回的类。
  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. }

运行结果:

新闻标题:建立ASP.NETWeb服务步骤详解
分享网址:http://www.hantingmc.com/qtweb/news17/527067.html

网站建设、网络推广公司-创新互联,是专注品牌与效果的网站制作,网络营销seo公司;服务项目有等

广告

声明:本网站发布的内容(图片、视频和文字)以用户投稿、用户转载内容为主,如果涉及侵权请尽快告知,我们将会在第一时间删除。文章观点不代表本网站立场,如需处理请联系客服。电话:028-86922220;邮箱:631063699@qq.com。内容未经允许不得转载,或转载时需注明来源: 创新互联