首先当然是开发一个webservice,我们来个简单的计算器的实现,代码如下
- using System;
- using System.Collections;
- using System.ComponentModel;
- using System.Data;
- using System.Linq;
- using System.Web;
- using System.Web.Services;
- using System.Web.Services.Protocols;
- using System.Xml.Linq;
- namespace WebServiceApp
- {
- /// <summary>
- /// CalcService 的摘要说明
- /// </summary>
- [WebService(Namespace = "http://tempuri.org/")]
- [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
- [ToolboxItem(false)]
- // 若要允许使用 ASP.NET AJAX 从脚本中调用此 Web 服务,请取消对下行的注释。
- //[System.Web.Script.Services.ScriptService]
- public class CalcService : System.Web.Services.WebService
- {
- [WebMethod]
- public double Add(double x, double y)
- {
- return x + y;
- }
- [WebMethod]
- public double Minus(double x, double y)
- {
- return x - y;
- }
- [WebMethod]
- public double Multiply(double x, double y)
- {
- return x * y;
- }
- [WebMethod]
- public double Devide(double x, double y)
- {
- return x / y;
- }
- [WebMethod]
- public double Power(double x, int n)
- {
- double result = 1;
- for (int i = 1; i <= n; i++)
- {
- result *= x;
- }
- return result;
- }
- [WebMethod]
- public double Sin(double x)
- {
- return Math.Sin(x);
- }
- [WebMethod]
- public double Cos(double x)
- {
- return Math.Cos(x);
- }
- }
- }
好,这么一个简单的webservice是开发完了,用IIS发布
调用的URL为: http://localhost/WebServiceApp/CalcService.asmx
下面分别介绍三种方式来调用该webservice,分别是:ASP.NET、Javascript、WinForm
先说第一种方式ASP.NET
新建一个ASP.NET网络应用程序,我取名为WebClient
第一步当然是添加web引用,把刚才的webservice调用网站复制到输入框中,然后取名为默认的localhost
在Default.aspx页面代码中添加如下代码
- <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="WebClient._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 runat="server">
- <title>WebService Test</title>
- </head>
- <body>
- <form id="form1" runat="server">
- <div>
- <asp:TextBox ID="num1" runat="server"></asp:TextBox>
- <asp:DropDownList ID="oper" runat="server" AutoPostBack="True"
- onselectedindexchanged="oper_SelectedIndexChanged">
- <asp:ListItem Text="+" Value="+" Selected="True"></asp:ListItem>
- <asp:ListItem Text="-" Value="-"></asp:ListItem>
- <asp:ListItem Text="*" Value="*"></asp:ListItem>
- <asp:ListItem Text="/" Value="/"></asp:ListItem>
- <asp:ListItem Text="power" Value="power"></asp:ListItem>
- <asp:ListItem Text="sin" Value="sin"></asp:ListItem>
- <asp:ListItem Text="cos" Value="cos"></asp:ListItem>
- </asp:DropDownList>
- <asp:TextBox ID="num2" runat="server"></asp:TextBox>
- <asp:Button ID="btnResult" runat="server" Text="=" οnclick="btnResult_Click" />
- <asp:TextBox ID="result" runat="server"></asp:TextBox>
- </div>
- </form>
- </body>
- </html>
也就是提供输入数字和显示结果的界面,很简单
后台代码为
- using System;
- using System.Collections;
- using System.Configuration;
- using System.Data;
- using System.Linq;
- using System.Web;
- using System.Web.Security;
- using System.Web.UI;
- using System.Web.UI.HtmlControls;
- using System.Web.UI.WebControls;
- using System.Web.UI.WebControls.WebParts;
- using System.Xml.Linq;
- namespace WebClient
- {
- public partial class _Default : System.Web.UI.Page
- {
- protected void Page_Load(object sender, EventArgs e)
- {
- }
- protected void btnResult_Click(object sender, EventArgs e)
- {
- double num1 = Double.Parse(this.num1.Text.Trim());
- double num2 = Double.Parse(this.num2.Text.Trim());
- string oper = this.oper.SelectedValue;
- double result = 0;
- localhost.CalcService service = new WebClient.localhost.CalcService();
- switch (oper)
- {
- case "+":
- result = service.Add(num1, num2);
- break;
- case "-":
- result = service.Minus(num1, num2);
- break;
- case "*":
- result = service.Multiply(num1, num2);
- break;
- case "/":
- result = service.Devide(num1, num2);
- break;
- case "power":
- result = service.Power(num1,(int)num2);
- break;
- case "sin":
- result = service.Sin(num1);
- break;
- case "cos":
- result = service.Cos(num1);
- break;
- default:
- result = service.Add(num1, num2);
- break;
- }
- this.result.Text = result + "";
- }
- protected void oper_SelectedIndexChanged(object sender, EventArgs e)
- {
- string oper = this.oper.SelectedValue;
- if (oper.Equals("sin") || oper.Equals("cos"))
- {
- this.num2.Visible = false;
- }
- else
- {
- this.num2.Visible = true;
- }
- }
- }
- }
着重要看的是下面一行代码
localhost.CalcService service = new WebClient.localhost.CalcService();
该代码将创建了一个我们开发的webservice的计算器实例
然后通过该实例去调用webservice暴露出来的服务接口
结果