1.创建Webservice服务应用程序
方式一:将VS2010采用的默认框架,改为使用框架.NET Framework2.0\3.0\3.5,这时新建"项目"or新建"网站"就可以找到需要的"ASP.NET WebService模板"来建立WebService了。
方式二:创建空的web应用程序项目,新建项选择Web服务。
2.添加WebService代码。代码使用方式和功能已含在代码注释中
using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.Services; namespace WebServiceByEmptyWeb { /// <summary> /// WebService1 的摘要说明 /// </summary> [WebService(Namespace = "HwSoft")] [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)] [System.ComponentModel.ToolboxItem(false)] // 若要允许使用 ASP.NET AJAX 从脚本中调用此 Web 服务,请取消对下行的注释。 // [System.Web.Script.Services.ScriptService] public class WebService1 : System.Web.Services.WebService { //[WebMethod] //public string HelloWorld() //{ // return "Hello World"; //} //只有加WebMethod特性的方法才能被引用 [WebMethod(Description = "求和的方法")] public double addition(double number1,double number2) { return number1 + number2; } [WebMethod(Description="求差的方法")] public double subtraction(double number1, double number2) { return number1 - number2; } [WebMethod(Description=("求积的方法"))] public double multiplication(double number1, double number2) { return number1 * number2; } [WebMethod(Description="求商的方法")] public double division(double number1, double number2) { if (number2 != 0) { return number1 / number2; } else return -1; } } }
3.用web测试webservice
创建web网站项目后添加【添加Web引用】。注意Web引用名,调用WebService使用
web网站测试代码:
前台:
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_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></title> </head> <body> <form id="form1" runat="server"> <div> <asp:TextBox ID="Num1" runat="server"></asp:TextBox> <asp:DropDownList ID="ddlOperator" runat="server"> <asp:ListItem Text="+" Value="add"></asp:ListItem> <asp:ListItem Text="-" Value="sub"></asp:ListItem> <asp:ListItem Text="*" Value="mul"></asp:ListItem> <asp:ListItem Text="/" Value="div"></asp:ListItem> </asp:DropDownList> <asp:TextBox ID="Num2" runat="server"></asp:TextBox> <span id="E" runat="server"></span> <asp:TextBox ID="Result" runat="server"></asp:TextBox> </div> </form> </body> </html>
C#后台代码
using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.UI; using System.Web.UI.WebControls; public partial class _Default : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { //动态创建按钮 Button btn = new Button(); btn.Text = "="; btn.Width = 25; btn.Click += new EventHandler(btn_Click); E.Controls.Add(btn); } protected void btn_Click(object sender, EventArgs e) { if (Num1.Text != "" && Num2.Text != "") { localhost.WebService1 webservice = new localhost.WebService1(); string operate = ddlOperator.SelectedValue; double number1 = double.Parse(Num1.Text ); double number2 = double.Parse(Num2.Text); switch (operate) { case "add": Result.Text = webservice.addition(number1,number2).ToString("F2"); break; case "sub": Result.Text =webservice.subtraction(number1,number2).ToString("F2"); break; case "mul": Result.Text = webservice.multiplication(number1,number2).ToString("F2"); break; case "div": Result.Text = webservice.division(number1,number2).ToString("F2"); break; } } } }
4.添加其他类型的Webservice
using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.Services; using System.Data; using System.ComponentModel; namespace WebServiceByEmptyWeb { /// <summary> /// OtherTypeWebServices 的摘要说明 /// </summary> [WebService(Namespace = "HwSoft")] [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)] [System.ComponentModel.ToolboxItem(false)] // 若要允许使用 ASP.NET AJAX 从脚本中调用此 Web 服务,请取消对下行的注释。 // [System.Web.Script.Services.ScriptService] public class OtherTypeWebServices : System.Web.Services.WebService { public OtherTypeWebServices() { //CODEGEN: 该调用是 ASP.NET Web 服务设计器所必需的 InitializeComponent(); } //Web 服务设计器所必需的 private IContainer components = null; /// <summary> /// 设计器支持所需的方法 - 不要使用代码编辑器修改 /// 此方法的内容。 /// </summary> private void InitializeComponent() { } /// <summary> /// 清理所有正在使用的资源。 /// </summary> protected override void Dispose(bool disposing) { if (disposing && components != null) { components.Dispose(); } base.Dispose(disposing); } [WebMethod] public string HelloWorld() { return "Hello World"; } [WebMethod(Description="返回整型数据")] public int GetInt() { return 1234; } [WebMethod(Description="返回布尔数据")] public bool GetBool() { return true; } [WebMethod(Description="返回实体类")] public Student GetStudent() { Student stu = new Student(); stu.Name = "张三"; stu.Age = 25; stu.Sex = true; return stu; } [WebMethod(Description="返回DataSet")] public DataSet GetDataSet() { DataSet ds = new DataSet(); return ds; } } }
上述中含有的Student类
using System; using System.Collections.Generic; using System.Linq; using System.Web; namespace WebServiceByEmptyWeb { /* * 为避免Framework1.1中返回实体类报错“请求格式无法识别。” * 要在Web.Config文件中<system.web>添加以下内容: * <webServices> <protocols> <add name="HttpPost" /> <add name="HttpGet" /> </protocols> </webServices> */ /// <summary> /// 学生基本信息类 /// 可序列化(webservice才能调用) /// </summary> [Serializable] public class Student { public Student() { } private string name; /// <summary> /// 姓名 /// </summary> public string Name { get { return name; } set { name = value; } } private bool sex; /// <summary> /// 性别(布尔变量真为女,假为男) /// </summary> public bool Sex { get { return sex; } set { sex = value; } } private int age; /// <summary> /// 年龄 /// </summary> public int Age { get { return age; } set { age = value; } } } }
5.WinForm测试4中建立的Webservice
在WinForm项目中【添加服务引用】- 【高级】 -【添加web引用】 界面与添加Web程序一致
winForm程序:
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; namespace OtherTypeWebServiecTest { public partial class Form1 : Form { public Form1() { InitializeComponent(); } //类 private void button1_Click(object sender, EventArgs e) { localhost.OtherTypeWebServices webservice = new localhost.OtherTypeWebServices(); localhost.Student stu = webservice.GetStudent(); txbName.Text = stu.Name; txbSex.Text = stu.Sex==false?"男":"女"; txbAge.Text = stu.Age.ToString(); } //DataTable private void button2_Click(object sender, EventArgs e) { localhost.OtherTypeWebServices webService = new localhost.OtherTypeWebServices(); txbOther.Text = webService.GetDataSet().Tables.Count.ToString(); } //Int private void button3_Click(object sender, EventArgs e) { localhost.OtherTypeWebServices webService = new localhost.OtherTypeWebServices(); txbOther.Text = webService.GetInt().ToString(); } //Bool private void button4_Click(object sender, EventArgs e) { localhost.OtherTypeWebServices webService = new localhost.OtherTypeWebServices(); txbOther.Text = webService.GetBool().ToString(); } //Hello World private void button5_Click(object sender, EventArgs e) { localhost.OtherTypeWebServices webService = new localhost.OtherTypeWebServices(); txbOther.Text = webService.HelloWorld(); } } }
测试结果显示:
要么忍,要么狠,要么滚!