首先,创建Webservice服务应用
1.打开Visual 2005,新建项目
系统会自动生成代码,Web服务的类继承自System.Web.services.WebService类.此外,Web服务对外提供的方法均需有[WebMethod]属性标记,对于供内部调用,则不需要使用.在系统自动生成的代码中,已经提供一个返回字符串(Hello world)的方法,这里再添加了一个计算两个整数的和的方法.代码如下:
using
System;
using System.Data;
using System.Web;
using System.Collections;
using System.Web.Services;
using System.Web.Services.Protocols;
using System.ComponentModel;
namespace WebService
... {
/**//// <summary>
/// Service1 的摘要说明
/// </summary>
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[ToolboxItem(false)]
public class Service1 : System.Web.Services.WebService
...{
[WebMethod]
public string HelloWorld()
...{
return "Hello World";
}
[WebMethod]
public int Add(int x,int y)
...{
return x + y;
}
}
}
using System.Data;
using System.Web;
using System.Collections;
using System.Web.Services;
using System.Web.Services.Protocols;
using System.ComponentModel;
namespace WebService
... {
/**//// <summary>
/// Service1 的摘要说明
/// </summary>
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[ToolboxItem(false)]
public class Service1 : System.Web.Services.WebService
...{
[WebMethod]
public string HelloWorld()
...{
return "Hello World";
}
[WebMethod]
public int Add(int x,int y)
...{
return x + y;
}
}
}
保存并生成项目.这样,一个Web服务就创建完成.
2.发布Web服务
创建好Web服务项目后,首先生成Web服务
输出Web服务
最后发布Web服务
注:.NET平台下的Web服务是创建在Web服务器IIS上的,因此,如果要在本机上创建和运行Web服务,必须事先安装IIS服务器,可以通过"添加/删除Windows组件"来安装IIS.
3.调用Web服务
新建一个Windows应用程序,设计界面
添加要引用的Web服务
编写代码
using
System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using WinDemo.localhost;
namespace WinDemo
... {
public partial class Form1 : Form
...{
public Form1()
...{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
...{
Service1 ser = new Service1();
label1.Text = ser.HelloWorld();
ser.Dispose();
}
private void textBox1_KeyPress(object sender, KeyPressEventArgs e)
...{
if (e.KeyChar != 8 && !char.IsDigit(e.KeyChar))
...{
e.Handled = true;
}
}
private void textBox2_KeyPress(object sender, KeyPressEventArgs e)
...{
if (e.KeyChar != 8 && !char.IsDigit(e.KeyChar))
...{
e.Handled = true;
}
}
private void button1_Click(object sender, EventArgs e)
...{
Service1 ser = new Service1();
label3.Text = Convert.ToString(ser.Add(Convert.ToInt32(textBox1.Text.Trim()), Convert.ToInt32(textBox2.Text.Trim())));
ser.Dispose();
}
private void button2_Click(object sender, EventArgs e)
...{
Application.Exit();
}
}
}
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using WinDemo.localhost;
namespace WinDemo
... {
public partial class Form1 : Form
...{
public Form1()
...{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
...{
Service1 ser = new Service1();
label1.Text = ser.HelloWorld();
ser.Dispose();
}
private void textBox1_KeyPress(object sender, KeyPressEventArgs e)
...{
if (e.KeyChar != 8 && !char.IsDigit(e.KeyChar))
...{
e.Handled = true;
}
}
private void textBox2_KeyPress(object sender, KeyPressEventArgs e)
...{
if (e.KeyChar != 8 && !char.IsDigit(e.KeyChar))
...{
e.Handled = true;
}
}
private void button1_Click(object sender, EventArgs e)
...{
Service1 ser = new Service1();
label3.Text = Convert.ToString(ser.Add(Convert.ToInt32(textBox1.Text.Trim()), Convert.ToInt32(textBox2.Text.Trim())));
ser.Dispose();
}
private void button2_Click(object sender, EventArgs e)
...{
Application.Exit();
}
}
}
占华