PS: 开发工具 VS2010, 所有工程都为Debug状态,本人刚接触 Web Service,此文为菜鸟入门用例,高手勿笑!

转载请注明出处 :http://www.cnblogs.com/yycxbjl/archive/2010/04/20/1716689.html

一、创建Web Service 工程

1. 新建一个 Web Service 工程,工程名为WebServiceProject

File -> New -> Project  -->   Visual C# -> Web -> ASP.NET Web Service Application

注意: .NET Framework版本选3.5, 4.0 中没有该类型的工程

2. 在WebServiceProject中,删除 Servie1 类中原有的HelloWorld方法,添加一个方法 ReverseString

        [WebMethod]
publicstring ReverseString(string s)
{
System.Threading.Thread.Sleep();
char[] a = s.ToCharArray();
Array.Reverse(a);
returnnewstring(a);
}

必须加上在方法前加上 [WebMethod] 属性

方法中首行的 Sleep(5000) 为了展示下文中同步调用与异步调用 Web Service中方法的区别

将   [WebService(Namespace = "http://tempuri.org/")]

改为 [WebService(Namespace = "http://WebServiceSample/")]     名字随便取

可以不改,若不改,下一步通过浏览器查看时会有提示(可以自己看一下)

3. 编译并测试WebServiceProject

按 F7编译工程,通过浏览器查看Servic1.asmx

由于工程中只有一个方法,页面显示如下:

点击ReverseString,可以进入该方法的测试页面,进行测试

二、部署Web Service

1.  发布工程到本地的某一个目录(D:\WebServiceSample)

2. 发布完后,在IIS中添加一个指向该目录的虚拟目录(或应用程序)

3. 通过 浏览器 查看,测试发布是否成功

http://localhost/webservicesample/service1.asmx

页面显示应与上一节中相同

三、使用Web Service

1.  使用WSDL 工具生成 Web Service 中 Servie1类的代理类

打开 VS2010 命名行工具

输入如下命名,在D:\生成一个myProxyClass.cs文件,里面有一个代理类

public partial class Service1 : System.Web.Services.Protocols.SoapHttpClientProtocol

关于如何生成代理类的详见

http://msdn.microsoft.com/zh-cn/library/7h3ystb6.aspx 

2. 新建一个Windows Form Application,来使用Web Service,工程名为 WebServiceClient

在工程中添加步骤1中生成的myProxyClass.cs文件

添加 System.Web.Services引用:Project -> Add Reference

3. 在Form1上,拖入几个控件

4.  为按钮添加事件响应函数

        //同步
privatevoid button1_Click(object sender, EventArgs e)
{
Service1 ws =new Service1();
textBox2.Text = ws.ReverseString(textBox1.Text);
} //异步
privatevoid button2_Click(object sender, EventArgs e)
{
Service1 ws =new Service1();
ws.ReverseStringCompleted +=new ReverseStringCompletedEventHandler(ReverseStringCompleted);
ws.ReverseStringAsync(textBox1.Text);
} privatevoid ReverseStringCompleted(object sender, ReverseStringCompletedEventArgs e)
{
textBox2.Text = e.Result;
}

5.  测试程序的效果

用同步响应时,在Web Service中的方法结束前,程序无法响应

用异步响应时,在Web Service中的方法结束前,程序可以响应

最新文章

  1. 软件架构---nop插件学习
  2. 在访问jsp时抛java.lang.IllegalArgumentException: Page directive: invalid value for import的原因
  3. 2017年8个UI设计流行趋势
  4. Goals100
  5. 在android中用跑马灯的效果显示textview
  6. Ubuntu 下Eclipse 安装SVN
  7. POJ 3080 Blue Jeans (多个字符串的最长公共序列,暴力比较)
  8. Android防微信首页左右滑动切换
  9. Android中获取应用程序(包)的大小-----PackageManager的使用(二)
  10. python基础(六)
  11. MIPI接口资料汇总(精)
  12. python 模块和包
  13. 29.Junit测试框架.md
  14. Android SDK更新8.1.0时报错
  15. ACM HDU-2952 Counting Sheep
  16. (转)Oracle存储过程中的事务
  17. HDU 5222 ——Exploration——————【并查集+拓扑排序判有向环】
  18. GEEK学习笔记— —程序猿面试宝典笔记(三)
  19. TensorFlow实战:Chapter-4(CNN-2-经典卷积神经网络(AlexNet、VGGNet))
  20. V4L2 camera 驱动 capture测试程序【转】

热门文章

  1. ZOJ 3829 Known Notation 贪心 难度:0
  2. 快速切题 sgu105. Div 3 数学归纳 数位+整除 难度:0
  3. 主席树模板(poj 2104&&poj2761)
  4. 数据库schema的简介
  5. 远程访问Centos6.5上的mysql或者mariadb(navicat)
  6. 《Python》 基础数据类型和for循环
  7. SWIFT模糊效果
  8. sparksql与hive整合
  9. Samsung_tiny4412(驱动笔记07)----spinlock,semaphore,atomic,mutex,completion,interrupt
  10. c++ 字符数组-print and 写入文件