.net平台内建了对Web Service的支持,包括Web Service的构建和使用。与其它开发平台不一样,使用.net平台,你不须要其余的工具或者SDK就能够完成Web Service的开发了。.net Framework自己就全面支持Web Service,包括服务器端的请求处理器和对客户端发送和接受SOAP消息的支持。下来咱们就一步一步的用Microsoft Visual Studio .net 2005(后面简称VS.NET 2005)建立和使用一个简单的Web Service。html
2.一、用建立一个最简单的Web Serviceweb
首先,打开VS2005,打开“文件-新建-网站”,选择“ASP.Net Web服务”。浏览器
查看Service.cs代码,你会发现VS.Net 2005已经为Web Service文件创建了缺省的框架。原始代码为:服务器
1 using System;框架
2 using System.Web;函数
3 using System.Web.Services;工具
4 using System.Web.Services.Protocols网站
5 [WebService(Namespace = "http://tempuri.org/")]spa
6 [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)].net
7 public class Service : System.Web.Services.WebService
8 {
9 public Service ()
10 //若是使用设计的组件,请取消注释如下行
11 //InitializeComponent();
12 }
13 [WebMethod]
14 public string HelloWorld() {
15 return "Hello World";
16 }
17 }
默认工程里面已经有一个Hello World的方法了,直接运行看看效果,
点击显示页面上图中的“HelloWorld”超连接,跳转到下一页面
再点击“调用”按钮,就能够看到用XML格式返回的Web Service结果下图。说明咱们的Web Service环境没有问题,并且还初步接触了一下最简单的Web Service。
2.二、建立一个简单带有功能的Web Service
上面咱们宏观的了解了webservice,其实它就是个对外的接口,里面有函数可供外部客户调用(注意:里面一样有客户不可调用的函数).倘若咱们是服务端,咱们写好了个webservice,而后把它给了客户(同时咱们给了他们调用规则),客户就能够在从服务端获取信息时处于一个相对透明的状态.便是客户不了解(也不须要)其过程,他们只获取数据.在代码文件里,若是咱们写了一个函数后,但愿此函数成为外部可调用的接口函数,咱们必须在函数上面添上一行代码[WebMethod(Description="函数的描述信息")],若是你的函数没有这个申明,它将不能被用户引用.下来咱们开始编写一个简单的Web Service 的例子。
先把默认的HelloWorld方法注释掉,简单的写了求加减乘除运算的四个方法;
1 using System;
2 using System.Web;
3 using System.Web.Services;
4 using System.Web.Services.Protocols;
5
6 [WebService(Namespace = "http://tempuri.org/")]
7 [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
8 public class Service : System.Web.Services.WebService
9 {
10 public Service () {
11 //若是使用设计的组件,请取消注释如下行
12 //InitializeComponent();
13 }
14 //[WebMethod]
15 //public string HelloWorld() {
16 // return "Hello World";
17 //}
18 [WebMethod(Description="求和的方法")]
19 public double addition(double i,double j)
20 {
21 return i + j;
22 }
23 [WebMethod(Description="求差的方法")]
24 public double subtract(double i, double j)
25 {
26 return i - j;
27 }
28 [WebMethod(Description="求积的方法")]
29 public double multiplication(double i, double j)
30 {
31 return i * j;
32 }
33 [WebMethod(Description="求商的方法")]
34 public double division(double i, double j)
35 {
36 if (j != 0)
37 return i / j;
38 else
39 return 0;
40 }
41 }
42
运行能够看到咱们本身写的能够被调用的方法,以下图:
一样点击addition方法,进入addition方法的调用页。
在参数上面输入参数i=3,j=3,如上图,点击调用,就能够看到用XML格式返回的Web Service结果(i与j相加的结果)下图
到这里,咱们会发现,其实webservice并非那么的神秘,它也不过只是个接口,对咱们而言,侧重点就是是接口函数的编写.
2.三、用ASP.NET调用Web Service
首先,打开VS2005,打开“文件-新建-网站”,选择“ASP.NET网站”。
选好存储位置,语言后点击肯定,进入默认页面。而后先添加Web引用,把WebService引到当前的工程里面。方法是:在资源管理器中点击右键,选择添加Web 引用,调出对话框:
在URL中填入,前面写好的WebService运行后浏览器上面显示的地址,点击“前往”按钮,如上图,就会显示出所引用的WebService中能够调用的方法,而后点击“添加引用”,就将webservice引用到了当前的工程里面 ,以下图,解决方案中会出现引进来的WebService文件
咱们在这就练习调用webservice的四个方法,作一个简单的调用的例子,先在网站的前台添加几个控件,代码以下:
1 <%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
2
3 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
4 <html xmlns="http://www.w3.org/1999/xhtml" >
5 <head runat="server">
6 <title>Webservice调用实例</title>
7 </head>
8 <body>
9 <form id="form1" runat="server">
10 <div>
11 <asp:TextBox ID="Num1" runat="server"></asp:TextBox>
12 <select id="selectOper" runat = "server">
13 <option>+</option>
14 <option>-</option>
15 <option>*</option>
16 <option>/</option>
17 </select>
18 <asp:TextBox ID="Num2" runat="server"></asp:TextBox>
19 <span id = E runat = "server"></span>
20 <asp:TextBox ID="Result" runat="server"></asp:TextBox>
21 </div>
22 </form>
23 </body>
24 </html>
25
而后在后台写调用的代码,调用以前和使用其它的对象同样,要先实例化,实例化的方法是localhost.Service a = new localhost.Service();而后就能够经过a来访问WebService里面提供的方法了。在这个例子里面,动态的建立了一个button控件来触发WebService的调用,后台代码以下:
1 using System;
2 using System.Data;
3 using System.Configuration;
4 using System.Web;
5 using System.Web.Security;
6 using System.Web.UI;
7 using System.Web.UI.WebControls;
8 using System.Web.UI.WebControls.WebParts;
9 using System.Web.UI.HtmlControls;
10 public partial class _Default : System.Web.UI.Page
11 {
12 protected void Page_Load(object sender, EventArgs e)
13 {
14 //在页面加载的时候动态建立一个按钮,在它的事件里调用Webservice
15 Button btn = new Button();
16 btn.Width = 20;
17 btn.Text = " = ";
18 btn.Click +=new EventHandler(btn_Click);
19 E.Controls.Add(btn);
20 }
21 /// <summary>
22 /// 定义动态建立Button的Click事件,在这个事件中调用Webservice
23 /// </summary>
24 /// <param name="sender"></param>
25 /// <param name="e"></param>
26 void btn_Click(object sender, EventArgs e)
27 {
28 if (Num1.Text != "" && Num2.Text != "")
29 {
30 //实例化引用的webservice对象
31 localhost.Service WebserviceInstance = new localhost.Service();
32 int Oper = selectOper.SelectedIndex;
33 switch( Oper)
34 {
35 //经过实例化的webservice对象来调用Webservice暴露的方法
36 case 0:
37 Result.Text = WebserviceInstance.addition(double.Parse(Num1.Text), double.Parse(Num2.Text)).ToString();
38 break;
39 case 1:
40 Result.Text = WebserviceInstance.subtract(double.Parse(Num1.Text), double.Parse(Num2.Text)).ToString();
41 break;
42 case 2:
43 Result.Text = WebserviceInstance.multiplication(double.Parse(Num1.Text), double.Parse(Num2.Text)).ToString();
44 break;
45 case 3:
46 Result.Text = WebserviceInstance.division(double.Parse(Num1.Text), double.Parse(Num2.Text)).ToString();
47 break;
48 }
49 }
50 }
51 }
52
运行后能够看到效果,以下图所示,在前面两个Textbox里面输入两个操做数,在中间的下拉列表中选择操做符,而后点击“=”号,将计算的结果输出到第三个Textbox里面。
而整个计算并非在本地进行的,是在Web服务端进行计算的而后将结果经过XML返还给了调用方的,因此,在运行该程序的时候,WebService程序还必须启动(所谓的启动,就是那边的项目解决方案是必须是打开的),不然会报没法链接远程服务器的异常,以下图:
到此一个一个简单的WebService的开发和调用就已经完成了,在实际应用中能够根据本身的须要,写一些功能强大的,复杂的WebService,无论多么复杂,整个流程都是这样的。
文章来源:http://blog.csdn.net/lishimin1012/article/details/38269045