一、建立WebService
1:新建一个ASP.NET项目
![](https://i-blog.csdnimg.cn/blog_migrate/24dec1b0e97fe4968c600612097311c0.png)
2:添加WebService
![](https://i-blog.csdnimg.cn/blog_migrate/ceb3fe86f4601b7fb68f45f1e34ac16d.png)
3:写入两个简单的方法(方法上面一定要加[WebMethod]特性)
[WebMethod]
public int Add(int a, int b)
{
return a + b;
}
[WebMethod]
public int Sub(int a, int b)
{
return a - b;
}
3:发布到文件夹
![](https://i-blog.csdnimg.cn/blog_migrate/f59240ab72e29761649810e9bae555ff.png)
4:打开IIS 配置网站信息(物理路径为刚刚发布的WebService文件夹路径)
![](https://i-blog.csdnimg.cn/blog_migrate/d98f1f9a8e438835a7253f1359242bb5.png)
二、使用WebService
1:新建项目,在引用处添加服务器引用
![](https://i-blog.csdnimg.cn/blog_migrate/686128f6982d8b65183ae63a24bcaeb9.png)
2:在namespace中using刚刚引用的WebService。
private void button1_Click(object sender, EventArgs e)
{
IndexSoapClient indexSoapClient = new IndexSoapClient();
int res= indexSoapClient.Add(20, 10);
MessageBox.Show(res.ToString(),"提示信息",MessageBoxButtons.OK,MessageBoxIcon.Information);
indexSoapClient = null;
}
即可调用成功!