1.新建一个web空项目:

2.添加web服务:

3.在添加的web服务中添加一个求和的方法:(注:必须在方法上面添加 [WebMethod]才会在网页上显示出来,其中(description为方法的描述))
using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.Services; namespace WebServiceDemo { /// <summary> /// WebServiceDemo 的摘要说明 /// </summary> [WebService(Namespace = "http://tempuri.org/")] [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)] [System.ComponentModel.ToolboxItem(false)] // 若要允许使用 ASP.NET AJAX 从脚本中调用此 Web 服务,请取消对下行的注释。 // [System.Web.Script.Services.ScriptService] public class WebServiceDemo : System.Web.Services.WebService { [WebMethod] public string HelloWorld() { return "Hello World"; } /// <summary> /// 两个数求和的方法 /// </summary> /// <param name="a">第一个数字</param> /// <param name="b">第二个数字</param> /// <returns>返回两个数字的和</returns> [WebMethod(Description="求和的方法")] public int GetSum(int a,int b) { return a + b; } } }
4.本地浏览调用:



5.发布:(点击项目-->鼠标右键-->发布-->发布方法选择文件系统-->选择发布目录-->点击发布)



6.安装iis参考:https://jingyan.baidu.com/article/19192ad853224ce53f570748.html
7.部署,点击网站-->鼠标右键添加网站(物理路径:选择发布的文件夹;ip地址:本机ip;端口:自定义未被占用的任意的端口)-->确定

8.浏览

报如下错误:

添加默认文档即可:文档名称为:添加的web服务时候自定义的名称


再浏览,可能报错:

点击应用程序池,切换CLR版本;

再浏览:

成功!!
9.客户端调用:
新建一个控制台来测试

点击引用->鼠标右键-->添加服务引用:

点击高级

添加web引用:

将浏览器浏览时的地址复制并粘贴到URL

点击箭头(转到),即可看到自己写的方法;

点击添加引用

添加成功!!
调用webservice中的方法:
using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace WebServiceClient { class Program { static void Main(string[] args) { //创建service对象 WebReference.WebServiceDemo service = new WebReference.WebServiceDemo(); int a = 100; int b = 100; Console.WriteLine("{0}+{1}={2}",a,b,service.GetSum(a,b)); Console.ReadKey(); } } }
运行:

成功!!
以上就是一个webservice创建、发布、部署、调用的简单实例、如有错误,欢迎指教!!
C#创建webservice并发布,部署到iis,客户端调用
原文:http://www.cnblogs.com/jiangxianshen/p/7490607.html