1、新建 WebService1.asmx ;
2、在WebService1.cs中加入代码:

[WebMethod]

public
int
WebAdd(
int
x,
int
y)

{

return x + y;

}


[WebMethod]

public
int
WebMultiply(
int
x,
int
y)

{

return x * y;

}
3、生成解决方案;
4、把程序放在localhost下WebService虚拟目录。
5、新建WinForm项目,拖入按钮控件btn_showServiceValue和标签控件lbl_msg;
6、添加Web引用http://localhost/WebService/WebService1.asmx,命名为localhost ;
7、在单击按钮事件中加入一下代码:

localhost.WebService1 service1
=
new
localhost.WebService1();

int
z
=
service1.WebAdd(
1
,
1
);

this
.lbl_msg.Text
=
"
1+1=
"
+
z.ToString();
8、执行WinForm项目,ok!
9、如果出现“WebException: 请求因 HTTP 状态 401 失败:Unauthorized”错误,解决办法:

1
、单击开始,指向管理工具,然后单击Internet 信息服务 (IIS)。

2
、展开“
*
服务器名称”(其中服务器名称 为该服务器的名称),右键单击Web 站点,然后单击属性。

3
、在Web 站点属性 对话框中,单击目录安全性 选项卡。

4
、在“身份验证和访问控制”下,单击编辑。

5
、单击“启用匿名访问”复选框,将其选中。


备注:

1
、“用户名”框中的用户帐户只用于通过 Windows guest 帐户进行匿名访问。

2
、默认情况下,服务器会创建并使用帐户 IUSR_computername。匿名用户帐户密码仅在Windows 中使用;匿名用户不使用用户名和密码登录。

3
、在“已验证身份的访问”下,单击“集成的 Windows 身份验证”复选框,将其选中。

单击确定 两次。
10、OK!