web service类什么时候初始化?

下面的这个例子演示了web service是如何初始化的:
1.创建测试用的web service.
 1using System;
 2using System.Web;
 3using System.Web.Services;
 4using System.Web.Services.Protocols;
 5using System.IO;
 6
 7[WebService(Namespace = "http://tempuri.org/")]
 8[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
 9public class Service : System.Web.Services.WebService
10{
11    private static int counter = 0;
12    public Service()
13    {
14
15        //Uncomment the following line if using designed components 
16        //InitializeComponent(); 
17        counter++;
18        using (System.IO.StreamWriter w = File.AppendText(@"c:\a\time.txt"))
19        {
20            w.WriteLine("You have called this webservice " +counter.ToString()  + " times, the lates call happened at: " + DateTime.Now.ToString());
21        }

22        
23    }

24
25    [WebMethod]
26    public string HelloWorld(string name)
27    {
28        //currentMethod = "HelloWord";
29        return "Hello World: " + name;
30    }

31    [WebMethod]
32    public string Welcome(string name)
33    {
34         return "Welcome: " + name;
35    }

36
37}

38
从代码中可以看出,在服务器的构造函数中只是在一个文件中记录了当前服务器的时间和一共调用了web service的次数.
注意:必须给对应的目录添加对ASPNET帐号的写权限,否则会出现访问拒绝的异常.

2.创建客户端测试代码
 1using System;
 2using System.Collections.Generic;
 3using System.ComponentModel;
 4using System.Data;
 5using System.Drawing;
 6using System.Text;
 7using System.Windows.Forms;
 8using System.Net;
 9
10namespace WSTest
11{
12    public partial class Form1 : Form
13    {
14        localhost.Service s = null;
15        public Form1()
16        {
17            InitializeComponent();
18            s = new localhost.Service();
19        }

20
21        private void btnHelloWorld_Click(object sender, EventArgs e)
22        {
23            MessageBox.Show(s.HelloWorld("zzq"));
24        }

25
26        private void btnWelcome_Click(object sender, EventArgs e)
27        {
28            MessageBox.Show(s.Welcome("zzq"));
29        }

30
31      
32    }

33}

只是一个简单的win form程序,你可以通过两个按钮按不同的次序调用两个web method,然后看生成的文件里记录的信息.

3.下面是生成的文本文件的截图.

.图片错误

4. 结论
 web service在每一个请求来之前都会重新初始化一次,每调用其中的一个web method都会造成web service被重新初始化.

posted on 2007-03-26 14:53 zhanqiangz(闲云野鹤) 阅读(391) 评论(0)  编辑 收藏 所属分类: ASP.NET2.0CSharp读书笔记WebService


标题  
姓名  
主页
Email (只有博主才能看到) 
验证码 *  看不清,换一张 [登录][注册]
内容(请不要发表任何与政治相关的内容)  
  登录  使用高级评论  新用户注册  返回页首  恢复上次提交      
 
另存  打印
 


<2007年3月>
25262728123
45678910
11121314151617
18192021222324
25262728293031
1234567

导航

统计

公告

如转载本博客的相关资料敬请注明出处.谢谢合作!

与我联系

常用链接

留言簿(6)

我参与的团队

我的标签

随笔分类(51)

随笔档案(49)

文章分类(7)

文章档案(5)

收藏夹(3)

.NET

BizTalk

Design

JavaScript

My Blogs

Power Tools

Process Control

Search Engine

SQL Server

XML

搜索

积分与排名

最新评论

阅读排行榜

评论排行榜